Sunday, March 5, 2017

Method Overriding In Java

When Superclass and subclass have method having same signature and name then a call to that method using subclass object will lead to invoking subclass version of that method. This is called method overriding.

Following Code will illustrate the concept:

Code::

class d
{
    void show()
    {
        System.out.println("D's method");
    }
}
class e extends d
{
    void show()
    {
        System.out.println("E's method");
    }
}
class f extends e
{
    void show()
    {
        System.out.println("F's method");
    }
}
public class MethodOverriding {

    public static void main(String[] nt)
    {
        f ob=new f();
        ob.show();
    }

}

Output::
As the above example shows that only subclass version is called.

Order of Execution of Constructors Java

Constructors follow the order of derivation in case of Inheritance
i.e- Superclass to Subclass
firstly superclass constructor is called then subclass constructor is called.
also in case of Multilevel inheritance.

Following code will illustrate the concept:

Code::

class a
{
    a()
    {
        System.out.println("A's Constructor");
    }
}
class b extends a
{
    b()
    {
        System.out.println("B's Constructor");
    }
}
class c extends b
{
    c()
    {
        System.out.println("C's Constructor");
    }
}
public class Constructors
{

    public static void main(String[] nt)
    {
        c ob=new c();
    }

}

Output::


super.member in JAVA

Keyword Super is also used to use the members of superclass which are same as the members of subclass.In this case if we use that member it refers to the member of subclass.

Syntax::   super.member;
here member can be data member or method

Following code will illustrate the concept:

Code::

import java.util.*;
class Base
{
    Scanner in=new Scanner(System.in);
    int a,b;
    Base()
    {
        System.out.print("Enter Value of a:");
        a=in.nextInt();
        System.out.print("Enter Value of b:");
        b=in.nextInt();
       
    }
    void display()
    {
        System.out.println("A= "+a+" \nB= "+b);
    }
}
class Derived extends Base
{
    Scanner in=new Scanner(System.in);
    int c;
    Derived()
    {
        System.out.print("Enter Value of c:");
        c=in.nextInt();
    }
    void display()
    {   super.display();
        System.out.println("C= "+c);    
    }
}
public class Inheritance
{

    public static void main(String[] nt)
    {
        Derived ob2=new Derived();
        System.out.println("Values are:");
        ob2.display();
    }
}

Output::

Saturday, March 4, 2017

Super() in Java

Keyword Super is used to refer to the immediate Superclass in case of Inheritance from the Subclass.It is used to call the constructor in case of constructor overloading because without this default constructor is called.

It is used in two forms:

1.Super is used to call the constructor of the Superclass in Subclass Constructor.

Syntax:: super(parameter  list)

Code::

import java.util.*;
class Base
{
    Scanner in=new Scanner(System.in);
    int a,b;
    Base()
    {
        System.out.print("Enter Value of a:");
        a=in.nextInt();
        System.out.print("Enter Value of b:");
        b=in.nextInt();
       
    }
    void show()
    {
        System.out.println(a+b);
    }
}
class Derived extends Base
{
    Scanner in=new Scanner(System.in);
    int c;
    Derived()
    {
        super();
        System.out.print("Enter Value of c:");
        c=in.nextInt();
    }
    void display()
    {
        System.out.println(a+b+c);     // a,b are the members of superclass
    }
}
public class Inheritance
{

    public static void main(String[] nt)
    {
        Derived ob2=new Derived();
        System.out.print("Sum is:");
        ob2.display();
    }
}

Output::

Reference To Subclass Object (Object Slicing)

A Superclass reference variable can refer to a Subclass Object .By doing this the superclass reference variable can access only those members of subclass object which are inherited (those are members of superclass).

Following Code will illustrate the concept:

Code::

class Super
{
    int a,b;
    void show()
    {
        System.out.println(a+b);
    }
}
class Sub extends Super
{
    int c;
    void display()
    {
        System.out.println(a+b+c);     // a,b are the members of superclass
    }
}
public class Inheritance
{

    public static void main(String[] nt)
    {
        Super ob;
        Sub ob2=new Sub();
        ob2.a=50;
        ob2.b=10;
        ob2.c=10;
        ob=ob2;         //superclass reference  variable is assigned a subclass object
        ob.show();
        ob2.display();
    }
}
Output::

Converse is not True i.e a Subclass variable cannot refer to a Superclass object.
Reason behind this can be that Subclass has some members which are not in Superclass so here error occurs.

Multilevel Inheritance Java

In Multilevel Inheritance the Subclass of one superclass is acting as a Superclass for another subclass. Hence the members of both superclass are present in the subclass.

Following code will illustrate the concept:

Code::
import java.util.*;
class A
{
    int a;
}
class B extends A
{
    int b;
}
class C extends B
{
    int c;
    void show()
    {
        System.out.println(a+b+c);                //a,b are members of superclass
    }
}
public class Multilevel
{

    public static void main(String[] nt)
    {   C ob=new C();
        Scanner in =new Scanner(System.in);
        System.out.print("Enter First Number:");
        ob.a=in.nextInt();
        System.out.print("Enter Second Number:");
        ob.b=in.nextInt();
        System.out.print("Enter Third Number:");
        ob.c=in.nextInt();
        System.out.print("Sum is: ");
        ob.show();
    }
   
}

Output::
Java Does not Support Multiple Inheritance Because in Multiple Inheritance if Two Classes have same members then its an ambiguity for Subclass to use which member.  So There is no Multiple Inheritance in Java. But we can achieve this with the help of Interfaces.

Single Inheritance Java

Inheritance is  a feature of reusing the code.Without starting from scratch we can create a new class(Child,Subclass) having all features of old(Parent,Superclass) in addition to its own features.

Syntax:  class Subclass name extends Superclass name
{

//body of sub class

}

Following Code will illustrate the concept:

Code::

class Super
{
    int a,b;
    void show()
    {
        System.out.println(a+b);
    }
}
class Sub extends Super
{
    int c;
    void display()
    {
        System.out.println(a+b+c);     // a,b are the members of superclass
    }
}
public class Inheritance
{

    public static void main(String[] nt)
    {
        Super ob=new Super();
        Sub ob2=new Sub();
        ob.a=50;
        ob.b=20;
        ob2.a=50;
        ob2.b=20;
        ob2.c=10;
        ob.show();
        ob2.display();
    }
}

Output::
Above example also illustrate the fact that we can use a superclass itself.