Java Inheritance
Inheritance bulletin points
- Creating new classes from already existing classes is called Inheritance.
- Advantage is newly deriving classes will get the all public features from existing classes.
- Newly deriving classes are called sub/child class.
- Existing classes are called as super/parent class.
- java.lang.Object class is the super class for every predefined and user defined classes.
- Without inheritance we can write even single Hello World program also.
- private data of super class can not be inherit to sub class.
- extends keyword is used to inherit the variables and methods from Parent class to child class.
- One class can extends only one class at a time, because Java does not support multiple inheritance
Syntax
class Parent
{
Assuming that Parent class holding 10 public features;
}
class Child extends Parent
{
Assuming that Child class holding 5 public features;
So finally Child class having 15 public features including Parent features;
}
- Assuming that classes A extends B, B extends C, C extends D, here creating object for D class is good, because D class is holding all the features of super classes.
- If we create an object to parent class then we can access only parent class data but not child class data.
- If we create an object to child class then we can get both parent and child classes data.
Thanks for your time.
Nireekshan