INHERITANCE
Method Overriding...
• In a class hierarchy, when a method in a subclass has
the same name and type signature as a method in its
superclass, then the method in the subclass is said to
override the method in the superclass.
• When an overridden method is called from within a
subclass, it will always refer to the version of that
method defined by the subclass. The version of the
method defined by the superclass will be hidden.
• If you wish to access the superclass version of an
overridden function, you can do so by using super.
Real example of Java Method Overriding
• Consider a scenario, Bank is a class that provides
functionality to get rate of interest. But, rate of
interest varies according to banks. For example,
SBI, ICICI and AXIS banks could provide 8%, 7% and
9% rate of interest.
Method Overriding...
class A {
int i, j;
A(int a, int b) { i = a; j = b; }
void show() { System.out.println("i and j: " + i + " " + j); } }
class B extends A {
int k;
B(int a, int b, int c) { super(a, b); k = c; }
void show() { System.out.println("k: " + k); } }
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); }}
O/P: k: 3 // For accessing superclass show(), add super.show() also
// inside the definition of show() in class B.
void show() { super.show(); System.out.println("k: " + k); } }
Can we override static methods in java
• NO. Its not possible to override static methods because static means
class level so static methods not involve in inheritance.
class SuperClassDemo
{
public static void staticMethod()
{
System.out.println("SuperClassDemo staticMethod called");
}
}
class SubClassDemo extends SuperClassDemo
{
public static void staticMethod()
{
System.out.println("SubClassDemo staticMethod called");
}
public static void main(String []args){
SuperClassDemo superObj= new SuperClassDemo();
SuperClassDemo superobj1= new SubClassDem();
SubClassDemo subObj= new SubClassDem();
// Here is no need to create object to call a static method.
superObj.staticMethod();
superObj1.staticMethod();
subObj.staticMethod();
}
}
Can we change accessibility modifier in subclass overridden method?
Yes we can change accessibility modifier in subclass
overridden method but should increase the accessibility if we
decrease compiler will throw an error message.
Dynamic Method Dispatch...
• Dynamic method dispatch is the mechanism by
which a call to an overridden method is resolved
at run time, rather than compile time.
• Dynamic method dispatch is important because
this is how Java implements run-time
polymorphism.
• Method overriding actually gives power to
Dynamic method dispatching by providing new
definition for overridden functions in each
subclass. This is polymorphism ‘one interface
provided by superclass and multiple methods’.
Dynamic Method Dispatch...
Ex. class A {
void callme() { System.out.println("Inside A's callme method"); } }
class B extends A {
void callme() { System.out.println("Inside B's callme method"); } }
class C extends A {
void callme() { System.out.println("Inside C's callme method"); } }
class Dispatch {
public static void main(String args[]) {
A a = new A(); B b = new B(); C c = new C();
A r; r = a; // r refers to an A object
r.callme(); // calls A's version of callme
r = b; // r refers to a B object
r.callme(); // calls B's version of callme
r = c; // r refers to a C object
r.callme(); } } // calls C's version of callme
Method Overriding...
Ex. class Figure {
double dim1; double dim2;
Figure(double a, double b) { dim1 = a; dim2 = b; }
double area() {
System.out.println("Area for Figure is undefined."); return 0;
} }
class Rectangle extends Figure {
Rectangle(double a, double b) { super(a, b); }
double area() { System.out.println("Inside Area for Rectangle.");
return dim1 * dim2; }}
class Triangle extends Figure {
Triangle(double a, double b) { super(a, b); }
double area() { System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2; } }
class FindAreas {
public static void main(String args[]) {
Method Overriding...
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref;
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
figref = f;
System.out.println("Area is " + figref.area()); } }