Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,014 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,051 people online right now. Registration is fast and FREE... Join Now!




The super keyword

 
Reply to this topicStart new topic

> The super keyword, Using the keyword super

japanir
Group Icon



post 8 Oct, 2009 - 02:31 AM
Post #1


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!
CODE

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()
CODE

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.
CODE

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:
CODE

//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:
CODE

super.printMethod();

ok. how it is seen inside the code?
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:
CODE

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.
CODE

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! smile.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 07:25AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month