2. Whenever a subclass needs to refer to its immediate
superclass, it can do so by use of the keyword super.
If your method overrides one of its superclass's methods, you
can invoke the overridden method through the use of the
keyword super.
You can also use super to refer to a hidden field (although
hiding fields is discouraged).
It has advantage so that you don’t to have to perform
operations in the “parentclass” again.
Only the immediate “parentclass’s” data and methods can be
accessed
3. Super has two general forms:-
The first calls the superclass' constructor.
The second is used to access a member of the
superclass that has been hidden by a member of
a subclass.
4. public class Superclass
{
public void printMethod()
{
System.out.println("Printed in Superclass.");
}
}
5. Here is a subclass, called Subclass, that
overrides printMethod():
public class Subclass extends Superclass
{
public void printMethod() // overrides printMethod in Superclass
{
super.printMethod();
System.out.println("Printed in Subclass");
}
public static void main(String[] args)
{
Subclass s = new Subclass();
s.printMethod();
}
}
6. Within Subclass, the simple
name printMethod() refers to the one declared
in Subclass, which overrides the one
in Superclass.
So, to refer toprintMethod() inherited
from Superclass, Subclass must use a qualified
name, using super as shown. Compiling and
executing Subclass prints the following:
• Printed in Superclass.
• Printed in Subclass.
7. The syntax for calling a superclass constructor is
super();
--or–
super(parameter list);
With super(), the superclass no-argument constructor is
called.
With super(parameter list), the superclass constructor with
a matching parameter list is called.
If a constructor does not explicitly invoke a superclass
constructor, the Java compiler automatically inserts a call to
the no-argument constructor of the superclass.
8. If the super class does not have a no-argument
constructor, you will get a compile-time
error. Object does have such a constructor, so ifObject is
the only superclass, there is no problem.
If a subclass constructor invokes a constructor of its
superclass, either explicitly or implicitly, you might think
that there will be a whole chain of constructors called, all
the way back to the constructor of Object. In fact, this is
the case. It is called constructor chaining, and you need to
be aware of it when there is a long line of class descent.
9. class Box {
private double width;
private double height;
private double depth;
Box(Box ob) {
// pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
11. BoxWeight() {
super();
weight = -1;
}
BoxWeight(double len, double m) {
super(len);
weight = m;
}
}
class DemoSuper {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();
}
}
12. The second form of super acts somewhat like
this, except that it always refers to the superclass of
the subclass in which it is used.
This usage has the following general form:
super.member
Here, member can be either a method or an
instance variable.
This second form of super is most applicable to
situations in which member names of a subclass hide
members by the same name in the superclass.
13. class A {
int i;
}
class B extends A {
int i; // this i hides the i in A
B(int a, int b) {
super.i = a; // i in A
i = b; // i in B
}
14. void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}
15. This program displays the following:
i in superclass: 1
i in subclass: 2
Although the instance variable i in B hides the i
in A, super allows access to the I defined in the
superclass. As you will see, super can also be used
to call methods that are hidden by a subclass.
16. Sometimes a method will need to refer to the
object that invoked it. To allow this, Java
defines the this keyword.
this can be used inside any method to refer
to the current object. That is, this is always a
reference to the object on which the method
was invoked.
You can use this anywhere a reference to
an object of the current class’ type is
permitted.
17. THIS KEYWORD
The keyword this is useful when you need to
refer to instance of the class from its method.
The keyword helps us to avoid name conflicts.
As we can see in the program that we have
declare the name of instance variable and local
variables same.
Now to avoid the confliction between them we
use this keyword
18. class Rectangle{
int length,breadth;
void show(int length,int breadth){
this.length=length;
this.breadth=breadth;
}
int calculate(){
return(length*breadth);
}
}
public class UseOfThisOperator{
public static void main(String[] args){
Rectangle rectangle=new Rectangle();
rectangle.show(5,6);
int area = rectangle.calculate();
System.out.println("The area of a Rectangle is : " + area);
}
}
19. The area of a Rectangle is : 30.
Inthe example this.length and this.breadth refers
to the instance variable length and breadth while
length and breadth refers to the arguments passed in
the method.