0% found this document useful (0 votes)
33 views3 pages

String Name String Nationality Height Hungry Tired Human String Name String Nationality Height Name Nationality Height

The document discusses the "this" and "super" keywords in Java. The "this" keyword allows variables in a constructor to reference instance variables within the same class that have the same name as parameters. This avoids naming conflicts. The "super" keyword allows a subclass to call methods and access variables of its parent class. When used in a subclass's method, it calls the parent class's implementation of that method rather than overriding it with the subclass's version.

Uploaded by

Sahil Akhtar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views3 pages

String Name String Nationality Height Hungry Tired Human String Name String Nationality Height Name Nationality Height

The document discusses the "this" and "super" keywords in Java. The "this" keyword allows variables in a constructor to reference instance variables within the same class that have the same name as parameters. This avoids naming conflicts. The "super" keyword allows a subclass to call methods and access variables of its parent class. When used in a subclass's method, it calls the parent class's implementation of that method rather than overriding it with the subclass's version.

Uploaded by

Sahil Akhtar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

The two keywords explain in this week are truly important I'll start explaining the "this"

keyword as I have been using that one a bit more, the "this" keyword allows us to use the
same variable name under a constructor because it recognizes where we are taking the
variable from, take an example here:
public class human {
public String name;
public String nationality;
public double height;
static public boolean hungry ;
static public boolean tired ;
human(String name, String nationality, double height){
this.name = name;
this.nationality = nationality;
this.height = height;
this.tired = false;
this.hungry = false;
}
}
there are multiple variables here that can store different values, but I don't want to create a
different name for those variables as I can get confused at the moment of debugging my code
when I create my constructor, so in order to keep the same name for the variables we can use
the "this" keyword, the way java sees it is like: "Okey you have a variable under this class
call human and you want me to store on that variable the information that is pass when
someone called the constructor and just for reference the values that they can place here are
things like name nationality or height." Java wouldn't get confused not knowing to what
variable we actually mean because of the "this" keyword.

The super method assist us in using a variable or method on a class that got an inheritance of
another class, and although this sounds gibberish I all used to classes the "Dad" class and the
"Daughter" class and daughter will inherit what dad has by using the extends keyword (I
would also use another class called "Imthemain" to call the functions of dad and daughter
there), the code looks like this:
package textio;

public class Dad {

public void greetings() {


System.out.println("Hey I'm the dad method");
}

package textio;

public class Daughter extends Dad{

public void greetings() {


System.out.println("Hey I'm the daugther method");
}
}
package textio;

public class Imthemain {

public static void main(String[] args) {

Daughter try1 = new Daughter();


try1.greetings();

}
Output is: Hey I'm the daugther method
But in case that we would like to use the method from that dad class when we have a
daughter object we can use the super keyword and that will say  "Oh, okay you want me to go
to the main class (In this case the "Dad" class) and called whatever function you want from
there."  code below:
package textio;

public class Dad {

public void greetings() {


System.out.println("Hey I'm the dad method");
}

}
package textio;

public class Daughter extends Dad{

public void greetings() {


super.greetings();
//System.out.println("Hey I'm the daugther method");
}

}
package textio;

public class Imthemain {

public static void main(String[] args) {

Daughter try1 = new Daughter();


try1.greetings();

}
}
Output : Hey I'm the dad method
This is truly interesting as even though we have a Daughter object if we used the super
keyword and then the variable or method that we would like to run, it would go to this main
class and it would take it from there and sent it so we see that it says that is the dad method
because is calling the method on the dad class rather than on the daughter class, we can use
both but just for the sake of example I commented the other one.

You might also like