7

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?

2
  • It really depends on whether "speaks" is defined as static or not... but in this question I'm guessing it's not. Commented Apr 12, 2010 at 13:14
  • "A class X speaks..." seems to imply perhaps a static method; it's certainly more suspect than "Objects of type X speaks...". If it is a static method, then p would speak its declared type, "i am a person". Commented Apr 12, 2010 at 13:15

18 Answers 18

12

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!
Sign up to request clarification or add additional context in comments.

1 Comment

Good example for a Java beginner (also; because no "abstract" is used +1)
10

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.

Comments

8

"I am a student"?

This is called Dynamic Binding

1 Comment

Java does NOT have dynamic binding. This is just dynamic dispatch. See: stackoverflow.com/questions/533330/dynamic-dispatch-and-binding
5

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

}

Comments

4
"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.

3 Comments

Specifically, all methods in Java are the equivalent of C++/C#'s virtual. Also... OMG UNICORN!
Good point...and yes we've become rare. Only 5 users left with "unicorn" in their user name :(
But you are the only one of these that have a unicorn avatar ;) I bet there are some more that have unicorn avatars but not a unicorn name (like me ;)).
2

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".

Comments

2

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

Comments

2

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

Comments

0

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

Comments

0

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.

Comments

0

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).

Comments

0

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.

Comments

0

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.

Comments

0

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.

1 Comment

Now P will speak i am student.
0

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.

Comments

0

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.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.