Breaking

Tuesday, April 16, 2019

What is the data type of super key word?

What is the data type of super key word?

Super keyword is employed for differentiating the bottom category options with derived category options. Super keyword is inserting a vital role in 3 places. they're at variable level, at technique level and at creator level.

Super at Variable level

Whenever we have a tendency to inherit the bottom category members into derived category, there's a prospect that base category members area unit like derived category members.

In order to tell apart the bottom category members with derived category members within the derived category, the bottom category members are going to be preceded by a keyword super.
What is the data type of super key word
LEARNWITHARYAN

Syntax for super at Variable level:

super. base category member name
For example:

class Bc
;
class Dc extends B.C.
{
    int a;
    void set (int x, int y)
    {
        super.a=x;
        a=y; //by default 'a' is preceded with 'this.' since 'this.' represents current category
    }
    void add ()
    
};
class InDemo

};

Super at technique level

Whenever we have a tendency to inherit the bottom category strategies into the derived category, there's a prospect that base category strategies area unit like derived strategies.

To differentiate the bottom category strategies with derived category strategies within the derived category, the bottom category strategies should be preceded by a keyword super.

Syntax for super at technique level:

super. base category technique name
For example:

class Bc
show ()
    {
        System.out.println ("BASE category - show...");
    }
};
class Dc extends B.C.
show ()
    {
        super.display (); //refers to base category show technique
        System.out.println ("DERIVED category - show...");
    }
};
class InDemo

};

Super at creator level

Whenever we have a tendency to develop any inheritance application, we have a tendency to use to make continually object of bottom most derived category. after we produce associate degree object of bottom most derived category, it in turns calls its immediate super category default creator and it in turns calls its prime most super category default creator. Therefore, in JAVA surroundings, constructors are going to be known as continually from bottom to prime and also the execution starts from prime to bottom.

Consider the subsequent multi level inheritance:


Super () is employed for vocation super category default creator from default creator or from parameterized creator of derived category. it's ex gratia.

Super (...) is employed for vocation super category parameterized creator either from default creator or from parameterized creator of derived category. it's continually obligatory.

Rules:

Whenever we have a tendency to use either super () or super (...) in derived category constructors they have to be used as initial statement.


1. Whenever we wish to decision default creator of base category from default creator of derived category mistreatment super () in default creator of derived category is ex gratia.

For example:

class Bc
{
    Bc ()
    {
        System.out.println ("I AM FROM BASE category...");
    }
};
class Ibc extends B.C.
{
    Ibc ()
    {
        System.out.println ("I AM FROM INTERMEDIATE BASE category...");
    }
};
class Dc extends Ibc
{
    Dc ()
    {
        super (); //optional
        System.out.println ("I AM FROM DERIVED category...");
    }
};
class InDemo3

};
2. Whenever we wish to decision the super category parameterized category from parameterized category of the derived category mistreatment super (...) in parameterized category of derived category is obligatory.

For example:

class C1
{
    int a;
    C1 (int a)
    {
        System.out.println ("PARAMETERIZED creator - C1");
        this.a=a;
        System.out.println ("VALUE OF a = "+a);
    }
};
class C2 extends C1
{
    int b;
    C2 (int a, int b)
    {
        super (a);
        System.out.println ("PARAMETERIZED creator - C2");
        this.b=b;
        System.out.println ("VALUE OF b = "+b);
    }
};
class InDemo4

};
3. Whenever we wish to decision default creator of base category from parameterized category of derived category mistreatment super () in parameterized category of derived category is ex gratia.

For example:

class C1
{
    int a;
    C1 ()
    {
        System.out.println ("PARAMETERIZED creator - C1");
        this.a=a;
        System.out.println ("VALUE OF a = "+a);
    }
};
class C2 extends C1
{
    int b;
    C2 (int b)
    {
        super (); //optional
        System.out.println ("PARAMETERIZED creator - C2");
        this.b=b;
        System.out.println ("VALUE OF b = "+b);
    }
};
class InDemo5

};
4. Whenever we wish to decision parameterized category of base category from default creator of derived category mistreatment super (...) in default creator of derived category is obligatory.

For example:

class C1
{
    int a;
    C1 (int a)
    {
        System.out.println ("PARAMETERIZED creator - C1");
        this.a=a;
        System.out.println ("VALUE OF a = "+a);
    }
};
class C2 extends C1
{
    int b;
    C2 ()
    {
        super (10);
        System.out.println ("DEFAULT creator - C2");
        this.b=20;
        System.out.println ("VALUE OF b = "+b);
    }
};
class InDemo6

};
Best example for the higher than given rules:

class Bc
{
    Bc ()
    {
        System.out.println ("BASE category - DEFAULT CONSTRUCTOR");
    }
    Bc (int x)
    {
        this ();
        System.out.println ("BASE category - PARAMETERIZED CONSTRUCTOR");
    }
};
class Ibc extends B.C.
{
    Ibc ()
    {
        super (100);
        System.out.println ("INTERMEDIATE BASE category - DEFAULT CONSTRUCTOR");
    }
    Ibc (int x)
    {
        this ();
        System.out.println ("INTERMEDIATE BASE category - PARAMETERIZED CONSTRUCTOR");
    }
};
class Dc extends Ibc
{
    Dc ()
    {
        this (10);
        System.out.println ("DERIVED category - DEFAULT CONSTRUCTOR");
    }
    Dc (int x)
    {
        super (10);
        System.out.println ("DERIVED category - PARAMETERIZED CONSTRUCTOR");
    }
};
class StDemo

};

No comments:

Post a Comment