Professional Documents
Culture Documents
Chap 06
Chap 06
Chap 06
Inheritance
Incheon Paik
class Ef { class Pq {
public static void main(String args[]) { public static void main(String args[]) {
F f = new F(); P p = new P();
f.x = "This is a string";
Here, if f.x = 30; ?
p.x = 55;
System.out.println("f.x = " + f.x);
System.out.println("p.x = " + p.x);
E e = new E();
Q q = new Q();
e.x = 45;
q.x = "This is a string";
System.out.println("e.x = " + e.x);
} System.out.println("q.x = " + q.x);
} }
}
Result :
f.x = This is a string Result :
p.x = 55
e.x = 45
q.x = This is a string
class SuperKeyword {
public static void main(String args[]) {
M200 m200 = new M200();
m200.display();
}
}
Result :
i = 200
super.i = 100
class A1 {
void hello() {
System.out.println("Hello from A1");
Result :
}
} Hello from C1
Overrided by hello of B1
class B1 extends A1 {
void hello() {
System.out.println("Hello from B1");
}
}
class B2 extends A2 {
void hello() { Overrided by hello of B2
System.out.println("Hello from B2");
}
}
class C2 extends B2 {
void hello() {
System.out.println("Hello from C2");
}
}
class MethodOverriding2 {
public static void main(String args[]) {
A2 obj = new C2();
obj.hello();
} Object of C2
}
class InheritanceAndConstructors2 {
public static void main(String args[]) {
U2 u2 = new U2(1, 2, 3);
class S2 {
System.out.println("u2.s2 = " + u2.s2);
int s2;
System.out.println("u2.t2 = " + u2.t2);
S2(int s2) {
System.out.println("u2.u2 = " + u2.u2);
this.s2 = s2;
}
}
}
}
Result :
class T2 extends S2 {
u2.s2 = 1
int t2;
u2.t2 = 2
T2(int s2, int t2) { u2.u2 = 3
super(s2);
this.t2 = t2;
}
}
class Person {
String name; What is the result of compilation when we removed
this comment?
int age;
private Singleton() {
}
class SingletonDemo {
public static void main(String args[]) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
if (s1 == s2)
System.out.println("Equal");
else
System.out.println("Not equal");
}
}