17.Method Overriding
Saturday, February 20, 2010
Hay, folks in this section we are going to learn “super” keyword and the “Overriding” concepts. So let’s get started!!!
super keyword
We have seen method overloading in the previous posts, as we know that it is the concept of polymorphisam, more precisely we can say that, we can write identical methods with identical method signatures in a same class. At the same way a subclass can also explicitly call a constructor of its immediate superclass. This is done by using the super constructor call. A super constructor call in the constructor of a
subclass will result in the execution of relevant constructor from the superclass, based onthe arguments passed.
For example, we show an example of a super constructor call. Given the following code for Employee.
From the above example we can observe that the code calls the default constructor of its immediate superclass and executes it.
If we compile the above program we can see the fallowing output.
Worker Information
Name :Sudarsn
Sex :M
Category : B
Dress allowance is paid
Officer Information
Name :Raghu
Sex : M
Class :I
Experiance :15
Vehical is provided
There are a few things to remember when using the super constructor call:
1. The super() call MUST OCCUR THE FIRST STATEMENT IN A CONSTRUCTOR.
2. The super() call can only be used in a constructor definition.
3. This implies that the this() construct and the super() calls CANNOT BOTH OCCUR IN
THE SAME CONSTRUCTOR.
Another use of super is to refer to members of the superclass (just like the this keyword).
The usage has the fallowing general form
super.member
For example,
The second form of super acts somewhat like this keyword, except that it always refers to the super class of the subclass in which it is used.
If we compile the above program we can get the fallowing output.
I in Super Class : 10
I in Sub Class : 20
Overriding Methods
If for some reason a derived class needs to have a different implementation of a certain
method from that of the superclass, overriding methods could prove to be very useful.This is also called Run-Time Ploymorphisam or Dynamic Method Dispatch.
A subclass can override a method defined in its superclass by providing a new
implementation for that method.Suppose we have the following implementation for the display() method in the super class A.
When we compile the above code we will get the fallowing result
Inside A's display method
Inside B's dispaly method
That’s all for this time! In the next post we will discuss more about further topics in java.
super keyword
We have seen method overloading in the previous posts, as we know that it is the concept of polymorphisam, more precisely we can say that, we can write identical methods with identical method signatures in a same class. At the same way a subclass can also explicitly call a constructor of its immediate superclass. This is done by using the super constructor call. A super constructor call in the constructor of a
subclass will result in the execution of relevant constructor from the superclass, based onthe arguments passed.
For example, we show an example of a super constructor call. Given the following code for Employee.
//Demonstration of Super Key word /** * * @author Administrator */ // super class Employee class Employee { String name; char sex; Employee(String n,char s) { name=n; sex=s; } public String getName() { return name; } public char getSex() { return sex; } } // sub class Worker class Worker extends Employee { char cat; boolean allow; Worker(String n,char s,char c,boolean d) { super(n,s); // calling super class Employee Constructor cat=c; allow=d; } public char getCat() { return cat; } public boolean getAll() { return allow; } } // subclass Officer class Officer extends Employee { char empclass; int exp; boolean veh; Officer(String n,char s,char c,int e,boolean v) { super(n,s); // Calling super class Worker Constructor empclass=c; exp=e; veh=v; } public char getEmpClass() { return empclass; } public int getExp() { return exp; } public boolean getVehicale() { return veh; } } // Main Class public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here //Constructing and calling Worker Object and its methods Worker w=new Worker("Sudarsn",'M','B',true); System.out.println("Worker Information"); System.out.println("Name :"+w.getName()); System.out.println("Sex :"+w.getSex()); System.out.println("Category : "+w.getCat()); if(w.getAll()) { System.out.println("Dress allowance is paid"); } else { System.out.println("Dress allowance is not paid"); } // Constructing Officer Object and calling its methods Officer o=new Officer("Raghu",'M','I',15,true); System.out.println("\nOfficer Information"); System.out.println("Name :"+o.getName()); System.out.println("Sex : "+o.getSex()); System.out.println("Class :"+o.getEmpClass()); System.out.println("Experiance :"+o.getExp()); if(o.getVehicale()) { System.out.println("Vehical is provided"); } else { System.out.println("Vechical is not provided"); } } }
From the above example we can observe that the code calls the default constructor of its immediate superclass and executes it.
If we compile the above program we can see the fallowing output.
Worker Information
Name :Sudarsn
Sex :M
Category : B
Dress allowance is paid
Officer Information
Name :Raghu
Sex : M
Class :I
Experiance :15
Vehical is provided
There are a few things to remember when using the super constructor call:
1. The super() call MUST OCCUR THE FIRST STATEMENT IN A CONSTRUCTOR.
2. The super() call can only be used in a constructor definition.
3. This implies that the this() construct and the super() calls CANNOT BOTH OCCUR IN
THE SAME CONSTRUCTOR.
Another use of super is to refer to members of the superclass (just like the this keyword).
The usage has the fallowing general form
super.member
For example,
//Demonstartion of name hiding /** * * @author Administrator */ class A { int i; } //Sub Class B class B extends A { int i; B(int a,int b) { super.i=a; // i in Class A i=b; // Refers i in Class B } void display() { System.out.println("I in Super Class : "+super.i); System.out.println("I in Sub Class : "+i); } } public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here B ob=new B(10,20); ob.display(); } }
The second form of super acts somewhat like this keyword, except that it always refers to the super class of the subclass in which it is used.
If we compile the above program we can get the fallowing output.
I in Super Class : 10
I in Sub Class : 20
Overriding Methods
If for some reason a derived class needs to have a different implementation of a certain
method from that of the superclass, overriding methods could prove to be very useful.This is also called Run-Time Ploymorphisam or Dynamic Method Dispatch.
A subclass can override a method defined in its superclass by providing a new
implementation for that method.Suppose we have the following implementation for the display() method in the super class A.
// Dynamic Method Dispatch /** * * @author Administrator */ class A { void display() { System.out.println("Inside A's display method"); } } class B extends A { void display() { System.out.println("Inside B's dispaly method"); } } public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here // Constructing Objects for A and B A a=new A(); B b=new B(); // Obtain a reference of type A A r; r=a; // Here r refers to an object of A r.display(); r=b; // r refers to an object of B r.display(); } }
When we compile the above code we will get the fallowing result
Inside A's display method
Inside B's dispaly method
That’s all for this time! In the next post we will discuss more about further topics in java.
Labels:
Inheritance
really nice blog.keep it uptodate
http://www.javabuzzu.blogspot.com
http://www.blog4mp3.blogspot.com