You are on page 1of 28

Inheritance Polymorphism

Scanner and Wrapper

Demonstration of function overloading and overriding. Multiple


constructors. Using ‘super’ and ‘this’ keywords. Use of scanner
class to get input from users. Wrapper class concept.
Overloading
● Different ways of Overloading:
○ Changing the Number of Parameters.
○ Changing Data Types of the Arguments.
○ Changing the Order of the Parameters of Methods
What if the exact prototype does not match with arguments?
Priority-wise, the compiler takes these steps:
● Type Conversion but to a higher type(in terms of range) in the same
family.
● Type conversion to the next higher family(suppose if there is no long
data type available for an int data type, then it will search for the float
data type).
Overloading
What if the exact prototype does not match
with arguments?
Priority-wise, the compiler takes these
steps:
● Type Conversion but to a higher
type(in terms of range) in the same
family.
● Type conversion to the next higher
family(suppose if there is no long data
type available for an int data type,
then it will search for the float data
type).
Overloading
● Advantages of Method Overloading
○ Method overloading improves the Readability and reusability
of the program.
○ Method overloading reduces the complexity of the program.
○ Using method overloading, programmers can perform a task
efficiently and effectively.
○ Using method overloading, it is possible to access methods
performing related functions with slightly different arguments
and types.
○ Objects of a class can also be initialized in different ways
using the constructors.
Overloading
● Some Salient points:
○ We can override static methods
○ We cannot overload methods if one is static and
the other is not
○ We can overload main()
○ We cannot overload by return types
○ We cannot overload an operator (unlike C++)
Overloading - Changing the Number of Parameters.
Overloading - Changing Data Types of the Arguments.
Overloading - Changing the Order of the Parameters of Methods
Overloading - Multiple main
Overriding
Overriding - Rules
● Overriding and Access Modifiers
○ The access modifier for an overriding method can allow more,
but not less, access than the overridden method. For example,
a protected instance method in the superclass can be made
public, but not private, in the subclass. Doing so will generate
a compile-time error.
● Final methods can not be overridden
○ If we donʼt want a method to be overridden, we declare it as
final.
Overriding - Rules
Superclass Superclass
Static methods can not be Instance Static
overridden(Method Overriding vs Method Method

Method Hiding):
When you define a static method with Subclass Generates a
Instance Overrides compile-time
the same signature as a static method Method error

in the base class, it is known as


method hiding. The following table
Subclass Generates a
summarizes what happens when you Static compile-time Hides
Method error
define a method with the same
signature as a method in a super-class.
Overriding - Rules
● Private methods can not be overridden
○ Private methods cannot be overridden as they are bonded
during compile time. Therefore we canʼt even override private
methods in a subclass.
● The overriding method must have the same return type (or subtype)
○ From Java 5.0 onwards it is possible to have different return
types for an overriding method in the child class, but the childʼs
return type should be a sub-type of the parentʼs return type.
This phenomenon is known as the covariant return type.
Overriding - Rules
● Invoking overridden method from sub-class
○ We can call the parent class method in the overriding method
using the super keyword.
● Overriding and Constructor
○ We cannot override the constructor as the parent and child
class can never have a constructor with the same name(The
constructor name must always be the same as the Class name).
Overriding - Rules
● Overriding and Abstract
Method
○ Abstract methods in an
interface or abstract
class are meant to be
overridden in derived
concrete classes
otherwise a
compile-time error will
be thrown.
Overriding - examples
Overriding - examples
Overriding - examples
Overriding - examples
Overriding - ask your instructor for other examples
Use of scanner to take inputs from user
Method Description

Used for reading Boolean


nextBoolean()
value

nextByte() Used for reading Byte value

Used for reading Double


nextDouble()
value

nextFloat() Used for reading Float value

nextInt() Used for reading Int value

nextLine() Used for reading Line value

nextLong() Used for reading Long value

nextShort() Used for reading Short value


Important Points About Java Scanner Class
● To create an object of Scanner class, we usually pass the predefined object System.in,
which represents the standard input stream. We may pass an object of class File if we
want to read input from a file.
● To read numerical values of a certain data type XYZ, the function to use is nextXYZ(). For
example, to read a value of type short, we can use nextShort()
● To read strings, we use nextLine().
● To read a single character, we use next().charAt(0). next() function returns the next
token/word in the input as a string and charAt(0) function returns the first character in
that string.
● The Scanner class reads an entire line and divides the line into tokens. Tokens are small
elements that have some meaning to the Java compiler. For example, Suppose there is
an input string: How are you
● In this case, the scanner object will read the entire line and divides the string into
tokens: “How”, “are” and “you”. The object then iterates over each token and reads each
token using its different methods.
Wrapper class
● A wrapper class wraps (encloses) around a data type and gives it
an object appearance. Wrapper classes are final and immutable.
Two concepts are there in the wrapper classes namely autoboxing
and unboxing.
● Autoboxing is a procedure of converting a primitive value into an
object of the corresponding wrapper class. For example, converting
int to Integer class. The Java compiler applies autoboxing when a
primitive value is:
○ Passed as a parameter to a method that expects an object of the
corresponding wrapper class.
○ Assigned to a variable of the corresponding wrapper class.
Wrapper class
● A wrapper class wraps (encloses) around a data type and gives it
an object appearance. Wrapper classes are final and immutable.
Two concepts are there in the wrapper classes namely autoboxing
and unboxing.
● Unboxing is a procedure of converting an object of a wrapper type
to its corresponding primitive value. For example conversion of
Integer to int. The Java compiler applies to unbox when an object of
a wrapper class is:
○ Passed as a parameter to a method that expects a value of the
corresponding primitive type.
○ Assigned to a variable of the corresponding primitive type.
Wrapper example
Wrapper example
Thanks to:

Saving lives of CS guys since 2008!


Now your Instructor will give you problems of
her/his choice:

By now, I guess,
you all are
probably
feeling like this
guy ->

You might also like