Pages

Inheritance

Inheritance
Definition

Inheritance is a mechanism in which object acquires all the properties and behaviors of parent object. Inheritance represent IS-A relationship. (or) Inheritance is the method by which objects of one class get the properties of objects of another class.

Why Use Inheritance ? 1. For method overriding (So Runtime Polymorphism).
2. For Code Reusability

Types of Inheritance 1. Single Inheritance : In Single Inheritance one class extends another class (one class only).
Example : Class B extends only Class A. Class A is a Super Class and Class B is a Sub Class.

2. Multiple Inheritance : In Multiple Inheritance, one class extending more than one class. Java does not support Multiple Inheritance.
Example : Class C extends Class A and Class B. Class C is a Sub Class of Class A and Class B.

3. Multilevel Inheritance : In Multilevel Inheritance, one class can inherit from a derived class. Hence, the derived class becomes the base class for the new class.
Example : Class C extend Class B and Class B extend Class A. Class C is subclass of B and B is a of subclass Class A.

4. Hierarchical Inheritance : In Hierarchical Inheritance, one class is inherited by many Sub Classes.
Example : Class B, C, and D inherit the same class A. Class A is a Super Class of Class B, Class C and Class D.

5. Hybrid Inheritance : In Hybrid inheritance is a combination of Single and Multiple Inheritance. Java does not support Hybrid Inheritence.
Example : All the Public and Protected members of Class A are inherited into Class D, first via Class B and secondly via Class C.

Inheritance Example package com.jaladhi.jst;
public class Bird {
      String name;
      public void eat() {
            System.out.println("I can eat");
      }
}
package com.jaladhi.jst;
public class Crows extends Bird {
      public void display() {
            System.out.println("My Name is " + name);
      }
}
package com.jaladhi.jst;
public class CrowsTest {
      public static void main(String[] args) {
            Crows crows = new Crows();
            crows.name = "David";
            crows.display();
            crows.eat();
      }
}
Output :
My Name is David
I can eat

What will be the output of this program ? package com.jaladhi.jst;
public class Animal {
      // Class Animal Variable and Methods
}
public class Cat {
      // Class Cat Variable and Methods
}
public class Dog extends Cat, Animal {
      // Class Dog Variable and Methods
}
Output :
A Class can not extend more than one Class. Class Dog is extending two classes – Class Animal and Class Cat. It is a compile time error in Java.

What will be the output of this program ? package com.jaladhi.jst;
public class First {
      int i = 10;
}
public class Second extends First {
      int i = 20;
}
public class MainClass {
      public static void main(String[] args) {
            A a = new B();
            System.out.println(a.i);
      }
}
Output :
10

What will be the output of this program ? package com.jaladhi.jst;
public class First {
      {
            System.out.println(1);
      }
}
public class Second extends First {
      {
            System.out.println(2);
      }
}
public class Third extends Second {
      {
            System.out.println(3);
      }
}
public class MainClass {
      public static void main(String[] args) {
            Third third = new Third();
      }
}
Output :
1
2
3

What will be the output of this program ? package com.jaladhi.jst;
public class First {
      String s = "Class First";
}
public class Second extends First {
      String s = "Class Second";
      {
            System.out.println(super.s);
      }
}
public class Third extends Second {
      String s = "Class Third";
      {
            System.out.println(super.s);
      }
}
public class MainClass {
      public static void main(String[] args) {
            Third third = new Third();
            System.out.println(third.s);
      }
}
Output :
Class First
Class Second
Class Third

What will be the output of this program ? package com.jaladhi.jst;
public class First {
      {
            System.out.println("THIRD");
      }
}
public class Second extends First {
      {
            System.out.println("SECOND");
      }
}
public class Third extends Second {
      {
            System.out.println("FIRST");
      }
}
public class MainClass {
      public static void main(String[] args) {
            Third third = new Third();
      }
}
Output :
THIRD
SECOND
FIRST

