A.java /file name
~~~~~
public class A //parent class (or) superclass
{
int i,j;
void showij()
{
System.out.println("i and j : "+i+" "+j);
}
}
~~~~~
public class A //parent class (or) superclass
{
int i,j;
void showij()
{
System.out.println("i and j : "+i+" "+j);
}
}
B.java //filename
~~~~~
public class B extends A //child class
{
int k;
void showk()
{
System.out.println("K : "+k);
}
void sum()
{
System.out.println("i+j+k="+(i+j+k));
}
}
C.java:
~~~~~
public class simpleinheritance
{
public static void main(String args[])
{
A superob=new A(); //creating object for parent class
B subob=new B(); //object creation for child class
superob.i=10;
superob.j=20; // assigning vales to super class variables
System.out.println("Contents of superob :");
superob.showij();
System.out.println();
subob.i=7;
subob.j=8;
subob.k=9;
System.out.println("Contents of subob : ");
subob.showij(); // calling parent class function at child class
subob.showk();
System.out.println();
System.out.println("Sum of i,j and k in subob : ");
subob.sum();
}
}
No comments:
Post a Comment