Inheritance FAQ

Que. Explain method overriding with suitable example?

Ans:

                Whenever same prototype of method is present in super class and sub class then method of sub class overrides the method of super class. Such type of method is known as overridden method and the technique is known as method overriding. In method overriding, object of sub class can access overridden method of sub class and cannot directly access overridden method of super class.

The concept of method overriding is useful to change the logical instructions of method of existing class without changing the existing code.

Example 1:

class A

{

                public void show()

                {

                                System.out.println(“I am in A”);

                }

}

class B extends A

{

                public void show()

                {

                                System.out.println(“I am in B”);

                }

}

class UseMe

{

                public static void main(String args[])

                {

                                B b1=new B();

                                b1.show();

                }

}

Output:                I am in B

                In the above example, class B is sub class of super class A and both the classes contain same prototype of show () method with different logical instructions therefore show () method of class B will override the show () method of class A and object of class B will access show () method of its own class i.e. show () method of class A is hidden for the object of sub class B. show () method is known as overridden method.

Que. Explain super keyword and super method?

Ans:

                super is standard inbuilt keyword of java language. It is used to access member variable, methods and constructors of super class from the block of sub class i.e. super keyword provides three different features as follows.

  1. To access member variable of super class in sub class.
  2. To access constructor of super class in sub class.
  3. To access methods of super class in sub class.

Example 1:

class A

{

                public void show()

                {

                                System.out.println(“I am in A”);

                }

}

class B extends A

{

                public void show()

                {

                                System.out.println(“I am in B”);

                }

}

class UseMe

{

                public static void main(String args[])

                {

                                B b1=new B();

                                b1.show();

                }

}

Output:                I am in B

In the above example, class B is sub class of super class A and both the classes contain same prototype of show () method due to which class B will not directly access the show () method of super class A. To access the show () method of super class A, we have to use super keyword that helps to access members of super class from the block of sub class as given below.

Example 1:

class A

{

                public void show()

                {

                                System.out.println(“I am in A”);

                }

}

class B extends A

{

                public void show()

                {

                                super.show();

                                System.out.println(“I am in B”);

                }

}

class UseMe

{

                public static void main(String args[])

                {

                                B b1=new B();

                                b1.show();

                }

}

Output:

I am in A

I am in B

                In the above example, super keyword is used to access the show () method of super class A.

Que. Explain the flow the execution of constructor in inheritance?

Ans:      

                Whenever the object is created, it will allocate memory as per the number of member variables available in its class as well as in the super classes and therefore it is necessary to initialize the value of all the member variables of instance. To initialize the value of member variables of object, java interpreter invoke all the default constructor of super classes and after the complete execution of all the default constructors of super classes it will invoke constructor of its own class (requested constructor).

Example1:

class A

{

                private int n;

                public A()

                {

                                a=10;

                }

                public void showA()

                {

                                System.out.println(“A=”+a);

                }

}

class B extends A

{

                private int b;

                public B(int n)

                {

                b=n;

                }

                public void showB()

                {

                                System.out.println(“B=”+b);

                }

}

class UseMe

{

                Public static void main(String arg[])

                {

                                B b1=new B(200);

                                b1.showA();

                                b1.showB();

                }

}

Que. Explain super member variable of java language?

Ans:

                                Whenever super and sub class contain same variable/object then variable/object of sub class will hide the variable/object of super class such type of variable/object is known as hidden variable or hidden object, in such case, sub class will not able to access member variable of super class. To access the member variable of super in sub class we have to use super keyword.

Example 1:

class A

{

                public int n;

                public void showA()

                {

                                System.out.println(“A=”+n);

                }

}

class B extends A

{

                public int n;

                public void setB(int n1,int n2)

                {

                                n=n1;

                                n=n2;

                }

                public void showB()

                {

                                System.out.println(“B=”+n);

                }

}

class UseMe

{

                public static void main(String args[])

                {

                                B b1=new B();

                                b1.show();

                }

}

Output:                I am in B

Leave a Reply

Your email address will not be published. Required fields are marked *