What will be the output of this program ? public class A {
      public A() {
            System.out.println("Class A Constructor");
      }
}
public class B extends A {
      public B() {
            System.out.println("Class B Constructor");
      }
}
public class C extends B {
      public C() {
            System.out.println("Class C Constructor");
      }
}
public class MainClass {
      public static void main(String[] args) {
            C c = new C();
      }
}
Output :
Class A Constructor
Class B Constructor
Class C Constructor

What will be the output of this program ? public class X {
      static void staticMethod() {
            System.out.println("Class X");
      }
}
public class Y extends X {
      static void staticMethod() {
            System.out.println("Class Y");
      }
}
public class MainClass {
      public static void main(String[] args) {
            Y.staticMethod();
      }
}
Output :
Class Y

What will be the output of this program ? public class X {
      public X(int i) {
            System.out.println(1);
      }
}
public class Y extends X {
      public Y() {
            System.out.println(2);
      }
}
Output :
Write explicit calling statement to super class constructor in Class Y constructor.

What will be the output of this program ? public class X {
      public X(int i) {
            System.out.println(1);
      }
}
public class Y extends X {
      public Y() {
            super(10);
            System.out.println(2);
      }
}
public class A {
      public A() {
            System.out.println(1);
            super();
            System.out.println(2);
      }
}
Output :
Constructor calling statements ( super() or this() ), if written, must be the first statements in the constructor.Correct Code…
What will be the output of this program ? public class A {
      public A() {
            super();
            System.out.println(1);
            System.out.println(2);
      }
}
public class A {
      public A(int i) {
           
      }
}
public class B extends A {
           
}
Output :
Class B doesn’t have any constructors written explicitly. So, compiler will keep default constructor. In that default constructor, there will be a calling statement to super class constructor (super()). But, it is undefined in Class A.To remove the errors, either define A() constructor in class A or write B() constructor in class B and call super class constructor explicitly.

What will be the output of this program ? public class A {
      public A() {
            //Either keep this constructor
      }
      public A(int i) {
           
      }
}
public class B extends A {
      public B() {
            super(10);
      }
}
public class A {
      public A() {
            super();
            this(10);
      }
      public A(int i) {
            System.out.println(i);
      }
}
Output :
A constructor can have either super() or this() but not both.

What will be the output of this program ? public class M {
      static {
            System.out.println('A');
      }
      {
            System.out.println('B');
      }
      public M() {
            System.out.println('C');
      }
}
public class N extends M {
      static {
            System.out.println('D');
      }
      {
            System.out.println('E');
      }
      public N() {
            System.out.println('F');
      }
}
public class MainClass {
      public static void main(String[] args) {
            N n = new N();
      }
}
Output :
A
D
B
C
E
F

What will be the output of this program ? public class M {
      int i;
      public M(int i) {
            this.i = i--;
      }
}
public class N extends M {
      public N(int i) {
            super(++i);
            System.out.println(i);
      }
}
public class MainClass {
      public static void main(String[] args) {
            N n = new N(26);
      }
}
Output :
27

What will be the output of this program ? public class M {
      int i = 51;
      public M(int j) {
            System.out.println(i);
            this.i = j * 10;
      }
}
public class N extends M {
      public N(int j) {
            super(j);
            System.out.println(i);
            this.i = j * 20;
      }
}
public class MainClass {
      public static void main(String[] args) {
            N n = new N(26);
            System.out.println(n.i);
      }
}
Output :
51
260
520

What will be the output of this program ? public class X {
      private int m = 48;
}
public class Y extends X {
      void methodOfY() {
            System.out.println(m);
      }
}
Output :
Because, private field ‘m’ is not visible to class Y. What will be the output of this program ? public class X {
      int m = 1111;
      {
            m = m++;
            System.out.println(m);
      }
}
public class Y extends X {
      {
            System.out.println(methodOfY());
      }
      int methodOfY() {
            return m-- + --m;
      }
}
public class MainClass {
      public static void main(String[] args) {
            Y y = new Y();
      }
}
Output :
1111
2220
What will be the output of this program ? public class A {
      static String s = "AAA";
      static {
            s = s + "BBB";
      }
      {
            s = "AAABBB";
      }
}
public class B extends A {
      static {
            s = s + "BBBAAA";
      }
      {
            System.out.println(s);
      }
}
public class MainClass {
      public static void main(String[] args) {
            B b = new B();
      }
}
Output :
AAABBB

