# Problems - Inheritance (till super keyword)

## Level 1:

### Basic single level inheritance

#### 1)

{% embed url="https://www.hackerrank.com/challenges/java-inheritance-1/problem" %}

#### 2)

{% embed url="https://www.hackerrank.com/challenges/java-inheritance-2/problem" %}

#### 3)

{% embed url="https://www.techgig.com/practice/question/single-inheritance/ejIyeUxtK0h1TG5DYlhpQ3h6ZDE0WWV2QlNreVZoWE42N1BPS3AvOGZzOEFUb2VEV29DWDZpby9MaTVIYVB4Qg==/1" %}

### Multilevel inheritance

#### 1)

{% embed url="https://www.techgig.com/practice/question/one-two-three/cjJZV0Rlc2w5K2hlZUxmSHgzdk9XUTgzOWhRZHh6UE16aENJSkFFeFp3NDJ5WGRIYnpZTzBWOXg0YlFlcXMvUQ==/1" %}

### Basic method overriding

#### 1)

{% embed url="https://www.hackerrank.com/challenges/java-method-overriding/problem" %}

#### 2)

{% embed url="https://www.hackerrank.com/challenges/java-method-overriding-2-super-keyword/problem" %}

#### 3)

{% embed url="https://www.techgig.com/practice/question/object-oriented-programming/bXpVZEt2bko0V0hPaHhiRDBsVU1tTnh6d3ZFRnF1cFdwMTRIUVNOVFIwZ0FrRmxHWWt1UDlVRmd1M3NJcERXZw==/1" %}

## Level 2:

### 1) Resolve the compilation error :

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LctQHdU4dinbdpfUGRL%2F-LctQSUCxIH4W_o7k0j9%2FScreen%20Shot%202019-04-20%20at%2011.50.56%20AM.png?alt=media\&token=0e6364e5-d1fd-4bf8-94b7-d0c4361e41e2)

```java
package com.gs.ilp.corejava.Constructor;

public class ParentWithCon {
	String mem1;
	String mem2;

	public ParentWithCon(String mem1) {
		this.mem1 = mem1;
	}

	public ParentWithCon(String mem1, String mem2) {
		this.mem1 = mem1;
		this.mem2 = mem2;
	}

}

class ChildWithCon extends ParentWithCon {
	public ChildWithCon(String mem1) {
		super(mem1);
	}

	public ChildWithCon(String mem1, String mem2) {
		super(mem1, mem2);
	}
}
```

### 2) Whats the output ?

```java
package com.gs.ilp.corejava.Constructor;

class AA {

	AA() {
		System.out.println("AA");
	}

	AA(int i) {
		this();
		System.out.println("AA " + i);
	}
}

class BB extends AA {
	BB() {
		System.out.println("BB");
	}

	BB(int i) {
		this();
		System.out.println("BB " + i);
	}
}
```

```java
package com.gs.ilp.corejava.Constructor;

public class Problem2 {
	public static void main(String[] args) {
		new BB(4);
	}
}
```

#### Output :

```
AA
BB
BB 4
```

### 3) Whats the output ?

```java
package com.gs.ilp.corejava.Constructor;

class AA {

	AA(int i) {
		this();
		System.out.println("AA " + i);
	}
}

class BB extends AA {
	BB() {
		System.out.println("BB");
	}

	BB(int i) {
		this();
		System.out.println("BB " + i);
	}
}
```

```
CTE
```

### 4) Will it compile ?

```java
package com.gs.ilp.corejava.Constructor;

public class AAA {
	Object m1() {
		return null;
	}
}
class BBB extends AAA {
	String m1() {
		return null;
	}
}
```

### 5) Will it compile ?

```java
package com.gs.ilp.corejava.Constructor;

public class AAA {

	void m1() {

	}
}

class BBB extends AAA {

	int m1() {
		return 0;
	}
}
```

### 6) Whats the output ?

```java
package com.gs.ilp.corejava.Constructor;

class AAA {

	Object m1() {
		System.out.println("AAA m1");
		return null;
	}

}

class BBB extends AAA {

	String m1() {
		System.out.println("BBB m1");
		return null;
	}
}

public class Test2 {
	public static void main(String[] args) {
		AAA aaa = new AAA();
		aaa.m1();

		BBB bbb = new BBB();
		bbb.m1();

		AAA aba = new BBB();
		aba.m1();
	}
}
```

```
AAA m1
BBB m1
BBB m1
```

### 7) Will it compile ?

```java
package com.gs.ilp.corejava.Constructor;

class Animal {

}

class Dog {

}

class Parent {
	Animal m1() {
		return null;
	}
}

class Child {
	Dog m1() {
		return null;
	}
}

public class Test3 {

}
```
