Java Inheritance and Object Casting

I am new to programming, I have a question, please help me. (This question is a Java question, but I can’t remember the syntax, but what I am writing here is basically that.)

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 does p say then?

+7
java inheritance
source share
18 answers

p is just a variable, it does not change the type of object that is in it.

You can think of a cup: you can put any liquid in it, but the cup will not change the type of liquid.

 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 coffe in there, so it hot! 
+12
source share

p is both Student and Person , but if you call a method (e.g. whoAreYou() ), Java will first try to find it in Student and then in Person .

+9
source share

"I am a student"?

This is called Dynamic Linking.

+7
source share

I think I know what you mean ...

p would say that he is a student because you will override the method in which the person speaks. In Java, it should look like this:

 class Person { public void speak() { System.out.println("I'm a person!"); } } class Student extends Person { @Override public void speak() { System.out.println("I'm a student"); } } 
+5
source share
 "i am a student" 

This is Java polymorphism in action. The speaks() method is defined in the base class Person and overridden in the derived Student class.

In Java, the base class reference can refer to an object of a derived class, and when an overridden method calls such a link, the type object the link refers to determines the version of the method to execute.

+4
source share

Even if your reference to p declared as Person , p is actually an instance of Student . Therefore, p will β€œspeak” no matter what the student says.

It is legal to have a reference to the Student instance as " Person " because "Student continues from Person."

+2
source share

P will say student. Because the Student object is entered in the Person object.

+2
source share

The above question, wholly owned by the inheritance mechanism, has the same property used by different objects.

 class Person { String identity; Person() { this.identity = "Person"; System.out.println("A class " + this.getClass().getName() + " speaks i am a " + identity); } 

}

 public class Student extends Person { public Student() { this.identity = "Student"; System.out.println("A class " + this.getClass().getName() + " speaks i am a " + identity); } public static void main(String[] args) { Person p = new Student(); } 

}

+2
source share

I would suggest "i am a student" . This is basic polymorphism .

0
source share

If the method is not static, the Student method will be called like the others. Just be careful: if the method speaks static, then the Person method will be called. This is called shelter.

0
source share

This is an example of single inheritance in java. In this example, β€œPerson” is the base class, where β€œStudent” is a derived class. If nothing is specified,

Person p = new Student ();

the p object (it is similar to the Person object) will get access to the properties of the Student class, which overrides the properties of its own base class Person.

0
source share

Here he demonstrates the concept of polymorphism, such as a Super-class, can contain a reference to a child class, and also demonstrates the concept of inheritance, for example, Student is the Person class, which Student extends Person. So here Person is the Super class, and Student is the child class. In accordance with the polymorphism of the class Person (Super Class) may contain a reference to the class Student (subclass).

0
source share

P will say: I am a student.

But it will only have the general behavior of both classes. any behavior that the student has, but not in Person, P will not be able to access this behavior.

0
source share

The β€œP” face is the link here that is initialized by the student object. Therefore, when the program starts, the Student class function will be called at runtime.

0
source share

Here P is the object of the parent class that holds the object of the child class. this is due to the fact that here parent and child relationships (through inheritance) exist because you extend the parent class to the Student class. Thus, an object of a parent class can contain objects of its entire child class. Now P can access all the properties of its child class.

0
source share

In Java, when Student extends from Person, you can decide what Student behavior is causing from the person, you can restrict the student if he is a person, you can implement it as static, and static methods are not redefined. they are just a shadow. If you intentionally need an instance method, you implement its specific behavior.

0
source share

There is a dynamic submit / dynamic binding method . Person p = new Student ();
p is a reference variable whose type Person calls the Student object. Student is a child class, and Person is the parent class that is extended. Two classes have methods, static or not.

0
source share

It will say that I am a student. This is an example of dynamic binding.

0
source share

All Articles