in Core Java

Inheritance in Object oriented programming

Inheritance in OOPs

Inheritance is a very important feature in object oriented programming.

Inheritance is extremely useful in enabling the re-usability of the code and is enabled by using the extends keyword.

Inheritance and its advantages

Just like a child inherits some behaviors from their parents, Objects in the programs also get some methods and properties from their parent classes.

Inheritance helps  to access the basic functionality already declared in the parent types and gives an opportunity to declare new functionality in the sub types.

This gives the child an opportunity to declare some new methods and properties in it and still be able to have access to its super classes methods and properties through its instances.

Please note that there is no multiple inheritance in Java. A class can extend only one class. One has to consider using interfaces to add more functionality.

Let me explain Inheritance with an example.

Consider we have a type called vehicle. Its has a basic behavior move. Next we are creating a type called Car. It has more behaviors like move forward and backward. Next i am creating a type called Ford. This has more advanced behavior to move faster.

Let us create the classes for these. Refer to my article on Identifying object in requirement and classes and types for more information on creating types.

Inheritance in Java

To create a new type we need to create classes. So let me create the class Vehicle.

 

Now as per the inheritance rule, when i create an instance of Ford type, it should be able to access the methods of Car and Vehicle using the Fords Instance. 

In the same fashion, when i create an instance of Car type, i will be able to access the cars and vehicles methods but not its child Ford. 

So, Please note that in inheritance, a child instance has access to all the members of its super classes but a parent will not have any idea what types are actually extending it.

Output:

Inheritance in oops example

Using Super keyword:

Super keyword is used access the super class methods and properties in the sub class methods. This keyword helps to access the super class members without creating any instances.  Super keyword can only be used in a class with extends another class.

Super keyword can also be used to invoke the super class constructors. Super(Parameters) should be used in the first line of the constructor.

Note: The accessibility of the properties and methods of a type are subject to the access modifiers applied to the respective members. The methods and properties of super class are visible to the sub classes only when the access levels are set to public, default, protected. More about these in Java access modifiers.

 

Download this inheritance example here.

Next: Encapsulation in Object Oriented Programming-java

Previous:Inheritance in OOps

Srinivas Reddy

Srinivas Reddy

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...

More Posts - Website

Follow Me:
TwitterFacebook

Related Posts

Srinivas Reddy
Written By:

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...