method overriding 1.sub class can override the methods defined by the super class. 2.overridden...

24
METHOD OVERRIDING 1. Sub class can override the methods defined by the super class. 2. Overridden Methods in the sub classes should have same name, same signature , same return type and may have either the same or higher scope than super class method. 3. Java implements Run Time Polymorphism/ Dynamic Method Dispatch by Method Overriding. [Late Binding] 4. Call to Overridden Methods is Resolved at Run Time. 5. Call to a overridden method is not decided by the type of reference variable Rather by the type of the object where reference variable is pointing. 6. While Overriding a Method, the sub class should assign either same or higher access level than super class method.

Upload: quinton-buzzard

Post on 14-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

METHOD OVERRIDING

1. Sub class can override the methods defined by the super class.

2. Overridden Methods in the sub classes should have same name, same signature , same return type and may have either the same or higher scope than super class method.

3. Java implements Run Time Polymorphism/ Dynamic Method Dispatch by Method Overriding. [Late Binding]

4. Call to Overridden Methods is Resolved at Run Time.

5. Call to a overridden method is not decided by the type of reference variable Rather by the type of the object where reference variable is pointing.

6. While Overriding a Method, the sub class should assign either same or higher access level than super class method.

Page 2: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

EXAMPLE METHOD OVERRIDINGclass A{void show(){System.out.println("Hello This is show() in A");}// End of show() Method} // End of class Aclass B extends A{void show(){System.out.println("Hello This is show() in B");}// End of show() Method} // End of class B class override

{public static void main(String args[])

{// super class reference variable// can point to sub class object

A a1 = new A();a1.show();

a1 = new B();a1.show();

}}

B class overrides show() method from super class A

Call to show() of A

class

Call to show() of B

class

Page 3: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

class A{void show(int a){System.out.println("Hello This is show() in A");}}class B extends A{void show(){System.out.println("Hello This is show() in B");}}

class override1{public static void main(String args[]){/*A a1 = new B();a1.show(); */

A a1 = new A();a1.show(10);

B b1 = new B();b1.show(10);b1.show(); }

OUTPUTHello This is show() in AHello This is show() in AHello This is show() in B

Is this MethodOverriding

NO

Page 4: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

Dynamic Method Dispatch

1. Super class reference variable can refer to a sub class object.

2. Super class variable if refers to sub class object can call only overridden methods.

3. Call to an overridden method is decided by the type of object referred to.

A

B C D

A a1 = new B();a1.show(); // call to show() of Ba1 = new C();a1.show(); // call to show() of Ca1 = new D();a1.show(); // call to show() of D

Assume show() Method is Overridden by sub classes

Page 5: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

class A{void show(){System.out.println("Hello This is show() in A");}}class B extends A{void show(){System.out.println("Hello This is show() in B");}}

class C extends A{void show(){System.out.println("Hello This is show() in C");}}class D extends A{void show(){System.out.println("Hello This is show() in D");}}

DYNAMIC METHOD DISPATCH

CONTINUED…..

Page 6: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

class override2{public static void main(String args[]){A a1 = new A();a1.show();a1 = new B();a1.show();a1 = new C();a1.show();a1 = new D();a1.show();}}

Hello This is show() in A

Hello This is show() in B

Hello This is show() in C

Hello This is show() in D

Page 7: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

class override3{public static void main(String args[]){A a1 = new B();B b1 = (B) a1;

/*A a1 = new B();C c1 = (C) a1;

Exception in thread "main" java.lang.ClassCastException: Bat override3.main(override3.java:39)*/}}

Page 8: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

Examples Overriding

class A{void show() { …. }}class B extends A{void show() { …. }void show(int x) { … }void print() { … }}

A a1 = new B();a1.show() ; // Valid// a1.show(10); // Invalid //a1.print(); // Invalid

When a super class variable points to a sub class object, then it can only call overridden methods of the sub class.

Page 9: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

class A{protected void show(){System.out.println("Hi");}}class B extends A{void show(){System.out.println("Hi");}}

D:\Java1>javac AB.javaAB.java:10: show() in B cannot override show() in A; attempting to assign weaker access privileges; was protectedvoid show() ^1 error

Page 10: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

class A{private void show(){System.out.println("Hi");}}class B extends A{int show(){System.out.println("Hi");return 10;}}

NO

IS THIS METHOD OVERRIDING

CODE WILL COMPILE & RUN SUCESSFULLY

Page 11: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

class A{static int show(){System.out.println("class A");return 0;}}

class B extends A{void show(){System.out.println("class B");}}

What’s Wrong Here

sample.java:12: show() in B cannot override show() in A; overridden method is staticvoid show() ^1 error

Page 12: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

class A{static int show(){System.out.println("class A");return 0;}}

class B extends A{static int show(){System.out.println("class B");}}

What’s Wrong Here

Nothing

The code will compile and run successfully.

Method Hiding

B class Hides show() method from super class A

Page 13: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

class A{static int show() {System.out.println("SHOW METHOD OF CLASS A");return 0;}// End of show() Method}// End of class Aclass B extends A{static void show() {System.out.println("SHOW METHOD OF CLASS B");}// End of show() Method}// End of class B

Will code Compile Sucessfully

TestAB.java:11: show() in B cannot override show() in A; attempting to use incompatible return typefound : void required: int static void show()

Page 14: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

class A{static int show() {System.out.println("SHOW METHOD OF CLASS A");return 0;}// End of show() Method}// End of class A

class B extends A{static int show() {System.out.println("SHOW METHOD OF CLASS B");return 0;}// End of show() Method}// End of class B

class TestAB{public static void main(String args[]){A a1 = new B();System.out.println(a1.show());B b1 = new B();b1.show();}}

E:\Java Programs>java TestABSHOW METHOD OF CLASS A0SHOW METHOD OF CLASS B

Page 15: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

class A{private int a;private int b;private int c;A(){a=b=c=10;}A(int a, int b, int c){this.a = a;this.b = b;this.c = c;}public int getA() { return this.a;}public int getB() { return this.b;}public int getC() { return this.c;}public void show(){System.out.println("a="+a);System.out.println("b="+b);System.out.println("c="+c);}// End of show() Method}// End of class A

class B extends A{private int d;B(){super();d=10;}B(int a, int b, int c,int d){super(a,b,c);this.d = d;}public int getD() { return this.d;}public void show(){System.out.println("a="+super.getA());System.out.println("b="+super.getB());System.out.println("c="+super.getC());System.out.println("d="+this.d);}// End of show() Method}// End of class B

Page 16: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

class Test{public static void main(String args[]){A a1 = new B();System.out.println(a1.getA());System.out.println(a1.getD());a1.show();}// End of main() Method}// End of class Test

Test.java:56: cannot find symbolsymbol : method getD()location: class ASystem.out.println(a1.getD());

Page 17: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

Polymorphism

In the previous slide, the two variables are defined to have the same type at compile time: BankAccount

However, the types of objects they are referring to at runtime are different

What happens when the withdraw method is invoked on each object?anAccount refers to an instance of BankAccount. Therefore, the withdraw method defined in BankAccount is invoked.account1 refers to an instance of OverdraftAccount. Therefore, the withdraw method defined in OverdraftAccount is invoked.

Polymorphism is: The method being invoked on an object is determined AT RUNTIME and is based on the type of the object receiving the message.

Page 18: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

Final Methods and Final Classes

Methods can be qualified with the final modifierFinal methods cannot be overridden.This can be useful for security purposes.

public final boolean validatePassword(String username, String Password)

{[...]

Classes can be qualified with the final modifierThe class cannot be extendedThis can be used to improve performance. Because there an be no subclasses, there will be no polymorphic overhead at runtime.

public final class Color{[...]

Page 19: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

19

Inheritance

• To write a method’s definition of a subclass, specify a call to the public method of the superclass.– If subclass overrides public method of

superclass, specify call to public method of superclass:

super.MethodName(parameter list)– If subclass does not override public method of

superclass, specify call to public method of superclass:

MethodName(parameter list)

Page 20: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

20

Polymorphism• Java allows us to treat an object of a subclass as an

object of its superclass. In other words, a reference variable of a superclass type can point to an object of its subclass.

Person name, nameRef;

PartTimeEmployee employee, employeeRef;

name = new Person("John", "Blair");

employee = new PartTimeEmployee("Susan", "Johnson", 12.50, 45); nameRef = employee;System.out.println("nameRef: " + nameRef);

nameRef: Susan Johnson wages are: $562.5

Page 21: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

21

Polymorphism• Late binding or dynamic binding (run-time binding):

– Method to be executed is determined at execution time, not compile time.

• The term polymorphism means to assign multiple meanings to the same method name.

• In Java, polymorphism is implemented using late binding.

• The reference variable name or nameRef can point to any object of the class Person or the class PartTimeEmployee.

• These reference variables have many forms, that is, they are polymorphic reference variables. They can refer to objects of their own class or to objects of the classes inherited from their class.

Page 22: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

22

Polymorphism• You can declare a method of a class final using the keyword

final. For example, the following method is final.

public final void doSomeThing(){

//...}

• If a method of a class is declared final, it cannot be overridden with a new definition in a derived class.

• In a similar manner, you can also declare a class final using the keyword final.

• If a class is declared final, then no other class can be derived from this class.

• Java does not use late binding for methods that are private, marked final, or static.

Page 23: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

23

Polymorphism• You cannot automatically make reference variable of

subclass type point to object of its superclass.

• Suppose that supRef is a reference variable of a superclass type. Moreover, suppose that supRef points to an object of its subclass:

– You can use an appropriate cast operator on supRef and make a reference variable of the subclass point to the object.

– On the other hand, if supRef does not point to a subclass object and you use a cast operator on supRef to make a reference variable of the subclass point to the object, then Java will throw a ClassCastException—indicating that the class cast is not allowed.

Page 24: METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same

24

Abstract Classes

• A class that is declared with the reserved word abstract in its heading.

• An abstract class can contain instance variables, constructors, finalizers, and non-abstract methods.

• An abstract class can contain abstract methods.• If a class contains an abstract method, the class must

be declared abstract.• You cannot instantiate an object of an abstract class

type. You can only declare a reference variable of an abstract class type.

• You can instantiate an object of a subclass of an abstract class, but only if the subclass gives the definitions of all the abstract methods of the superclass.