Search This Blog

Monday, March 14, 2011

Role of Super Keyword in Java

In this article trying to explain the use of 'Super' keyword in Child class.
  1. super() method @ Child class constructor

  2. Super() with parameter method @ Child class constructor

  3. Super()default/parameter method@ child class member method

  4. Super keyword @child class Constructor

  5. Super keyword @ child class member method

    Basically super keyword always refer the parent class object reference. We all know the parent of all java class is java.lang.Object. So when ever we create a java file(class) with out extending any class \,java compiler will do three basic operation.
  1. Extend parent class java.lang.Object

  2. if no constructor(default/Parameter) for the child class its include
    the default constructor
    .

  3. If no super() method in constructor its include the super() in the
    first line of the constructor.

    Fact:

when ever we create the object for any User define class we always able to
access the methods from the Object class.Its does means always
create two object in Heap memory one is user define class and another
one is Object class.



S.No



Place of
Super



Description



When
executes



Where Not
Include



Sample
Code


1


Super() default method @
Child class Constructer


Super()
its used for calling parent class constructer from child class
constructer.In this case two object will be created .


First
Parent object will be created and than child object will be
created.


While
creating object reference for the child class.


It should be Present in
first line of the child class constructer.


Should not be two super()
method statement in same constructer.


package
superpck.blog;


class
Parent{


public
Parent() {


System.out.println("Creating
Parent Object"
);


}


}


class
Child
extends
Parent


{


public
Child() {


System.out.println("Creating
Child Object"
);


}



}


public
class
SampleSuper {


public
static
void
main(String arg[])


{


Child
child=new
Child();


}


}


output:


Creating
Parent Object


Creating
Child Object



2


Super() with
parameter method @ child class.


Super()
with Parameter it used for calling parent class constructer with
parameter from child class constructer.In this case two object
will be created .


First
Parent object will be created and than child object will be
created.


But
we can initialize parent class instance variable from this.





while
creating object for the child class


Same as S.No:1(Super()
default method @ Child class Constructer)


package
superpck.blog;


class
Parent{


Integer
Parentage;


public
Parent() {


System.out.println("Creating
Parent Object"
);


}


public
Parent(
int
age){
Parentage=age;}


}


class
Child
extends
Parent


{


public
Child() {


System.out.println("Creating
Child Object"
);


}


public
Child(
int
Parentage) {


super(Parentage);


}



}


public
class
SampleSuper {


public
static
void
main(String arg[])


{


Child
child=
new
Child(30);


System.out.println("Parent
age::"
+child.Parentage);


}


}


output:


Parent
age::30






3



Super() default/parameter
method @ child class member method.



Error as :


Constructor
call must be the first statement in a constructor



-



Compile time error



-



4



Super keyword @ Child
class constrcter



Its act as a parent class
object reference from child class.Using super keyword we can call
the parenet class member methods and instance variables only not
constrcter.






Its
depends upon the Instruction flow.



It can present any where
and many time in both child class constrcter/


member methods.



package
superpck.blog;


class
Parent{


Integer
Parentage;


public
Parent() {


System.out.println("Creating
Parent Object"
);


}


public
Parent(
int
age){
Parentage=age;}


public
void
memberMethod()


{System.out.println("Inside
member method of Parent"
);


}


}


class
Child
extends
Parent


{


public
Child() {


System.out.println("Creating
Child Object"
);


}


public
Child(
int
Parentage) {


super(Parentage);


super.memberMethod();


super.memberMethod();


}


}


public
class
SampleSuper {


public
static
void
main(String arg[])


{


Child
child=
new
Child(30);


System.out.println("Parent
age::"
+child.Parentage);


}


}





Output:


Inside
member method of Parent


Inside
member method of Parent


Parent
age::30



5



Super keword @ child class
member method.



Its
act as a parent class object reference from child class.Using
super keyword we can call the parenet class member methods and
instance variables only not constrcter.



Its
depends upon the Instruction flow.



It can present any where
and many time in both child class constrcter/


member methods.



Refer
s.No 4 sample.


No comments:

Post a Comment