class Test {
static int s = 4;
final int d = 5;
static void sit() {
System.out.println(" sit method of test class ");
}
void get() {
System.out.println(" get method test class");
}
}
class Test1 extends Test {
static int s = 6;
static int a = 2;
final int d = 3;
static void sit() {
System.out.println("sit method test1 class");
}
void get() {
System.out.println("get method get test1 class");
}
}
class Test2 extends Test1 {
static int s = 7;
static void sit() {
System.out.println("sit method test2 class");
}
void get() {
System.out.println("get method get test2 class");
}
}
class Demo {
}
public class HelloWorld {
public static void main(String[] args) {
System.out.println("1 : test2 class instance of test2 class.......... ");
System.out.println();
Test2 obj2 = new Test2();
//class c inherit static
System.out.println("static varialble : " + obj2.a); // 2
System.out.println();
System.out.println("final varibale : " + obj2.d); //3
System.out.println();
obj2.sit();//print method of test2 class
System.out.println();
System.out.println();
System.out.println("2 : test1 class instance of test2 class.......... ");
System.out.println();
Test1 obj1 = new Test2();
obj1.sit();// print method of class test1 : static method
System.out.println();
obj1.get();//print method of class test2 :non static method
System.out.println();
//testing the type cast
Test objtrail1 = new Test1();//correct
Test objtrail2 = new Test2();//correct
// Test1 cannot be converted to Test2
// Test2 objtrail3=new Test1();//incorrect type cast error
// incompatible types: Test cannot be converted to Test2
// Test2 objtrail3=new Test();//incorrect type cast error
// Test1 obje=new Test();//error: incompatible types: Test cannot be converted to Test1
System.out.println();
System.out.println();
System.out.println("3 : test class instance of test1 class.......... ");
Test obje = new Test1();//the instance of test1 but print the static and final variable and static method of test
System.out.println();
System.out.println("print the static variable of test class " + obje.s);//4
System.out.println();
System.out.println("print the final variable of test class " + obje.d);//5
System.out.println();
obje.sit();//print the method of static test class
System.out.println();
obje.get();//print method of non static test1 class
System.out.println();
// System.out.println("3 : test2 class instance of test class.......... ");
// Test2 objet=new Test();
// objet.sit();//error: incompatible types: Test cannot be converted to Test2
// Test2 objet=new Test1();//error: incompatible types: Test1 cannot be converted to Test2
// Test s=new Demo();//error: incompatible types: Demo cannot be converted to Test
}
}
output :
1 : test2 class instance of test2 class..........
static varialble : 2
final varibale : 3
sit method test2 class
2 : test1 class instance of test2 class..........
sit method test1 class
get method get test2 class
3 : test class instance of test1 class..........
print the static variable of test class 4
print the final variable of test class 5
sit method of test class
get method get test1 class
No comments:
Post a Comment