Saturday, July 27, 2013

SINGLE INHERITANCE USING SUPER KEYWORD

a.java:
~~~~~

public class a
{
int a=10;
String abc="LEARN JAVA";

void display()
{
System.out.println("Value of 'a' is "+a);
System.out.println("Value of 'abc' is "+abc);
}
}


b.java:
~~~~
public class b extends a
{
int j=a;
String cba=abc;        //assigning the value of variable abc to cba

void display()
{
super.display();             //accessing the members of the main class using super keyword
System.out.println("Value of 'j' is "+j);
System.out.println("Value of 'cba' is "+cba);
}
}

inheritmain.java:
~~~~~~~~~~~
public class inheritmain
{
public static void main(String[] args)
{
b ob= new b();        // creating tthe object reference to the last created class
ob.display();     //here we are calling the display() of b class
}

}



No comments:

Post a Comment

Animated Social Gadget - Blogger And Wordpress Tips