What will be the output of this program ? public class X {
      int i = 101010;
      public X() {
            i = i++ + i-- - i;
      }
      static int staticMethod(int i) {
            return --i;
      }
}
public class Y extends X {
      public Y() {
            System.out.println(staticMethod(i));
      }
}
public class MainClass {
      public static void main(String[] args) {
            Y y = new Y();
      }
}
Output :
101010

What will be the output of this program ? public class A {
      void A() {
            System.out.println(1);
      }
}
public class B extends A {
      void B() {
            A();
      }
}
public class MainClass {
      public static void main(String[] args) {
            new B().B();
      }
}
Output:
Yes, but with a warning that method has a constructor name. Output will be 1

What will be the output of this program ? public class A {
      int i = 1212;
}
public class B extends A {
      A a;
      public B(A a) {
            this.a = a;
      }
}
public class MainClass {
      public static void main(String[] args) {
            A a = new A();
            B b = new B(a);
            System.out.println(a.i);
            System.out.println(b.i);
            System.out.println(b.a.i);
            b.a.i = 2121;
            System.out.println("--------");
            System.out.println(a.i);
            System.out.println(b.i);
      }
}
Output :
1212
1212
1212
——–
2121
1212
What will be the output of this program ? public class ClassOne {
      static int i, j = 191919;
      {
            --i;
      }
      {
            j++;
      }
}
public class ClassTwo extends ClassOne {
      static {
            i++;
      }
      static {
            --j;
      }
      public static void main(String[] args) {
            System.out.println(i);
            System.out.println(j);
      }
}
Output :
1
191918
What will be the output of this program ? public class A {
      int[] a = new int[5];
      {
            a[0] = 10;
      }
}
public class MainClass extends A {
      {
            a = new int[5];
      }
      {
            System.out.println(a[0]);
      }
      public static void main(String[] args) {
            MainClass main = new MainClass();
      }
}
Output :
0

What will be the output of this program ? public class Mooru {
      int methodOfMooru(int i) {
            i /= 10;
            return i;
      }
}
public class Entu extends Mooru {
      int methodOfEntu(int i) {
            i *= 20;
            return methodOfMooru(i);
      }
}
public class MainClass {
      public static void main(String[] args) {
            Entu b = new Entu();
            System.out.println(b.methodOfEntu(100));
      }
}
Output : 200

What will be the output of this program ? public class Aaru {
      static int i;
      static {
            i++;
      }
      {
            ++i;
      }
}
public class Aidu extends Aaru {
      static {
            --i;
      }
      {
            i--;
      }
}
public class MainClass {
      public static void main(String[] args) {
            System.out.println(new Aidu().i);
      }
}
Output : 0

What will be the output of this program ? public class MainClass {
      public MainClass(int i, int j) {
            System.out.println(method(i, j));
      }
      int method(int i, int j) {
            return i++ + ++j;
      }
      public static void main(String[] args) {
            MainClass main = new MainClass(10, 20);
      }
}
Output : 31.

What will be the output of this program ? public class Ondu {
      static {
            Yeradu.methodOfYeradu();
      }
}
public class Yeradu extends Ondu {
      static void methodOfYeradu() {
            System.out.println("Hi....");
      }
}
public class MainClass {
      public static void main(String[] args) {
            Yeradu.methodOfYeradu();
      }
}
Output : “Hi….” Message prints 2 times.

What will be the output of this program ? package com.jaladhi.jst;
public class Mooru {
      static int i = 111;
      int j = 222;
      {
            i = i++ - ++j;
      }
}
public class Nalku extends Mooru {
      {
            j = i-- + --j;
      }
}
Output :
i = -113
j = 110

What will be the output of this program ? package com.jaladhi.jst;
public class Ondu {
      int x = 2121;
}
public class Yeradu {
      int x = 1212;
      {
            System.out.println(x);
      }
}
public class MainClass {
      public static void main(String[] args) {
            Yeradu two = new Yeradu();
      }
}
Output : 1212.