tags:

views:

245

answers:

8

Hi

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?

+5  A: 

"I am a student"?

This is called Dynamic Binding

thephpdeveloper
Java does *NOT* have dynamic binding. This is just dynamic dispatch. See: http://stackoverflow.com/questions/533330/dynamic-dispatch-and-binding
polygenelubricants
+5  A: 

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.

Aaron Digulla
+1  A: 

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

matt b
A: 

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

D.Shawley
+3  A: 

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

}
Martijn Courteaux
A: 

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.

Petar Minchev
+1  A: 
"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.

codaddict
Specifically, all methods in Java are the equivalent of C++/C#'s virtual. Also... OMG UNICORN!
R. Bemrose
Good point...and yes we've become rare. Only 5 users left with "unicorn" in their user name :(
codaddict
@unicornaddict: 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 ;)).
Felix Kling
+4  A: 

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!
Hardcoded
Good example for a Java beginner (also; because no "abstract" is used +1)
Pindatjuh