LAT
LAT

Reputation: 291

Java inheritance and object casting

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?

Upvotes: 7

Views: 5106

Answers (18)

Anchal Shrestha
Anchal Shrestha

Reputation: 1

This is a example of single inheritance in java.In this example,"Person" is a base class where as "Student" is a derived class.Unless anything is specified,

Person p=new Student();

object p(it seems like object of Person) will access the properties of Student class which has overriden the properties of its own base class Person.

Upvotes: 0

Srinivas Repuri
Srinivas Repuri

Reputation: 21

Above question completely belonging to inheritance mechanism same property used by different entities.

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();
    }
}

Upvotes: 2

Giridhar Kumar
Giridhar Kumar

Reputation: 629

Here p will speak I am a student. This is an example of dynamic binding.

Upvotes: 0

Yasith Saminthaka
Yasith Saminthaka

Reputation: 11

There is dynamic method dispatch/dynamic binding. Person p = new Student();
p is a reference variable that type of Person which called to Student object.Student is child class and Person is parent class which extended.Two classes has methods that static or not.

Upvotes: 0

Kumar Abhishek
Kumar Abhishek

Reputation: 3124

In Java when Student extends from Person you can decide what kind of behavior Student invokes from person , You can restrict student to say if it is person, You can implement it as an static then static methods are not overridden. they just shadow. If Intentionally you want instance method then you implement its specific behavior.

Upvotes: 0

Mandy Sharma
Mandy Sharma

Reputation: 19

Here P is the Parent class Object that is holding the child class object. this is because here parent and child relationship(through inheritance ) exists because you are extending the Parent class into the Student class. Thus Parent class Object can hold the Objects of all its children class. Now P can access all the properties of its child class.

Upvotes: 0

rajvineet
rajvineet

Reputation: 319

Person "P" is a reference here which is initialized with object of student. So when the execution of program will start, at runtime Student's class function will be called.

Upvotes: 0

Varun Bansal
Varun Bansal

Reputation: 33

P will Speak : I am student.

But it will only have common behavior of both class. any behavior which student has but not in Person, P will not be able to access that behavior.

Upvotes: 0

Mukesh
Mukesh

Reputation: 586

Here it is showing the concept of Polymorphism like Super class can hold the reference of child class along with that it is also showing the concept of Inheritance like Student is a Person means class Student extends Person. So here Person is a Super class and Student is a child class. As per the polymorphism Person Class (Super Class) can hold the reference of Student class (Sub-Class).

Upvotes: 0

user2790467
user2790467

Reputation:

P will say student. Because Student object is casted into Person object.

Upvotes: 2

Hardcoded
Hardcoded

Reputation: 6494

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!

Upvotes: 12

codaddict
codaddict

Reputation: 454970

"i am a student" 

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

In Java a base class reference can refer to a derived class object, and when a overridden method is call on such a reference, the type of the object to which the reference refers to decides the version of the method to be executed.

Upvotes: 4

Petar Minchev
Petar Minchev

Reputation: 47373

If the method is not static then the Student's method will be called as others mentioned. Just be careful that if the method speak is static, then the Person's method will be called. This is called hiding.

Upvotes: 0

Martijn Courteaux
Martijn Courteaux

Reputation: 68847

I think I know what you mean...

p would say he is a student, because you will override the method where 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");
    }

}

Upvotes: 5

D.Shawley
D.Shawley

Reputation: 59553

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

Upvotes: 0

matt b
matt b

Reputation: 139921

Even though your reference to p is declared as a Person, p is actually an instance of Student. Therefore p will "speak" whatever a student speaks.

It is legal to have refer to a Student instance as a "Person" since "Student extends from Person".

Upvotes: 2

Aaron Digulla
Aaron Digulla

Reputation: 328556

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.

Upvotes: 10

mauris
mauris

Reputation: 43619

"I am a student"?

This is called Dynamic Binding

Upvotes: 8

Related Questions