Page 1 of 1

The super keyword Using the keyword super Rate Topic: -----

#1 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Post icon  Posted 08 October 2009 - 03:31 AM

Using the super keyword

Hi all!
In that tutorial ill cover the uses of the super keyword

When extending a class , lets call it Parent class
using the "extends" keyword, you
create a new subclass, lets call it Child class, of the extended class.

Note that I will use These Parent and Child classes for the rest
of my tutorial. so pay attention!
public class Parent {
//the "superclass"
}
public class Child extends Parent{
//the subclass
}



Using super inside overriden methods

Suppose Parent class had a method to print something inside it,
called printMethod()
public class Parent {
public void printMethod(){
System.out.println("The print method of Parent");
}
}


After extending the Parent class, Child class automatically gets the
printMethod() of Parent class.
However, you decided that you want to override the printMethod()
of Parent, and assign it a new functionality in Child class.
public class Child extends Parent {
public void printMethod(){
System.out.println("The print method of Child");
}
}


Ok! everything is fine now!
if you tun this program:
//inside the child class add
public static void main(String args[]){
Child child = new Child();
child.printMethod();
}


the output of this program would be:
The print Method of Child.

Great!
what is the problem? it works fine!
actually, there is no problem. only, what happens if
you decided that you dont want to change completely the original printMethod() of Parent.
you actually want to Add it a new functionality.
so in order to use the original print method of Parent.
You will have to create a new instance of
Parent. right?
Not really. i mean, you could, but it would be a bad design.

Instead, you could use the keyword super!

ok. to be able to use the printMethod of Parent inside the Child class
we simply have to add the super keyword before the method name.
just like that:
super.printMethod();


ok. how it is seen inside the code?
//the new child class
public class Child extends Parent {
public void printMethod(){
super.printMethod();//here is the super keyword
System.out.println("The print method of Child");
}
public static void main(String args[]){
Child child = new Child();
child.printMethod();
}
}


the output of this code will be:

The print method of Parent
The print method of Child

That's it. As simple as that.

Using super inside Constructors

suppose the Parent class i mentioned before had that constructor:
public class Parent {
private int x;
public Parent(int newX){
this.x = x;
}
}


to invoke the constructor of Parent inside Child class,
we will use the super keyword again as following.
public class Child extends Parent{
public Child(int x){
super(x);//using the Parent class constructor.
}


important!
1.in the constructor, super must be located the first line!
2.the number of parameters super gets depend on the paramaters that the constructor of the Parent class gets.
if it had an empty constructor, super will be used as:
super();
if it had 2 parameters, you use it that way:
super(parameter1, parameter2);
and so on.

Using super with fields

You could use super also to refer to hidden fields.
However, it is highly [/b]NOT recommended to hide fields, so i wont cover it in my tutorial.

Thats all for the super keyword!
Hope it helped you to understand its uses! :)

Is This A Good Question/Topic? 2
  • +

Page 1 of 1