Program 2.16 Defining A Class Inside A Method
Program 2.16 Defining A Class Inside A Method
Program 2.16 Defining A Class Inside A Method
class MethodClass
{
public static void main(String args[])
{
class MyClass //A class inside main() method
{
int x;
MyClass(int x) //Constructor
{
this.x = x;
}
void display() //Method
{
System.out.print("Your value : ");
System.out.println(x);
}
}
class Super
{
int i, j;
void show()
{
System.out.print("i and j: ");
System.out.println( + i + " " + j);
}
}
void display()
{
System.out.println("k: " + k);
}
void sum()
{
System.out.println("i+j+k: " + (i+j+k));
}
}
class SimpleInheritance
{
public static void main(String args[])
{
Super a = new Super();
Sub b = new Sub();
a.i = 5;
a.j = 12;
System.out.println("Contents of super: ");
a.show();
System.out.println();
b.i = 11;
b.j = 13;
b.k = 17;
System.out.println("Contents of sub: ");
b.show();
b.display();
System.out.println();
class Super
{
int i, j;
void show()
{
System.out.print("i and j: ");
System.out.println( + i + " " + j);
}
}
void display()
{
System.out.println("k: " + k);
}
void sum()
{
System.out.println("i+j+k: " + (i+j+k));
}
}
class Reference
{
public static void main(String args[])
{
Super a; //Statement1
Sub b = new Sub();
b.i = 11;
b.j = 13;
b.k = 17;
System.out.println("Contents of sub: ");
b.show();
b.display();
System.out.println();
a = b; //Statement2
class Primary
{
int cal; //declaration1
void show()
{
System.out.println("Super class cal : "+cal);
}
}
class Secondary extends Primary
{
int cal; //declaration2
Secondary(int x,int y) //statement1
{
cal = x; //statement2
super.cal = y; //statement3
}
void display()
{
System.out.println("Sub class cal : "+cal);
}
}
class SuperUse1
{
public static void main(String args[])
{
Secondary s = new Secondary(15,22);
s.show();
s.display();
}
}
Program 2.20 First use of the ‘super’ keyword