I've been reading through the text and a bit confused on one thing, if you do something like
Person p = new Student(); //student extends person
you can't access any of the student methods the person class doesn't have
BUT if the student and person method have the same method like toString()
it will be overriden by the subclass?
#Polymorphism question MOOC part 9
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
I've been reading through the text and a bit confused on one thing, if you do something like
Person p = new Student(); //student extends person
you can't access any of the student methods the person class doesn't have
BUT if the student and person method h
When you create an object using the superclass type, you can only access the methods and fields that are defined in the superclass. If you want to access the subclass-specific methods, you need to cast the object back to the subclass type.
Example:
Person p = new Student();
Student s = (Student) p;
s.studentMethod();
Useful links:
- AI generated response
every class has toString equals and hashcode by default you can always override it
every class in Java extends Object superclass
which has these
This beginner Java tutorial describes fundamentals of programming in the Java programming language
sorry if it wasn't clear
im basically asking if the superclass and subclass have the same method it is overriden by the subclass but objects instantiated as a superclass but referencing a subclass cannot access subclass methods the superclass doesnt have
yeah i know this much im asking about what methods a object like
SuperClass obj = new Subclass(); has access to
every method the subclass has
and the superclass has given the superclass method havent been overriden
then u get the override ones
just tried it i got my answer it only has access to thee superclass methods but if its overriden in the subclass then it will call that method
no?
class Animal {
public void speak() {
System.out.println("Speaking");
}
public void eat() {
System.out.println("Eating food");
}
}
class Dog extends Animal {
@Override
public void speak() {
System.out.println("Barking");
}
public void greet() {
System.out.println("Greetings human");
}
}
class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.speak();
dog.eat();
dog.greet();
}
}
Detected code, here are some useful tools:
System out
[Nothing]
See output
Barking
Eating food
Greetings human
Detected code, here are some useful tools:
ofc u cant call greet on Animal animal = new Dog(); because it doesnt exist on animal just on dog
A little bit late to this but yeah, you are completely correct. In your Person p = new Student() example, you'd only have direct access to methods from the Person class, but those methods can still be overridden by the Student class, which get used since it's a Student object.