0

Please explain the difference between object one and two:

    car one = new opel();    
    opel two = new opel();  

Class opel extends class car.

kdmurray
  • 2,988
  • 3
  • 32
  • 47
lxknvlk
  • 2,744
  • 1
  • 27
  • 32
  • 1
    Read the [Java tutorial](http://docs.oracle.com/javase/tutorial/)? It explains the difference between objects and variables quite well ... – meriton Jan 21 '14 at 23:06
  • Assuming that `car` is a supertype (super class or interface) of `opel`, the first case is an example of programming to the `car` interface. My above comment links to a question that explains more about that. As an aside, you should consider following standard Java conventions that class names (and interface names) are capitalized: `Car one = new Opel()`. – yshavit Jan 21 '14 at 23:06
  • @meriton Might help to provide a bit more specific direction, since OP might not know what to look for. In particular, the [Java tutorial on polymorphism](http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html) is a good place to start. – yshavit Jan 21 '14 at 23:08
  • 1
    There is no difference at all between the two objects created, and the two statements will, in fact, generate *the exact same bytecode sequence*. The difference is that the `opel` class name clues the compiler to allow access to any opel-specific methods or data members, while the `car` class name allows access only to generic `car` interfaces. – Hot Licks Jan 21 '14 at 23:15
  • Thanks guys! I will also read all the links you posted to tutorials, but as is seemes to be, for some reason it is hard for me to understand the oficial java tutorials. It becomes clearer now. So, in two i can use only methods that are in opel, but in one i can use both methods of opel and car? I will also read something about standart Java convantions! – lxknvlk Jan 22 '14 at 00:13
  • @lxknvlk In `two` you can use methods that are in `opel` and `car`; in `one` you can use only methods of `car`. – ajb Jan 22 '14 at 00:30
  • @yshavit would it be proper to make the class car - abstract? – lxknvlk Jan 22 '14 at 02:21
  • @lxknvlk That depends on a whole lot of things -- it's a bit too broad a question to be answered as a comment to this question. If you can narrow down the question a bit, it may be suitable for a StackOverflow question. Otherwise, your best bet is probably to get something working however you can, and then ask for improvement suggestions on http://codereview.stackexchange.com (on a trimmed-down, "distilled" version of your code -- big code dumps are hard to read.) – yshavit Jan 22 '14 at 02:51

3 Answers3

3

You could reassign one to an object of some other subclass of car:

one = new Ford(...);

But you can't reassign two like that, since it's restricted to being an opel.

If m is a method that's defined in the opel class but not the car class, then the compiler will give you an error if you do this:

one.m();

But this is OK:

two.m();

since it knows two is restricted to being an opel, so it knows that method m will exist.

Generally, you want to declare your variables to be the broadest type possible. That is, if you're only going to use methods in car, then declare it with type car (like you did with one), because you're telling the reader that the algorithm only needs to know that one is a car, it doesn't need to know what kind of car it is.

More: It's necessary to understand that a variable has both a compile-time type and a runtime type. The compiler sees one as a car, because it doesn't know what kind of car the variable will be at any given time. But the runtime type of both will be opel. If you have a method mm that is defined for car, and then overridden for opel, one.mm() and two.mm() will both call the same method. Once the compiler looks at the compile-time type and decides the call is legal, then which one gets called when the program is run depends on the runtime type.

ajb
  • 31,309
  • 3
  • 58
  • 84
  • So, does it mean that one is a car object, but two is an opel object? Also, if i pass some parameters, for example one(1), will this go to the car class, and two(1) parameter will go to the opel class? – lxknvlk Jan 22 '14 at 00:19
  • @lxknvlk I've added to my answer, hopefully that will help with your first question. I have no idea what your second question means. – ajb Jan 22 '14 at 00:28
  • I will try to explain my question. For example, in class `opel` i have a method `m(int i)` that prints out `"opel" + i`. And in class `car` i have a method `m(int i)` that prints out `"car" + i`. If i then do this `one.m(1)` and `two.m(2)`. Will it print `car1 opel2`? – lxknvlk Jan 22 '14 at 00:41
  • @lxknvlk No, it will print `opel1 opel2`. The parameter value never affects which method is called. – ajb Jan 22 '14 at 00:56
0

Well in your example there isn't any difference, variables one and two are referencing two separate instances of the class 'opel'. Variable titled 'one' could have been assigned to any other object which extends car, or car itself, where variable two can only be assigned to a object of type opel.

One reason you might use the base class to declare the variable without assigning it could be because you don't know what specific class will be used until runtime, say

private Car buildCar(string brandOfCar){

    Car car;

    switch (brandOfCar){
        case "Honda": car = new Honda();
            break;
        case "Opel": car = new Opel();
            break;
     }

     return car;
}

You could also create an array of Car, and fill it with a mix of objects which extend Car, where if you created an array of Opel, it could only contain Opels.

That said, cars are not good examples, as the brand of the car should probably simply be a property of the class Car, not an actual class which extends car.

fpsColton
  • 873
  • 5
  • 11
0
public class Opel extends Car{

    private Car car;
    private String TypeofCar;

    public Opel(){
        TypeofCar="Honda";        
    }

    public void display(){
        System.out.println("Car :" + TypeofCar);
    }
Bosko Mijin
  • 3,287
  • 3
  • 32
  • 45