Doing Inheritance
- The meaning of inheritance is to take characteristic of one class into another class.
- The mechanism of deriving new class from old class is known as inheritance.
- The old class is known as base class or super class or parent class.
- The new class is known as sub class, derived class or child class.
- Java allows different types of inheritance which are as follow:
1)Single inheritance
- It has only one super class and only one sub class.
2)Multilevel inheritance
- one derived class can be derived from another class.
3)Hierarchical inheritance
- It has one super class and more than one subclass.
Syntax:
class subclass-name extends superclassname
{
variables declaration;
method declaration;
}
The keyword extends signifies that the properties of the superclass name are
extended to the subclass name.
Example
Class A
{
……
……
}
Class B extends A
{
……
……
}
- Here, class B extend the properties of class A.
- That means all properties of class A can go in the class B.
- We can use the object of class B for use the properties of class A.
No comments:
Post a Comment