-2

I know its via inheritance when we say B c = new C(); but what is the difference with C c = new C()

I know this code works but the why and how is my epidemic. when I did my own math (using pen and paper) it gave me 80. But when I read through the OCA paper I noticed they arrived at the same answer but for different reasons.

"When the program is run, the main() method will call the max() method in C with parameters 10 and 20 because the actual object referenced by 'c' is of class C. This method will call the max() method in B with the parameters 20 and 40. The max() method in B will in turn call the max() method in A with the parameters 20 and 40. The max() method in A will return 40 to the max() method in B. The max() method in B will return 80 to the max() method in C. And finally the max() of C will return 80 in main() which will be printed out."

Which sounds more or less Greek to me, please help. Wanna know what it means to write B c = new C(); and how it differs to C c = new C();

VaJohnifi3d
  • 13
  • 1
  • 4

5 Answers5

0

If you write B c = new C(); the result will be the same because of dynamic binding. At runtime the object type will be used to resolve the method to call. The object type of c (C) ist still the same, so no difference.

The only difference in the code is, that the type of the reference has changed.

wake-0
  • 3,918
  • 5
  • 28
  • 45
0

Suppose we have C c = new C(); B b = c;

A C object is constructed but you only see its B part through b. Just like when different people see you differently: for example your friend see you as a friend but your parent see you as a child even if it is the same seen person (you are unique but people see you differently). Then talking to this object through b only permits to talk with a B vision of it (only what is defined as public in B). Of course the object is a C one then if you use a B method on it is answers with its C behavior to it. This is called polymorphism (different looks for a single object) by subtyping (looks are related to each other by inclusion).

A real life example of such usage could be the following. I imagine that you agree that a Car is a Vehicule as a Van is a Vehicle. Then when you build a parking you didn't design it only for cars but for any Vehicle, as you can park a Car or a Van. So it is very useful to see a Car or a Van as a simple Vehicle.

In java this could give:

class VehicleThatCouldBeParked {}
class Car extends VehicleThatCouldBeParked {}
class Van extends VehicleThatCouldBeParked {}

...
VehicleThatCouldBeParked []parking = ne VehicleThatCouldBeParked[100]; // build a parking with hundred locations
...
Car myCar = new Car(); // some car
...
Van myVan = new Van(); // some van
...
parking[76] = myCar; // my car is parked in slot 77
parking[34] = myVan; // my van is parked in slot 35
...

This is very important in many situations (even in real life) no to be aware of some details of objects.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
0

At runtime, the method of actual object will be called.

The difference however depends on your usage where possibly you have B defining a behavior and C, AND for the sake of answer D, E and F may be implementing it in a specific way. Now using the reference of B type, you can create a common contract for client.

Will suggest reading basic OOPs concept from tons of resources on internet.

PankajT
  • 145
  • 3
0

The issue you are concerning is about polymorphism. If you have variable (imagine it as box) pointing to class (let's call it A), pointing to some class, you can put in this box any class that extends A.

On you further problem, I advice reading carefully the post: Casting objects in Java

Community
  • 1
  • 1
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
0
abstract class A {
    protected abstract void print();
}

class B extends A {

    @Override
    protected void print() {
        System.out.println("I am in B");
    }

    public void print1() {
        System.out.println("No in A");
    }
}

class C extends A {

    @Override
    protected void print() {
        System.out.println("I am in C");
    }

    public void print1() {
        System.out.println("No in A");
    }
}

A a = new C(); a.print();

//a.print1(); can't resolve method print1()

//B b = new C(); incompatible type

Amit Yadav
  • 32,664
  • 6
  • 42
  • 57