Inheritance


Hare class Dog and Cat have some common properties.

So now we create a new class Animal and put the common properties in this class and use them back inter class Dog and Cat.

To use the properties of class Animal in class Dog and Cat we need to declare a word extends Animal as shown below.

Now the class Animal is called as parent class or super class or base class.
And the class Dog and Cat are called as sub class or child class.
Example

we can also declare the properties of parent class in the child class again but the first priority will be given to the properties present in the child class when we call them.

If we want to use the methods of super class(parent class) in the child class then we need to use the syntax 
super.methodName()

In the above example we use super.eat() to execute the method of parent class in child class method.

Post a Comment

0 Comments