4 Replies - 715 Views - Last Post: 20 August 2008 - 12:20 AM Rate Topic: -----

#1 IMU  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 07-August 08

THIS AND SUPER

Posted 18 August 2008 - 10:26 PM

Hi guys
needed to know about the use of this and super in java.can any one explain this concept.i needed help in this concept.can any one also give a real example to where we can use this;.
Is This A Good Question/Topic? 0
  • +

Replies To: THIS AND SUPER

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: THIS AND SUPER

Posted 19 August 2008 - 01:55 AM

If you create a subclass then it's common that you call the super method in your constructor in order to make a call to the superclass, I.e the class above your subclass.

It's a bit abstract but maybe if I give you an example.

Let's say you have a class called Human as your superclass and you feel it could be a good idea to extend this class with two subclasses, the class Man and the class Woman.

Now to start with, the class Human might have a structure with afew variables like

String name;
int age;
int weight;
Color eyeColor;

These variables are common for all Human.

Now the Man class might have some special preferences like:

String job;
int Strength;

While a Woman could have

boolean period;
boolean badMood;
boolean irrational;

Now if you want to create an instance of Man or Woman, you could make a call to super (by writing super();) in their respective constructor to show that you want them to inherit the structure of Human as an addition to the preferences of their own class. I.e, the Man object would have all variables:

String name;
int age;
int weight;
Color eyeColor;
String job;
int Strength;

So super(); makes a call to the superclass constructor, normally you wanna do this to instantiate the variables of the superclass as they would all be pointers to null unless you assign them some values using the super([place some values in here]);
Super is also used to access methods and variables of the superclass, not only the constructor.

This post has been edited by Gloin: 19 August 2008 - 02:15 AM

Was This Post Helpful? 0
  • +
  • -

#3 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: THIS AND SUPER

Posted 19 August 2008 - 01:58 AM

Some words on super

This is a pointer to the current instance, so for example if you have a method parameter that hides a member variable, you can access the member variable using the this keyword. This is quite common with Java bean getter/setter methods:
public class SomeClass{
  int number = 5;
  public void setNumber(int number){
		 this.number = number;
  }
  //...
}


Was This Post Helpful? 0
  • +
  • -

#4 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: THIS AND SUPER

Posted 19 August 2008 - 02:12 AM

As for 'this', you can use it to announce to Java that you want to operate on an instance variable within the class.

Let's say you have a class abcde. (Don't ever write code like this))
In this class you have some variables:
private int a;
private int b;
private int c;
private int d;
private int e;

and also you have some method

public void modify_a(int a) {
int b = a;
this.a = b;
}

Now what happens in that method is that first you assign b the value of the local variable a (the value that you include with the method call), then you assign the class' private instance variable a the value in b. If you had left out the 'this' then you would have just reassigned the value of the local variable a.
'this' is a reference to the object instance of the current class.
Was This Post Helpful? 0
  • +
  • -

#5 IMU  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 07-August 08

Re: THIS AND SUPER

Posted 20 August 2008 - 12:20 AM

thank u guys for the help...gloin and 1lacca
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1