I am quite new to programming, i have a question please help me. ( this question is java question but i can't remember the syntax but what i write here is mostly it.)
A class Person speaks "i am a person"
A class Student speaks "i am a student"
Student extends from Person
Person p = new Student
then what is p speaking then?
p
is just variable, it doesn't change the type of the Object that's in it.
You can think of a cup: You can put any fluid in it, but the cup won't change the type fluid.
abstract class Fluid{
public String getTemp(){
return "unknown";
}
}
class Coffee extends Fluid{
public String getTemp(){
return "hot";
}
}
class Cola extends Fluid{
public String getTemp(){
return "ice-cold"
}
}
Fluid cup = new Coffee();
System.out.println(cup.getTemp()); //It's coffe in there, so it's hot!
p
is both a Student
and a Person
but if you call a method (like whoAreYou()
), Java will first try to find it in Student
and then in Person
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With