1

i've read a lot of blogs, tutorials & co but i don't get something about the dynamic binding in java. When i create the object called "myspecialcar" it's creates an object from the class "car" as type of the class vehicle as a dynamic binding right? So java know that when i execute the method myspecialcar.getType() i have a car object and it execute the method from the class car. But why i got the type from the class vehicle? Is that because the variable from the class vehicle (type) is a static binding?

Regards,

Code:

public class vehicle {
    String type = "vehicle";

    public String getType(){
        return type;
    }
}

public class car extends vehicle {
    String type = "car";

    public String getType(){
        return type;
    }
}

public class test {
    public static void main (String[] args){
        vehicle myvehicle = new vehicle(); // static binding
        car mycar = new car(); // static binding
        vehicle myspecialcar = new car(); //dynamic binding

        System.out.println(myspecialcar.getType());
        System.out.println(myspecialcar.type);
        System.out.println(myspecialcar.getClass());
    }
}

Output:

car
vehicle
class car
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
CLNRMN
  • 1,409
  • 9
  • 23
  • 1
    Please read about [naming conventions](http://www.oracle.com/technetwork/java/codeconventions-135099.html). It helps other developers to read your code. – Silk0vsky Aug 09 '17 at 11:50

4 Answers4

3

Instance Method Rule

When an instance method is invoked on an object using a reference, it is the class of the current object denoted by the reference, not the type of the reference, that determines which method implementation will be executed.

Instance Property/Field Rule

When a field of an object is accessed using a reference, it is the type of the reference, not the class of the current object denoted by the reference, that determines which field will actually be accessed.

Yusuf Kapasi
  • 577
  • 2
  • 9
2

You faced the fields hiding.

Within a class, a field that has the same name as a field in the superclass hides the superclass's field, even if their types are different. Within the subclass, the field in the superclass cannot be referenced by its simple name. Instead, the field must be accessed through super, which is covered in the next section. Generally speaking, we don't recommend hiding fields as it makes code difficult to read.

Silk0vsky
  • 941
  • 1
  • 18
  • 34
1

There is a difference between hiding and overriding. You can not override class fields, but rather class methods. This means in your concrete example that

Vehicle mySpecialCar = new Car() // use upperCase and lowerUpperCase pls

You have a type of Vehicle which is an instance of Car. The over riden methods will be used while the class fields will be hidden.

If you use

Car myCar = new Car()

you will get

myCar.type // car
myCar.super.type // vehicle
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
1

You do not override class variables in Java you hide them.

Overriding is for instance methods. Hiding is different from overriding.

In your case you are hiding the member variable of super class. But after creating the object you can access the hidden member of the super class.

Adeel
  • 413
  • 1
  • 7
  • 22
  • Thank you, but i have one more question. I just thought that vehicle is my superclass, so why i'm hiding my subclass member variable? Or is car now the superclass because i used Vehicle mySpecialCar = new Car()? – CLNRMN Aug 09 '17 at 12:13
  • 1
    You seem very confused. See what you did is legal but not recommended just like cousin marriage :). Invoking a field using parent reference will invoke the field of the parent not the child object but for method it is opposite. – Adeel Aug 09 '17 at 12:21
  • Yes i'am confused :-) thanks for your help, also that i'ts not recommended. (it was a example of the studybook...) – CLNRMN Aug 09 '17 at 12:36