Super keyword
Super keyword is used for differentiating the base class features with derived class features.
Super keyword is placing an important role in three places. They are at variable level, at method
level and at constructor level.
• Super at variable level
Whenever we inherit the base class members into derived class, there is a possibility that
base class members are similar to derived class members.
In order to distinguish the base class members with derived class members in the derived
class, the base class members will be preceded by a keyword super.
Syntax for super at VARIABLE LEVEL:
super. base class member name
For example:
class Bc
{
int a;
};
class Dc extends Bc
{
int a;
void set (int x, int y)
{
super.a=x;
a=y; //by default 'a' is preceded with 'this.' since 'this.' represents current class
}
void sum ()
{
System.out.println ("SUM = "+(super.a+a));
}
};
class InDemo1
{
public static void main (String k [])
{
int n1=Integer.parseInt (k[0]);
int n2=Integer.parseInt (k[1]);
Dc do1=new Dc ();
do1.set (n1, n2);
do1.sum ();
}
};
• Super at method level
Whenever we inherit the base class methods into the derived class, there is a possibility that
base class methods are similar to derived methods.
To differentiate the base class methods with derived class methods in the derived class, the
base class methods must be preceded by a keyword super.
Syntax for super at method level: super. base class method name
For example:
class Bc
{
void display ()
{
System.out.println ("BASE CLASS - DISPLAY...");
}
};
class Dc extends Bc
{
void display ()
{
super.display (); //refers to base class display method
System.out.println ("DERIVED CLASS");
}
};
class InDemo2
{
public static void main (String k [])
{
Dc do1=new Dc ();
do1.display ();
}
};
Super keyword is used for differentiating the base class features with derived class features.
Super keyword is placing an important role in three places. They are at variable level, at method
level and at constructor level.
• Super at variable level
Whenever we inherit the base class members into derived class, there is a possibility that
base class members are similar to derived class members.
In order to distinguish the base class members with derived class members in the derived
class, the base class members will be preceded by a keyword super.
Syntax for super at VARIABLE LEVEL:
super. base class member name
For example:
class Bc
{
int a;
};
class Dc extends Bc
{
int a;
void set (int x, int y)
{
super.a=x;
a=y; //by default 'a' is preceded with 'this.' since 'this.' represents current class
}
void sum ()
{
System.out.println ("SUM = "+(super.a+a));
}
};
class InDemo1
{
public static void main (String k [])
{
int n1=Integer.parseInt (k[0]);
int n2=Integer.parseInt (k[1]);
Dc do1=new Dc ();
do1.set (n1, n2);
do1.sum ();
}
};
• Super at method level
Whenever we inherit the base class methods into the derived class, there is a possibility that
base class methods are similar to derived methods.
To differentiate the base class methods with derived class methods in the derived class, the
base class methods must be preceded by a keyword super.
Syntax for super at method level: super. base class method name
For example:
class Bc
{
void display ()
{
System.out.println ("BASE CLASS - DISPLAY...");
}
};
class Dc extends Bc
{
void display ()
{
super.display (); //refers to base class display method
System.out.println ("DERIVED CLASS");
}
};
class InDemo2
{
public static void main (String k [])
{
Dc do1=new Dc ();
do1.display ();
}
};
0 comments:
Post a Comment