Method Overriding

If the subclass have the method with same name and signature as in super class then it is called as subclass override method from super class.

// example for Method overriding.
class A 
{
 int i, j;
 A(int a, int b) 
 {
 i = a;
 j = b;
 }
 // display i and j
 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() 
 {
 super.show(); // this calls A's show() by using  super if it is not used then it will display simply k value
 System.out.println("k: " + k);
 }
  // overload show()
 void show(String msg) 
 {
 System.out.println(msg + k);
 }
}

public class Override
{
 public static void main(String args[]) 
 {
 B subOb = new B(1, 2, 3);
 subOb.show("This is k: "); // this calls show() in B as overloading
 subOb.show(); // this calls show() in A 
 }
}

Output:

This is k: 3 -- ----- This is the example for overloading the method show

i and j: 1 2 ------- This is for using super to call super class method show

k: 3 ----------- This is the sub class method show.

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 which provides run-time polymorphism.

There will be some situations where super class object will refer to subclass object where it overridden the method of super class to solve this java uses this mechanism to find which method should have to executes before compiling the program.

// example for Dynamic Method Dispatch
class A 
{
 void callme() 
 {
 System.out.println("Inside A's callme method");
 }
}
class B extends A 
{
 // override callme()
 void callme() 
 {
 System.out.println("Inside B's callme method");
 }
}
class C extends A
{
 // override callme()
 void callme() 
 {
 System.out.println("Inside C's callme method");
 }
}

public class Dispatch 
{
 public static void main(String args[])
 {
 A a = new A(); // object of type A
 B b = new B(); // object of type B
 C c = new C(); // object of type C

   A r; // obtain a reference of type A
 r = c; // 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 = a; // r refers to a C object
 r.callme(); // calls C's version of callme
 }
}

Output:

Inside C's callme method
Inside B's callme method
Inside A's callme method

Need of Overridden Methods

It provides like one interface, multiple methods one we define in super class which can be derives in all other sub classes.

By combining inheritance with overridden methods, a supe rclass can define the general form of the methods that will be used by all of its sub classes.

provides reuse and robustness by dynamic run time polymorphism.

Using Method Overriding

// Using run-time polymorphism.
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);
 }
 // override area for rectangle
 double area() 
 {
 System.out.println("Inside Area for Rectangle.");
 return dim1 * dim2;
 }
}

class Triangle extends Figure 
{
 Triangle(double a, double b) 
 {
 super(a, b);
 }

  // override area for right triangle
 double area() 
 {
 System.out.println("Inside Area for Triangle.");
 return dim1 * dim2 / 2;
 }
}

public class FindAreas 
{
 public static void main(String args[]) 
 {
 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());
 }
}

Output:

Inside Area for Rectangle.
Area is 45.0
Inside Area for Triangle.
Area is 40.0
Area for Figure is undefined.
Area is 0.0

Abstract Classes

There will be some situations where we have to give just the general form of the method which will implements by the sub classes that's why we can call it assub classes responsibility that must override them it cannot simply use the version defined in the super class.

The general form is::

abstract type name(parameter-list);

No definition is there. when we use abstract methods in class that class should be declared as abstract where we can't declare objects to that class because there is no complete definition of class and it have to be implemented by sub classes or it will extends like abstract class.

//example for abstract method
abstract class Figure 
{
 double dim1;
 double dim2;
 Figure(double a, double b) 
 {
 dim1 = a;
 dim2 = b;
 }
 abstract double area();

}

class Rectangle extends Figure
{
 Rectangle(double a, double b)
 {
 super(a, b);
 }
 // override area for rectangle
 double area()
 {
  System.out.println("Inside Area for Rectangle");
   return dim1*dim2;
 }

}

class Triangle extends Figure 
{
 Triangle(double a, double b) 
 {
 super(a, b);
 }

  // override area for right triangle
 double area() 
 {
 System.out.println("Inside Area for Triangle.");
 return dim1 * dim2 / 2;
 }
}

public class FindAreas 
{
 public static void main(String args[]) 
 {

 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());

 }
}

Output:

Inside Area for Rectangle
Area is 45.0
Inside Area for Triangle.
Area is 40.0

results matching ""

    No results matching ""