String Name String Nationality Height Hungry Tired Human String Name String Nationality Height Name Nationality Height
String Name String Nationality Height Hungry Tired Human String Name String Nationality Height Name Nationality Height
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;
package textio;
}
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;
}
package textio;
}
package textio;
}
}
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.