Lecture 6 LPU CSE 310 Programming in Java

  • Slides: 12
Download presentation
Lecture 6 © LPU : : CSE 310 Programming in Java : : Sawal

Lecture 6 © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Outcome of the Lecture Upon completion of this lecture you will be able to

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 : : CSE 310 Programming in Java : : Sawal Tandon

Outline of the Presentation ü Method Overloading ü Subtype and Supertype ü Polymorphism and

Outline of the Presentation ü Method Overloading ü Subtype and Supertype ü Polymorphism and Dynamic Binding ü Generic Programming ü Casting Objects and instanceof operator Lecture 6 © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Method Overloading ü Defining multiple methods with the same name but different signatures within

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 : : CSE 310 Programming in Java : : Sawal Tandon

Method Overloading public class Test. Method. Overloading { public static void main(String[] args) {

Method Overloading public class Test. Method. Overloading { public static void main(String[] args) { System. out. println("The maximum between 3 and 4 is "+ max(3, 4)); System. out. println("The maximum between 3. 0 and 5. 4 is "+max(3. 0, 5. 4)); System. out. println("The maximum between 3. 0, 5. 4, and 10. 14 is "+max(3. 0, 5. 4, 10. 14 )); } public static int max(int num 1, int num 2){ if (num 1 > num 2) return num 1; else return num 2; } public static double max(double num 1, double num 2, double num 3) { return max(num 1, num 2), num 3); } } Lecture 6 © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Method Overloading ü Sometimes there are two or more possible matches for an invocation

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 ambiguous invocation. public class Ambiguous. Overloading { public static void main(String[] args) { System. out. println(max(1, 2)); } public static double max(int num 1, double num 2) { if (num 1 > num 2) return num 1; else return num 2; } public static double max(double num 1, int num 2) { if (num 1 > num 2) return num 1; else return num 2; } } Lecture 6 © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Subtype and Supertype ü A class defines a type. ü A type defined by

Subtype and Supertype ü A class defines a type. ü A type defined by a subclass is called a subtype and a type defined by its superclass is called a supertype ü Circle is a subtype of Geometric. Object and Geometric. Object 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, but not vice versa ü every circle is a geometric object, but not every geometric object is a circle ü an instance of a subclass can be passed to a parameter of its superclass type ü An object of a subtype can be used wherever its supertype value is required. This feature is known as polymorphism.

Polymorphism and Dynamic Binding public class Polymorphism. Demo { public static void main(String[] args)

Polymorphism and Dynamic Binding public class Polymorphism. Demo { public static void main(String[] args) { m(new Graduate. Student()); 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. to. String()); known as polymorphism. } } class Graduate. Student extends Student { } When the method m(Object x) is executed, the argument class Student extends Person { x’s to. String method is invoked. x may be an instance of public String to. String() { return "Student"; Graduate. Student, Person, or Object. Classes } Graduate. Student, Person, and Object have their } own implementation of the to. String method. Which class Person extends Object { public String to. String() { implementation is used will be determined dynamically return "Person"; by the Java Virtual Machine at runtime. This capability is } known as dynamic binding. } Lecture 6 © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Generic Programming public class Polymorphism. Demo { public static void main(String[] args) { m(new

Generic Programming public class Polymorphism. Demo { public static void main(String[] args) { m(new Graduate. Student()); m(new Person()); m(new Object()); } public static void m(Object x) { System. out. println(x. to. String()); } } class Graduate. Student extends Student { } class Student extends Person { public String to. String() { return "Student"; } } class Person extends Object { public String to. String() { return "Person"; } } Polymorphism allows methods to be used generically for a wide range of object arguments. This is known as Generic Programming. Lecture 6 © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Casting Objects and instanceof operator ü Casting can be used to convert an object

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 implicit (upcasting) Object o = new Student(); ü Casting from super class to sub class needs to be explicit (downcasting) Student b = (Student)o Lecture 6 © LPU : : CSE 310 Programming in Java : : Sawal Tandon

Casting Objects and instanceof operator ü If the superclass object is not an instance

Casting Objects and instanceof operator ü If the superclass object is not an instance of the subclass, a runtime Class. Cast. Exception occurs ü instanceof operator is used to test whether an object is an instance of a class Object my. Object = new Circle(); if (my. Object instanceof Circle) { System. out. println("The circle diameter Circle)my. Object). get. Diameter()); . . . } ü 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 : : CSE 310 Programming in Java : : Sawal Tandon

Casting Objects and instanceof operator ü Object member access operator (. ) precedes the

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). get. Area()); Lecture 6 © LPU : : CSE 310 Programming in Java : : Sawal Tandon