Saturday 23 March 2013

About super keyword


A subclass constructor is used to construct the instance variable of both the subclass and the super class.  The subclass constructor uses the keyword super to invoke the constructor method of the super class.  The keyword super is used subject to the following conditions.
Ø  super may be used only within the subclass constructor method
Ø  The call to super class constructor must appear as the first statement with in the subclass constructor
Ø  If a parameterized constructor is used, then the signature is matched with the parameters passed to the super method as super(parameter-list);

Example 1:

class student
{
            public student()
            {
            System.out.println("super");
            }
}
class exam extends student
{
            public exam()
            {
            System.out.println("sub");
            }
}
class s
{
            public static void main(String[] args)
            {
                        exam obj=new exam();
            }
}

O/P:    super
            sub

Example 2:

class student
{
            public student()
            {
            System.out.println("super");
            }
}
class exam extends student
{
            public exam()
            {
            super( );                            // explicit call of super( )
            System.out.println("sub");
            }
}
class s
{
            public static void main(String[] args)
            {
                        exam obj=new exam();
            }
}

O/P:    super
            sub

Example 3:

class student
{
            int len,bre;
            public student(int a,int b)
            {
            len=a;
            bre=b;
            }
            int area()
            {
            return len*bre;
            }
}
class exam extends student
{
            int ht;
            public exam(int x,int y,int z)
            {
            super(x,y);
            ht=z;
            }
            int volume()
            {
            return len*bre*ht;
            }
}
class s
{
            public static void main(String[] args)
            {
                        exam obj=new exam(2,3,4);
                        System.out.println("area="+obj.area());
                        System.out.println("volume="+obj.volume());
            }
}

            Another important use of super is applicable to situations in which member names of a subclass hide members by the same name in the super class.  It is similar to the ‘this’ keyword except that it always refers to the super class of the subclass in which it is used.  The general form is as
                        super.member
where member can be either a method or an instance variable

Example:

class A
{
            int i=7;
}
class B extends A
{
            int i=6;
            void show()
            {
            System.out.println("super i="+super.i);
            System.out.println("sub   i="+i);
            }
}

class s1
{
            public static void main(String[] args)
            {
                        B obj=new B();
                        obj.show();
            }
}

No comments:

Post a Comment