Use this code to practice polymorphism and inheritance with different modifiers for a better understanding how it works in Java.
/** This simple application will help you understand polymorphism
* @version 1.0.0
* @author Zoltan Szabo
*/
public class Polymorphism{
final static int INDEX=2;
/** application entry point to test inheritance of modifiers
* @param args as a String array for command argument input
*/
public static void main(String[] args) {
SuperClass1[] obj = new SuperClass1[3];
obj[0]=new SuperClass1();
obj[1]=new SubClass1();
obj[2]=new SubSubClass1();
//Direct access to field values
System.out.println("SuperClass1 public field "+obj[INDEX].x);
//System.out.println("SuperClass1 private field "+obj[INDEX].y);
System.out.println("SuperClass1 protected field "+obj[INDEX].z);
System.out.println("SuperClass1 default field "+obj[INDEX].k);
//Static fields from super class
System.out.println("SuperClass1 public static field "+SuperClass1.XX);
//System.out.println("SuperClass1 private static field "+SuperClass1.YY);
System.out.println("SuperClass1 protected static field "+SuperClass1.ZZ);
System.out.println("SuperClass1 default static field "+SuperClass1.KK);
//Methods from super class
System.out.println("SuperClass1 public method "+obj[INDEX].func1());
//System.out.println("SuperClass1 private method "+obj[INDEX].func2());
System.out.println("SuperClass1 protected method "+obj[INDEX].func3());
System.out.println("SuperClass1 default method "+obj[INDEX].func4());
}
}
/** Class that is used as the super class with different modifiers to help understand inheritance
* @version 1.0.0.0
* @author Zoltan Szabo
*/
public class SuperClass1{
public int x=50;
private int y=60;
protected int z=70;
int k=80;
public static int XX=150;
private static int YY=160;
protected static int ZZ=170;
static int KK=180;
/** Super class no-arg constuctor*/
public SuperClass1(){
System.out.println("This is the superclass constructor.");
}
/** Public method in the super class
* @return x public field from super class
*/
public int func1(){
return x;
}
/** Private method in the super class
* @return y private field from super class
*/ private int func2(){
return y;
}
/** Protected method in the super class
* @return z protected field from super class
*/ protected int func3(){
return z;
}
/** Public method in the super class
* @return k default field from super class
*/ int func4(){
return k;
}
}
/**
*Class that is a child of the SuperClass1 inherits public, protected, and default attributes and methods
*@version 1.0.0
*@author Zoltan Szabo
*/
public class SubClass1 extends SuperClass1{
/** SubClass1class no-arg constuctor*/
public SubClass1(){
System.out.println("This is the subclass constructor.");
}
}
//if you want to implement an interface, use this class
/**
*Class that is a child of the SubClass1 inherits public, protected, and default attributes and methods
*@version 1.0.0
*@author Zoltan Szabo
*/ public class SubSubClass1 extends SubClass1{
/** SubSubClass1 class no-arg constuctor*/
public SubSubClass1(){
System.out.println("This is the subsubclass constructor.");
}
}
//*************** You can add a simple interface as well to practice implementations
/**
* Simple interface using methods to welcome and hank users
* @author Zoltan Szabo
* @version 1.0.0
*/
public interface AddDetails
{
/**
* Welcome the user to your application
* @param y has no significance, only used to practice input parameters
*@return an integer value to practice return data
*/
public int welcomeMessage(int y);
/**
* Thank the user for using your application
* @param y has no significance, only used to practice input parameters
*@return an integer value to practice return data
*/ public int thankYouMessage(int y);
}
No comments:
Post a Comment