Monday, March 28, 2011

Inheritance in Java

Inheritance in Java

Inheritance means a child class can use the property of super/base class, 
results Re-usability of existing code:

Example 1 with output is given below:
//Base.java

package inheritance;

public class Base {
int baseVariable=10;
public void m1(){ System.out.println("Base class, m1 method..");}
public void m2(){System.out.println("Base class, m2 method..");}
public void m3(){System.out.println("Base class, m3 method..");}
}



//Child.java
package inheritance;

public class Child extends Base {
int childVariable=100;
public void m1(){ System.out.println("Child class, m1 method..");}
public void m2(){System.out.println("Child class, m2 method..");}
public void m4(){System.out.println("Child class, m4 method..");}

public static void main(String[] args) {
Base b1=new Base();
Child c1=new Child();
b1.m1();
b1.m2();
b1.m3();
System.out.println(b1.baseVariable);

c1.m1();
c1.m2();
c1.m3();
c1.m4();
System.out.println(c1.baseVariable);
System.out.println(c1.childVariable);

Base b2=new Child();
b2.m1();
b2.m2();
b2.m3();
//b2.m4();  //Illegal, the method m4() is undefined for type Base.
System.out.println(b2.baseVariable);
//System.out.println(b2.childVariable); //b2.childVariable can't be resolved or not a field.

}

}

//Run Child.java, output is mentioned here:
Base class, m1 method..
Base class, m2 method..
Base class, m3 method..
10
Child class, m1 method..
Child class, m2 method..
Base class, m3 method..
Child class, m4 method..
10
100
Child class, m1 method..
Child class, m2 method..
Base class, m3 method..
10



//Other important points regarding inheritance
1) static methods can't be overridden by non static methods, means:
//Base class
public static void m1(){..}

//Child class
public void m1(){..}  //Is wrong, program won't compile
-----------
But, see the below:

//Base class
public static void m1(){..}

//Child class
public static void m1(){..}  //Is right, but in that scenario static methods
//will be treated as class level and local for that class
//so following line generates changed output:
Base b2=new Child();
b2.m1();  //Base class, m1 method..

//Please note if you don't use static in both m1(), it prints 
//Child class, m1 method..




2) If your base class method throws any type of Exceptions (Compile time or run-time),
sub-class doesn't need to override method with same or Higher Exceptions. Means:

//Base class
public void m1() throws Exception { .. }

//Child class
public void m1(){ .. }  //Its right no Exception or problem here. Compiles successfully.

//However you have to handle this while using these two caller objects:

Base b1=new Base();
b1.m1();  //Must handle the Exception, else comment this line

Base b2=new Child();
b2.m1();  //Must handle the Exception, else comment this line




3) Now take case of reverse way:
Sub-class writes an overridden method with throws clause, but in super class
throws is not specified. Compiler will not give exception if throws clause uses a RuntimeException,
but it will give exception, if throws uses a Compile time (Checked exception). Means:

//Base class, no throws clause
public void m1(){ .. }

//Child class
public void m1()throws NullPointerException{ .. }  //Works fine,  no compile error.
or
public void m1()throws RuntimeException{ .. }     //Works fine, no compile error.


But following definition of Child class method:
public void m1()throws IOException{ .. } //Compiler error
or
public void m1()throws Exception{ .. }   //Compiler error




4) If Sub-class method uses throw (not throws) at the end of method definition, 
that throw exception must be handled by the same method. Means if throw uses RuntimeException,
no "throws RuntimeException" is required at the method declaration section, but
if throw clause uses Checked-Exception like IOException then "throws IOException" or
"throws Exception" is required at the method declaration section.
And hence the point-2 and point-3 of this blog is again applied (just now you have read above point).

Means if your base class method declaration uses "throws Exception" or "throws RuntimeException",
your Sub-class method don't need to override the same thing, but if sub-class uses throws clause,
then Base class must define that (if "throws" uses Checked-Exception else no declaration is required at Base class method).
class level.



5) What if you declare static in Sub-class overriding method but not in Base class method.
//Base class
public void m1(){..}

//child class
public static void m1() {..} //Will not compile, Reason: The static method can't hide 
//instance method from Base class

---------------------------END----------------------------

No comments:

Post a Comment