/**
* Write a program to implement Method overloading and overriding.
*
*
*/
public class MethodOverriding {
/**
* execution begin from here
*
* @param args
* commandline arguments
*/
public static void main(String[] args) {
methodOverloadingAndOverridingDemo();
}
/**
* use to demo. method overloading and method overriding
*
*/
public static void methodOverloadingAndOverridingDemo() {
// Method Overloading demo
System.out.println("Method Overloading Demo :: new Child(\"child\")");
Child chObj1 = new Child("child");
chObj1.displayAll();
System.out
.println("\nMethod Overloading Demo :: new Child(\"parent\",\"child\")");
Child chObj2 = new Child("parent", "child");
chObj2.displayAll();
// Method Overriding demo
System.out.println("\nMethod Overridining Demo ::");
System.out.println("\tchObj1.display() ::");
chObj1.display();
System.out.println("\n\tparObj.display() ::");
Parent parObj = chObj1;
parObj.display();
System.out.println("\n\tparObj.display() ::");
parObj = new Parent("parent");
parObj.display();
}
}
/**
* parent class with strParent string object
*
* @author Ritesh
*
*/
class Parent {
String strParent;
/**
* constructor to initialize member variable
*
* @param strParent
* input
*/
Parent(String strParent) {
this.strParent = strParent;
}
/**
* displays strParent value
*
*/
public void display() {
System.out.println("\t\tstrParent : " + strParent);
}
}
/**
* Class Child extends parent class and also does method overriding and method
* overloading
*
* @author Ritesh
*
*/
class Child extends Parent {
String strChild;
/**
* constructor with one argument
*
* @param strChild
* input
*/
Child(String strChild) {
super("DefaultValue");
this.strChild = strChild;
}
/**
* constructor with two arugments
*
* @param strParent
* input
* @param strChild
* input
*/
Child(String strParent, String strChild) {
super(strParent);
this.strChild = strChild;
}
/**
* displays values of chiild members
*/
public void display() {
System.out.println("\t\tstrChild : " + strChild);
}
/**
* display all the variables of child and parent class
*
*/
public void displayAll() {
System.out.println("\tstrChild : " + strChild);
System.out.println("\tstrParent: " + strParent);
}
}
No comments:
Post a Comment