/**
* Write a Parent and child class. Parent class has one private variable called
* x. Please print value of x from child class.
*
*
*/
public class Modifier {
/**
* execution begin from here
*
* @param args
* commandline arguments
*/
public static void main(String[] args) {
// create child class object and display X
Child chObj = new Child(6);
chObj.displayX();
}
}
/**
* Parent class which will be inherited by Child to display x
*
* @author Ritesh
*
*/
class Parent {
// x that will be fetched
private int x;
/**
* Constructor
*
* @param x
* initialization parameters
*/
Parent(int x) {
setX(x);
}
/**
* get value of x
*
* @return x
*/
public int getX() {
return x;
}
/**
* sets value of x
*
* @param x
* argument for x
*/
public void setX(int x) {
this.x = x;
}
}
/**
* Inherits Parent class members and display value of x
*
* @author Ritesh
*
*/
class Child extends Parent {
/**
* child class constructor
*
* @param x
* initialization parameter
*/
Child(int x) {
super(x);
}
/**
* displays value of x
*
*/
public void displayX() {
System.out.println("Parent.x = " + getX());
}
}
No comments:
Post a Comment