Download lecture-6-polymorphism-and-method-overloading

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lecture 6
Lecture 6
© LPU :: CSE310 Programming in Java :: Sawal Tandon
Outcome of the Lecture
Upon completion of this lecture you will be able to
 Overload the methods
 Understand the concept of polymorphism
 Know the concept of Dynamic Binding and Generic
Programming
Lecture 6
© LPU :: CSE310 Programming in Java :: Sawal Tandon
Outline of the Presentation
 Method Overloading
 Subtype and Supertype
 Polymorphism and Dynamic Binding
 Generic Programming
 Casting Objects and instanceof operator
Lecture 6
© LPU :: CSE310 Programming in Java :: Sawal Tandon
Method Overloading
 Defining multiple methods with the same name but different signatures
within same class
 Java compiler determines which method is used based on the method
signature
 Overloaded methods must have different parameter lists. You cannot
overload methods based on different modifiers or return types
Lecture 6
© LPU :: CSE310 Programming in Java :: Sawal Tandon
Method Overloading
public class TestMethodOverloading {
public static void main(String[] args) {
System.out.println("The maximum between 3 and 4 is "+
System.out.println("The maximum between 3.0 and 5.4 is "+
System.out.println("The maximum between 3.0, 5.4, and 10.14 is "+
}
public static
if (num1 > num2)
return num1;
else
return num2;
}
public static
if (num1 > num2)
return num1;
else
return num2;
}
public static
{
return max(max(num1, num2), num3);
}
}
Lecture 6
© LPU :: CSE310 Programming in Java :: Sawal Tandon
Method Overloading
 Sometimes there are two or more possible matches for an invocation of a method,
but the compiler cannot determine the most specific match. This is referred to as
.
public class AmbiguousOverloading {
public static void main(String[] args) {
System.out.println(max(1, 2));
}
public static double max(int num1, double num2) {
if (num1 > num2)
return num1;
else
return num2;
}
public static double max(double num1, int num2) {
if (num1 > num2)
return num1;
else
return num2;
}
}
Lecture 6
© LPU :: CSE310 Programming in Java :: Sawal Tandon
Subtype and Supertype
 A class defines a
.
 A type defined by a subclass is called a
and a type defined by its
superclass is called a
 Circle is a subtype of GeometricObject and GeometricObject is a supertype
for Circle.
 inheritance relationship enables a subclass to inherit features from its
superclass with additional new features
 subclass is a specialization of its superclass; every instance of a subclass is
also an instance of its superclass,
 every circle is a geometric object, but not every geometric object is a circle
 An object of a subtype can be used wherever its supertype value is required.
This feature is known as
.
Polymorphism and Dynamic Binding
public class PolymorphismDemo {
public static void main(String[] args) {
m(new GraduateStudent());
m(new Student());
Method m takes a parameter of the Object
m(new Person());
type. You can invoke it with any object.
m(new Object());
}
An object of a subtype can be used wherever
public static void m(Object x) {
its supertype value is required. This feature is
System.out.println(x.toString());
known as polymorphism.
}
}
class GraduateStudent extends Student {
}
When the method m(Object x) is executed, the argument
class Student extends Person {
x’s toString method is invoked. x may be an instance of
public String toString() {
GraduateStudent, Student, Person, or Object. Classes
return "Student";
}
GraduateStudent, Student, Person, and Object have their
}
own implementation of the toString method.
class Person extends Object {
public String toString() {
return "Person";
This capability is
}
known as
}
Lecture 6
© LPU :: CSE310 Programming in Java :: Sawal Tandon
Generic Programming
public class PolymorphismDemo {
public static void main(String[] args) {
m(new GraduateStudent());
m(new Student());
m(new Person());
m(new Object());
}
public static void m(Object x) {
System.out.println(x.toString());
}
}
class GraduateStudent extends Student {
}
class Student extends Person {
public String toString() {
return "Student";
}
}
class Person extends Object {
public String toString() {
return "Person";
}
}
Lecture 6
Polymorphism allows methods to be
used generically for a wide range of
object arguments. This is known as
Generic Programming.
© LPU :: CSE310 Programming in Java :: Sawal Tandon
Casting Objects and instanceof operator
 Casting can be used to convert an object of one class type to
another within an inheritance hierarchy.
 Casting from subclass to super class is
Object o = new Student();
 Casting from super class to sub class needs to be
Student b = (Student)o
Lecture 6
© LPU :: CSE310 Programming in Java :: Sawal Tandon
Casting Objects and instanceof operator
 If the superclass object is not an instance of the subclass, a runtime
ClassCastException occurs
operator is used to test whether an object is an instance of a class
Object myObject = new Circle();
if (myObject instanceof Circle) {
System.out.println("The circle diameter Circle)myObject).getDiameter());
...
}
 To enable generic programming, it is a good practice to define a variable
with a supertype, which can accept a value of any subtype
Lecture 6
© LPU :: CSE310 Programming in Java :: Sawal Tandon
Casting Objects and instanceof operator
 Object member access operator (.) precedes the casting operator.
 Parentheses should be used to ensure that casting is done before the .
operator, as in
((Circle)object).getArea());
Lecture 6
© LPU :: CSE310 Programming in Java :: Sawal Tandon