3

What is the difference between these two object creations where car is the super class and toyota is the subclass...

car t = new toyota(); toyota t = new toyota();

(I believe we cant do something like this : toyota t = new car();.... Why ?)

Inheritance is confusing me and so is polymorphism... Any help would be appreciated

Arn ZXY
  • 77
  • 1
  • 8

5 Answers5

3

The difference is in the type of the object t: in the first case, only car's methods are available, while in the second case you also get the toyota-specific methods, if any.

Here is an example:

public class car {
    public void drive() {...}
    public void stop() {...}
}
public class toyota extends car {
    public void drive() {... /*toyota-specific code*/}
    public void stop() {... /*toyota-specific code*/}
    public void rollUpWindows() {...}
}

If you declare

car c = new toyota();

you can call drive and stop, but not rollUpWindows. If you declare

toyota c = new toyota();

you can call all three methods.

There is a general concept of programming to an interfaces which is similar to case #1 above.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

The creation is the same. How you access the created object is different. Let's use a slightly modified example:

Car c = new ();
AutomaticTransmissionCar a = new AutomaticTransmissionCar();
StandardTransmissionCar s = new StandardTransmissionCar();
Car c = new AutomaticTransmissionCar();

a.drive();
s.drive();
c.drive();

Presumably, the drive() method for an AutomaticTransmissionCar is much different than the drive() method of a StandardTransmissionCar. This is the key of polymorphism: when we call c.drive(), the car automatically determines the correct drive() method to use.

(Note that this is probably not the best design, and is only used to illustrate the concepts here.)

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • It determines that by comparing signatures ? – Arn ZXY Dec 07 '13 at 19:45
  • @ArnZXY In this design, `drive()` is an abstract method which is declared in the `Car` class with implementations in `AutomaticTransmissionCar` and `StandardTransmissionCar`. In order for polymorphism to work, the signatures must be exactly identical for all three of these. Which `drive()` method to use is determined at run-time based on the *concrete type* of the variable `c`. In very simplified terms, Java knows that `c` is *really* an `AutomaticTransmissionCar` and so its `drive()` method is used. – Code-Apprentice Dec 07 '13 at 19:52
1

Let's start with the second creation (it's the easiest one) : Here you're creating a Toyota object and for the entire course of your program this will be a Toyota with all his specific properties and methods AND the protected/public properties and protected/public methods it INHERITED from car.

The first creation is also valid but is polymorfic, drawback is that you won't be able to address the Toyota specific properties and method, because it has only been declared as a Car. However deep down it's a Toyota, so when you do this

Toyota t2 = (Toyota)t; 

You've changed (casted) it to a Toyota.

The first creation works because a Toyota is also a Car. The other way around doesn't work because a Car isn't always a Toyota, it can be a BMW of a Lexus, .... and because the compiler has no certain way a telling what is can be, this is not allowed.

Little tip : inheritance is easy if you draw the inheritance tree. Put superclass on top and subclasses under it and so on. Inhertance works traveling down, not traveling up

Hope this clears it a bit

N0lf
  • 138
  • 7
0

In your example, toyota extends car. This means that toyota is a (or rather, isA) kind of a car. Every toyota is also a car, but not every car is a toyota (e.g., some of them may be Hondas).

If t is a reference to a car, it can hold any type of car - a Toyota, a Honda, a Ferrari, you name it. However, if t is defined as a toyota, it can't hold any old car, just toyotas.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Think of it this way: car is a superclass and toyota is subclass (meaning it inherits from car), in other words it makes sense to say that each toyota is a car, but not that each car is toyota. This is an example of IS-A relationship between two classes (Toyota IS-A car).

Melquiades
  • 8,496
  • 1
  • 31
  • 46