Inheritance
The dictionary meaning of inheritance is the process of transmission of characteristics from parent to offspring.
In term of OOPs we can correlate the parents and offspring as type. so in OOPs inheritance means process of transmission of characteristics from one type to Other type where one is called as the supertype or base type and other type inheriting the base type called as the derived type or subtype.
Understanding inheritance is very important in java because it provides the foundation for understanding different hierarchies used JavaSE library like exception , thread and many more.
Coding in terms of inheritance provides re-usability which is very important paradigm of object-oriented methodology. Besides code re-usability it also plays an important role in achieving Polymorphism.
Inheritance helps to establish relationship between two types in java- these relationships are: IS-A relation and HAS-A relation.
With the rise of relationship inheritance give birth to two new concepts called as : Aggregation and composition.
To use the inheritance concept in our code we need to use the "extend" keyword with superclass name.
Example:
package in.technoscience.inheritance;
public class ParentClass {
}
package in.technoscience.inheritance;
public class ChildClass extends ParentClass {
}
For inheritance, these are things which we perform at class level.
Note: when you write any simple class in java without extending any other class then that simple class inherit java.lang.Object class. means since the first program we have written in java knowingly or unknowingly we are using the concept of inheritance. And this also states that Java.lang.Object is superclass of every class in java.
When we should adopt inheritance?
It all depends on the requirement, but before adopting to code level try to think for "is-a" relationship at class modelling level.
consider an example: if you are working on House management system then in that case house can be of different types it can be bungalow, Apartment type, independent house, hut. And house will build of bricks and cement. so in that case we can think as "is-a" relationship and can conclude that bungalow is a house, Apartment is a house, hut is a house. so in that scenario we can go for the inheritance concept. But when we think about dinning room , bathroom, kitchen then we can not say kitchen is a house or bathroom is a house. Here we can say house "has-a" kitchen, house "has-a" bathroom. here "has-a"will lead to composition.
What we can do after sub-classing a class?
Look for comments for explanation.
Look around comments for explanation.
Note: when you write any simple class in java without extending any other class then that simple class inherit java.lang.Object class. means since the first program we have written in java knowingly or unknowingly we are using the concept of inheritance. And this also states that Java.lang.Object is superclass of every class in java.
When we should adopt inheritance?
It all depends on the requirement, but before adopting to code level try to think for "is-a" relationship at class modelling level.
consider an example: if you are working on House management system then in that case house can be of different types it can be bungalow, Apartment type, independent house, hut. And house will build of bricks and cement. so in that case we can think as "is-a" relationship and can conclude that bungalow is a house, Apartment is a house, hut is a house. so in that scenario we can go for the inheritance concept. But when we think about dinning room , bathroom, kitchen then we can not say kitchen is a house or bathroom is a house. Here we can say house "has-a" kitchen, house "has-a" bathroom. here "has-a"will lead to composition.
What we can do after sub-classing a class?
- A subclass will always inherit the public and protected member of superclass.
- if superclass and subclass will be in same package then subclass can inherit the package-level members also.
- After inheriting the fields in subclass, inherited fields can be used directly , here subclass will pretend that inherited fields are local to that class.
- After extending any class we can declare any new fields in subclass which are not present in superclass.
- In inheritance we can also declare a field in subclass which can have same name as superclass field, this technique is called as hiding.
- In case of methods also we can use those directly considering as local method of that class.
- Due to inheritance we can write same instance method signature in subclass like as in superclass this is called as overriding.
- We can also write new methods in subclass which are not present in super class.
- In inheritance we can write static methods in sublclass which can have same signature as the one in superclass -- this also represents hiding. but remember we can not override any static methods.
- Constructors can not be inherited but from subclass we can call the constructor of superclass using super keyword.
- A subclass does not inherit th
e private
members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
Look for comments for explanation.
package in.technoscience.inheritance;
//implicitly it is subclass of java.lang.Object
public class ParentClass {
// instance methods with package level accessifier
int a = 100;
int b = 200;
// static variable
private static int noOfTimes;
public static int noOfVisit;
//private instance variable
private boolean indicator;
protected String name;
// constructor
public ParentClass() {
indicator = true;
name = "parent class";
noOfTimes++;
}
// instance methods
public void m1() {
System.out.println("i am in m1 method of parent class");
}
public void m2() {
System.out.println("i am in m2 method of parent class");
}
// static method accessing private variables
public static void writeCount() {
System.out.println("Number of times from super class: " + noOfTimes);
}
//normal static method
public static void display()
{
System.out.println("from parent class ");
}
//final method
public final void finalMethod(){
System.out.println("i am final method of super class");
}
}
Look around comments for explanation.
package in.technoscience.inheritance;
// subclass of Parentclass
public class ChildClass extends ParentClass {
// same variable name as present in superclass
int b = 30;
// new variable of sub class
int c = 40;
// overridden method
public void m2() {
System.out.println("i am in m2 method of child class");
}
// new method of subclass
public void m3() {
System.out.println("i am in m3 method of child class");
}
// method hiding.
public static void writeCount() {
System.out.println("Number of visit from subclass: " + noOfVisit);
}
// here compiler complains in term of accessibility and
// don't consider as new method of subclass
/*
* static void display() { System.out.println("from child class"); }
*/
// here we used public accessifier now compiler is happy and considering as
// new method of subclass
public static void display() {
System.out.println("i am from child class");
}
// final method can not be overridden but can be inherited
/*
* public final void finalMethod(){
* System.out.println("i am final method of sub class"); }
*/
}
look around comments if you don't understand then ping me on facebook.
package in.technoscience.inheritance;
// a class for testing the concept
public class TestingInheritance {
public static void main(String[] args) {
System.out.println("first case===>");
// creating an object of parent class
ParentClass pc=new ParentClass();
//accessing all members from parent class only, a normal scenario
System.out.println(pc.a); //output : 100
System.out.println(pc.b);//output : 200
System.out.println(pc.name); // output : parentclass
pc.m1(); // accessing m1 method of parentclass
pc.m2(); // accessing m2 method of parentclass.
pc.display(); // accessing static method of parentclass
pc.writeCount(); // a static method of parent class dealing with private field
pc.finalMethod(); // accessing final method of parent class.
System.out.println("second case===>");
// an object of child class
ChildClass cb =new ChildClass();
System.out.println(cb.a);//output : 100 ==> can be accessed due to inheritance
System.out.println(cb.b);//output : 30==> same name as parentclass but value changed==> explain hiding
System.out.println(cb.c);//output : 40 ==> new member of subclass only
//System.out.println(cb.noOfTimes); can not inherit private member so not accessible through subclass
System.out.println(cb.name); // o/p: parent class , protected members can be inherited so accessible
//System.out.println(cb.indicator); // again private member
cb.m1(); // accessible due to inheritance.inheritingly present in subclass
cb.m2(); // overridden method
cb.m3(); // new method of subclss
cb.display(); // static method, it will called as new method and it also explain hiding
cb.writeCount();// subclass hiding the static method of superclass.
cb.finalMethod();// final method being inherited but not overriden
System.out.println("third case =====>");
// another object of parent class
ParentClass pc1=new ParentClass();
System.out.println(pc1.a);//o/p ==> 100 accessing member of his own class(parentclass)
System.out.println(pc1.b);//o/p ==> 200 accessing member of his own class(parentclass)
/*parent class does not have any information that who is going to extend him, so parent class does not know
about members of subclass.*/
// System.out.println(pc1.c); // c is presnt in subclass not in parent class.
pc1.m1();// method present in his own class
pc1.m2();// method present in his own class
//pc1.m3(); its not present in his own class, so parent class does not have any info about subclass member.
// following code will explain static methods can not be overridden
System.out.println("special scenario for static methods");
ParentClass pc2= new ParentClass();
ChildClass cc= new ChildClass();
//runtime polymorphism
ParentClass pc3= new ChildClass();
pc2.writeCount(); //Number of times from super class: 6
cc.writeCount();//Number of visit from subclass:0
pc3.writeCount();//Number of times from super class: 6
//if static method could have been overridden then last line and second last line both would have been giving
//same output.its inherited but not overridden
}
}
you can find the code at : My Git
more to come : aggregation,composition and scenario based on typecasting, types of inheritance supported in java, why not multiple inheritance.
No comments:
Post a Comment