Thursday 28 June 2012

Java Training: - How do you implement inheritance in Java?

To understand this better lets do a small project to get the actual feel of the same.

Below is the project structure of the “Inheritance” project. It has the three classes:-

• “ClsAdd.java”
This class has a basic subroutine which adds two numbers.

• “ClsSpecialAdd.java”
This class inherits from “ClsAdd.java” and overrides the subroutine which does the basic addition. It changes the basic addition functionality a bit by adding the two numbers but also adds up an extra adjustment value “20”.

• “ClsRun.java”
This is the class which has the “public static void main (String [] args)” method. It’s the main class which will run the whole show by creating the objects of “ClsAdd” and “ClsSpecialAdd” and executing the “Add” method.

Figure: - Project File structure of inheritance structure

Figure: - Inheritance in Action


When you run the project you can see two outputs one from the parent class and second from the child class with the adjustment value added up. So the child class has made the parent add class more specialized.

Figure: - Output from Inheritance Project


See the following video on Inheritance between beans and Springin Java: -



Click to get Java Training

Regards,

Get more Java training stuffs from author's blog

No comments:

Post a Comment