C
C
Core Java
Search…
Problems - Inheritance (till super keyword)

Level 1:

Basic single level inheritance

1)

Java Inheritance I | HackerRank
HackerRank

2)

Java Inheritance II | HackerRank
HackerRank

3)

Largest Tech Community | Hackathons, Programming & Coding Challenges | TechGig

Multilevel inheritance

1)

Largest Tech Community | Hackathons, Programming & Coding Challenges | TechGig

Basic method overriding

1)

Java Method Overriding | HackerRank
HackerRank

2)

Java Method Overriding 2 (Super Keyword) | HackerRank
HackerRank

3)

Largest Tech Community | Hackathons, Programming & Coding Challenges | TechGig

Level 2:

1) Resolve the compilation error :

1
package com.gs.ilp.corejava.Constructor;
2
3
public class ParentWithCon {
4
String mem1;
5
String mem2;
6
7
public ParentWithCon(String mem1) {
8
this.mem1 = mem1;
9
}
10
11
public ParentWithCon(String mem1, String mem2) {
12
this.mem1 = mem1;
13
this.mem2 = mem2;
14
}
15
16
}
17
18
class ChildWithCon extends ParentWithCon {
19
public ChildWithCon(String mem1) {
20
super(mem1);
21
}
22
23
public ChildWithCon(String mem1, String mem2) {
24
super(mem1, mem2);
25
}
26
}
Copied!

2) Whats the output ?

1
package com.gs.ilp.corejava.Constructor;
2
3
class AA {
4
5
AA() {
6
System.out.println("AA");
7
}
8
9
AA(int i) {
10
this();
11
System.out.println("AA " + i);
12
}
13
}
14
15
class BB extends AA {
16
BB() {
17
System.out.println("BB");
18
}
19
20
BB(int i) {
21
this();
22
System.out.println("BB " + i);
23
}
24
}
Copied!
1
package com.gs.ilp.corejava.Constructor;
2
3
public class Problem2 {
4
public static void main(String[] args) {
5
new BB(4);
6
}
7
}
Copied!

Output :

1
AA
2
BB
3
BB 4
Copied!

3) Whats the output ?

1
package com.gs.ilp.corejava.Constructor;
2
3
class AA {
4
5
AA(int i) {
6
this();
7
System.out.println("AA " + i);
8
}
9
}
10
11
class BB extends AA {
12
BB() {
13
System.out.println("BB");
14
}
15
16
BB(int i) {
17
this();
18
System.out.println("BB " + i);
19
}
20
}
Copied!
1
CTE
Copied!

4) Will it compile ?

1
package com.gs.ilp.corejava.Constructor;
2
3
public class AAA {
4
Object m1() {
5
return null;
6
}
7
}
8
class BBB extends AAA {
9
String m1() {
10
return null;
11
}
12
}
Copied!

5) Will it compile ?

1
package com.gs.ilp.corejava.Constructor;
2
3
public class AAA {
4
5
void m1() {
6
7
}
8
}
9
10
class BBB extends AAA {
11
12
int m1() {
13
return 0;
14
}
15
}
Copied!

6) Whats the output ?

1
package com.gs.ilp.corejava.Constructor;
2
3
class AAA {
4
5
Object m1() {
6
System.out.println("AAA m1");
7
return null;
8
}
9
10
}
11
12
class BBB extends AAA {
13
14
String m1() {
15
System.out.println("BBB m1");
16
return null;
17
}
18
}
19
20
public class Test2 {
21
public static void main(String[] args) {
22
AAA aaa = new AAA();
23
aaa.m1();
24
25
BBB bbb = new BBB();
26
bbb.m1();
27
28
AAA aba = new BBB();
29
aba.m1();
30
}
31
}
Copied!
1
AAA m1
2
BBB m1
3
BBB m1
Copied!

7) Will it compile ?

1
package com.gs.ilp.corejava.Constructor;
2
3
class Animal {
4
5
}
6
7
class Dog {
8
9
}
10
11
class Parent {
12
Animal m1() {
13
return null;
14
}
15
}
16
17
class Child {
18
Dog m1() {
19
return null;
20
}
21
}
22
23
public class Test3 {
24
25
}
Copied!
Last modified 2yr ago