Object Oriented Programming OOP Concepts Inheritance in Java


Video: ClickMe

Inheritance - "is a" relationship
        - Single Inheritance
        - Multiple (level) Inheritance


Inheritance code example
Output:
//ClsCalculator objClsCalculator = new ClsCalculator();
Base class output start 
1 + 1 = 2
100.1 + 100.1 = 200.2
Base class output end 
//ClsCalculator objClsAdvCalculator = new ClsAdvCalculator();
Sub class output Start 
1 + 1 = 2
100.1 + 100.1 = 200.2
Sub class output end 

//ClsAdvCalculator objClsAdvCalculator1 = new ClsAdvCalculator();
Base + Sub class output Start 
1 + 1 = 2
100.1 + 100.1 = 200.2
Sub class output Vinod  +  kumar = Vinod  kumar
Base + Sub class output end 



ClsCalculator.java
/**
* */ package inheritance.tutorialbyexample.com.copy; /** * @author Vinod Kumar * */ public class ClsCalculator { /** * Default constructor of ClsCalculator */ public ClsCalculator() { // TODO Auto-generated constructor stub } /** * Addition of two number and print the value in console and return the * number as output. * * @param parameter1 * @param parameter2 * @return */ public int addition(int parameter1, int parameter2) { // addResult the local variable for storing the add result and used in // print // method and return. int addResult = parameter1 + parameter2; // Private method for internal use not outside of world print(parameter1, parameter2, addResult); return addResult; } public float addition(float parameter1, float parameter2) { // addResult the local variable for storing the add result and used in // print // method and return. float addResult = parameter1 + parameter2; // Private method for internal use not outside of world print(parameter1, parameter2, addResult); return addResult; } /** * Private method for printing the addition of input and output * * @param arg1 * @param arg2 * @param argAdd */ private void print(int arg1, int arg2, int argAdd) { System.out.println("" + arg1 + " + " + arg2 + " = " + argAdd); } /** * Private method for printing the addition of input and output * * @param arg1 * @param arg2 * @param argAdd */ private void print(float arg1, float arg2, float argAdd) { System.out.println("" + arg1 + " + " + arg2 + " = " + argAdd); } }
ClsAdvCalculator.java
/** * */ package inheritance.tutorialbyexample.com.copy; /** * @author Vinod Kumar * */ public class ClsAdvCalculator extends ClsCalculator { /** * Default constructor of ClsCalculator */ public ClsAdvCalculator() { // super(); // TODO Auto-generated constructor stub } /** * Addition of two number and print the value in console and return the * number as output. * * @param parameter1 * @param parameter2 * @return */ public String addition(String parameter1, String parameter2) { // addResult the local variable for storing the add result and used in // print // method and return. String addResult = parameter1 + parameter2; // Private method for internal use not outside of world print(parameter1, parameter2, addResult); return addResult; } /** * Private method for printing the addition of input and output * * @param arg1 * @param arg2 * @param argAdd */ private void print(String arg1, String arg2, String argAdd) { System.out.println("Sub class output " + arg1 + " + " 
+ arg2 + " = " + argAdd); } }
MainAppOfCalculator.java
/**
* */ package inheritance.tutorialbyexample.com.copy; /** * @author Vinod Kumar * */ public class MainAppOfCalculator { /** * Default Constructor of MainAppOfCalculator */ public MainAppOfCalculator() { // TODO Auto-generated constructor stub } /** * Main method for creation the instance of {@link ClsCalculator} * ClsCalculator instance as object * * @param a */ public static void main(String a[]) { System.out.println("Base class output start "); // Creating the instance of ClsCalculator() class. ClsCalculator objClsCalculator = new ClsCalculator(); // Calling the addition method with two int parameter objClsCalculator.addition(1, 1); // This is from base class // ClsCalculator // Calling the addition method with two float parameter objClsCalculator.addition(100.1f, 100.1f); // This is from base class // ClsCalculator System.out.println("Base class output end "); System.out.println(""); System.out.println("Sub class output Start "); ClsCalculator objClsAdvCalculator = new ClsAdvCalculator(); objClsAdvCalculator.addition(1, 1); // This is from sub class // ClsCalculator objClsAdvCalculator.addition(100.1f, 100.1f); // This is from sub class // ClsCalculator System.out.println("Sub class output end "); System.out.println(""); System.out.println("Base + Sub class output Start "); ClsAdvCalculator objClsAdvCalculator1 = new ClsAdvCalculator(); objClsAdvCalculator1.addition(1, 1); // This is from sub class // ClsCalculator objClsAdvCalculator1.addition(100.1f, 100.1f); // This is from sub class // ClsCalculator objClsAdvCalculator1.addition("Vinod ", " kumar"); System.out.println("Base + Sub class output end "); }
}

No comments:

Post a Comment

Please provide your input, it will honer for me...