Object Oriented Programming OOP Concepts Plymorphism in Java

Video: ClickMe


Plymorphism
- Overloading - Static Binding
 - Overriding - Dynamic Binding


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 
Sub class output 1 + 1 = 2
Sub class output  100.1 + 100.1 = 200.2
Sub class output end 



ClsCalculator.java

/**
*
*/
package plymorphism.tutorialbyexample.com;

/**
* @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 plymorphism.tutorialbyexample.com;

/**
* @author Vinod Kumar
*
*/
public class ClsAdvCalculator extends ClsCalculator {

/**
* Default constructor of ClsCalculator
*/
public ClsAdvCalculator() {
// 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;

}

/**
* Addition of two number and print the value in console and return the
* number as output.
*
* @param parameter1
* @param parameter2
* @return
*/
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("Sub class output " + 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("Sub class output " + arg1 + " + " + arg2 + " = " + argAdd);
}

}


MainAppOfCalculator.java
/**
*
*/
package plymorphism.tutorialbyexample.com;

/**
* @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 ");

}
}

No comments:

Post a Comment