What is the difference between this() and super()?

Answer Posted / b.h srinath

This is a keyword that refers to the present/current class.
Usage of this:
1)it can be used to invoke constructor
syntax:this();
example: public sample
{
this(20);
this.display();
}
It is mandatory that,when we are calling constructor through
'this' it must be first statement in that method or constructor.
2)It is used to access data members
example:
this.len=len;
this bth=bth;
3)It is used to access member methods
example:
this.getvalue();
this.setvalue();
super() keyword:
super is a keyword that refers to super class from a subclass.
That means super can refer to super class

i)super class instance variable
example:System.out.println(super.a);
program: class A
{
int a = 30;
}

class B extends B
{

void show()
{
int a=10;
System.out.println("The a Value in super
class is"+super.a);
System.out.println("The a Value in super
class is"+a);
}
public static void main(String[] args)
{
B b=new B();
b.show()
}
}

ii)super class methods
example:super.setvalue();
program:

class one
{

int x,y,s;
one()
{

System.out.println("First went to one default const of super
class");
}

void show(int a,int b)
{

x=a;
y=b;


System.out.println("\n superclass value X is"+x);

System.out.println("\n superclass value Y is"+y);

}
void gets()
{

s=x+y;
}
}
class two extends one
{
int a,b,c;
two()
{

System.out.println("\n second came to default const of
child class");


}

void show()
{
super.gets();
System.out.println("\n baseclass value is"+s);

}
}
class intmet
{
public static void main(String args[])
{
two t=new two();
t.show(2,3);
t.show();
}
}


iii)super class constructor
example:super(a);
super(a,b) and so on

program:


class one
{

int x;
one()
{
System.out.println("First went to one default const of super
class");
}

one(int x)
{
System.out.println("\nas the supre(a)is referd one parameter
const is printed");

this.x=x;

}

void show()
{

System.out.println("\none parameter const value is"+x);

}

}

class two extends one
{

int x,a;
two()
{
System.out.println("\n second came to default const of
child class");
}

two( int a,int b)
{


super(a);

System.out.println("\ntwo const");
x=b;
}
void show()
{

System.out.println("\ntwo parameter const value is"+x);
super.show();

}

}

class const1
{
public static void main(String args[])
{
two t=new two();

two t1=new two(2,3);
t1.show();
}
}

Is This Answer Correct ?    1 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do you print array in java?

641


Can we declare an interface as final?

670


How do weakhashmap works?

716


What is type conversion in java?

664


Explain some best practices you would apply while using collection in java?

674






What is java command?

652


Can you declare the main method as final?

615


What is the difference between access specifiers and access modifiers in java? 16

625


Objects or references which of them gets garbage collected?

622


Explain the difference between map and flatmap stream operation?

910


Are arrays static in java?

671


Does apple use java?

596


What is binary tree in java?

643


Is java util list serializable?

609


Why volatile is used in java?

621