why do we using THIS keyword and SUPER keyword in programming ?

#3
public class Demo {
Demo(){
super();
System.out.println("in Demo Constructor");
}
public void m1(){
System.out.println("in m1 of Demo Class");
}
public void m2(){
System.out.println("in m2 of Demo Class");
this.m1();
}
public void m3(){
System.out.println("in m3 of Demo Class");
}
public static void main(String[] args) {
Demo d=new Demo();
d.m1();
d.m2();
d.m3();

Demo sd=new SubClassOfDemo();
sd.m1();
sd.m2();
sd.m3();


}
}
class SubClassOfDemo extends Demo
{

public SubClassOfDemo() {

super();

}

public void m1(){

System.out.println("in m1 of SubClasssDemo Class");
}
public void m2(){
super.m2();
System.out.println("in m2 of SubClasssDemo Class");
}

}
 
#4
hii radika..

about "this" :

If we create any object, internally a reference variable "this" will created and it points to the object.. so we can say ''this'' is a keyword that points to the current object. Suppose we hav created an object as Radhika rad=new Radhika(); then internally 2 reference variables "rad" and "this" will be pointed to the Radhika object. So by using "this" we can access members of Radhika object.

class Sample {
int i;
int j;
Sample(int i, int j) {
this.i=i;
this.j=j;
}}
class Demo {
public static void main(String args[]) {
Sample s=new Sample(10,20);
}}

about "super" :

"super" is a keyword used to access the super class members..
why super keyword?? --? Suppose we hav a super class and sub class having a method with same name, then from subclass if we want to acess the super class method, we need to use the suer keyword for accesing the super class method.
We can acess the super class members from subclass as super.variable,super.method(),super(values)...
ex:
class SupCls {
sameMethod(){ }
}
class SubCls {
sameMethod() { }
// calling super class method
super.sameMethod();
}

This is small discussion on super and this.. if u want more, contact me on srikkanth.99999@gmail.com
 
#5
Hi friends :),
what are the usages of This and Super keywords ? please send your explanations with sample codings ? :mad:
About this :
The key word this is useful when you need to refer to instance of class from its method. The keyword (this) helps us to avoid name conflicts. If we declare the name of instance variable and local variable with same name, then we'll get name conflicts. so to avoid it we use keyword <b> this </b>.

About super :
A subclass can call a constructor which defined in it's super class by using the following statement.
super();​
super() must always be the first statement that is executed inside a subclass' constructor.
 
#6
The super keyword in java programming language refers to the superclass of the class where the super keyword is currently being used.
THIS keyword holds the object of its own class.
 
#7
This :

This is used to refer the current class instance . For an Example

this.getName(); is use to access the getName() method for the current ClassInstance.

Super :

Super keyword is used to call immediate parent.
Super keyword can be used with instance members i.e., instance variables and instance methods.
Super keyword can be used within constructor to call the constructor of parent class.
 
#8
This will be a Pivotal word done java. It camwood a chance to be utilized inside the strategy alternately constructor for population. It(this) meets expectations Likewise An reference to the present item whose technique alternately constructor is, no doubt conjured. It is utilized inside An sub-class technique definition to call a strategy characterized in the super population.
 
Top