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

4
  • I recommend you to read SCJP 1.6 Programmer + Commented Dec 7, 2013 at 19:38
  • I think that's a certification guide and its probably more advanced... I'm a high school student Commented Dec 7, 2013 at 19:40
  • To understand polymorphism, you need a more complete example. See the answers below and feel free to ask more questions if something isn't clear. Commented Dec 7, 2013 at 19:43
  • I don't think it is a book to complex, give it a try specifically on polymorphism and then decide if you are interested or not. Commented Dec 7, 2013 at 22:22

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks :) I got it now
That concept is really good from what I see but I'm just bookmarking it now because I haven't read about interfaces yet...
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.)

2 Comments

It determines that by comparing signatures ?
@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.
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

Comments

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.

Comments

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

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.