2

I'm new at learning Java can anyone explain me the execution flow of the following code? I'm quite confused with the output.

This is the code:

public class MainClass {
    public static void main(String[] args) {
        car c = new car();
        vehicle v = c;
        /* I am unable to understand what's happening while printing the values using the objects of the different classes*/

        System.out.println("|" + v.getModelName()
                + "|" + c.getModelName()
                + "|" + v.getRegNo() + "|" + c.getRegNo() + "|");
    }
}

class vehicle {
    public static String getModelName() {
        return "Volvo";
    }

    public long getRegNo() {
        return 12345;
    }
}

class car extends vehicle {
    public static String getModelName() {
        return "Toyota";
    }

    @Override
    public long getRegNo() {
        return 54321;
    }
}
fgb
  • 18,439
  • 2
  • 38
  • 52
Konark
  • 59
  • 6
  • 1
    Class,Interface name should always start with Capital Letter. – GOXR3PLUS Sep 04 '16 at 18:16
  • There is not much flow to explain. What do you mean "flow"? Do you mean that you want someone to explain what a class is, what a method is and what it means to invoke the method of a class? – Warren P Sep 04 '16 at 18:18
  • 2
    In your example, a car "is-a-kind-of-vehicle". Consequently, "car" can use as much of "vehicle's" behavior as it wants - or can change as much as it wants. For example, "car" can change ("override") getRegNo(). Google for [polymorphism](https://en.wikipedia.org/wiki/Polymorphism_(computer_science)) – paulsm4 Sep 04 '16 at 18:19
  • 1
    What output did you get, and what output did you expect? – Makoto Sep 04 '16 at 18:26
  • The output comes as |Volvo|Toyota|54321|54321| But i was expecting it ti be |Toyota|Toyota|54321|54321| .I am actually new and trying hard to learn programming in best way possible .I am confused ,why am i getting volvo rather than toyota – Konark Sep 04 '16 at 18:30
  • So what did you expect it to come out as? Keep in mind that `v` and `c` are sharing the same instance. – Makoto Sep 04 '16 at 18:31
  • 2
    You shouldn't call `static` methods on instances; you should call them through the class name. The compiler also warns you against doing so. Note also that `static` methods are not inherited. – Mick Mnemonic Sep 04 '16 at 18:31
  • @MickMnemonic yes sir i know that,hence i used suppressed warnings,i wanted to understand inheritance with respect to objects of static methods\ – Konark Sep 04 '16 at 18:34
  • 1
    @GoXR3Plus thank you sir i'll make it as a habit ..Thank you – Konark Sep 04 '16 at 18:36
  • 2
    To be precise, `static` methods are also inherited, but they cannot be overridden by a subclass; they can only be _hidden_ (but that's a bit irrelevant here). A good lesson to learn here is that you shouldn't disable compiler warnings. – Mick Mnemonic Sep 04 '16 at 18:41

1 Answers1

3

Object creation

  1. You are creating car instance ( new car())
  2. Add new object pointer to variable c
  3. Copy content of variable c to variable vehicle ( which point to car object)

Method call flow

When you are call static function on object it will not apply inheritance rules, so in call to v.getModelName() Java Virtual Machine call method in class vehicle.

But when you are call car() object with vehicle pointer (v variable) getRegNo method of class vehicle will call and also when you are using car pointer (c variable) getRegNo method of class vehicle will call.

edite suggestion form comment:

This ability called "Polymorphism": here you can find good tutorial. "Polymorphism" is definitely as important a concept as "inheritance" and "encapsulation'.

Community
  • 1
  • 1
Esterlinkof
  • 1,444
  • 3
  • 22
  • 27
  • 1
    Thank you for your time,effort and clear explanation.Seems like now i have understood the concept of static members and new things about inheritance...Thank you very much – Konark Sep 04 '16 at 18:51
  • 1
    I'm glad you like it :) – Esterlinkof Sep 04 '16 at 18:52
  • 2
    The ability for "someClass.getRegNo()" to do one thing, and a different "someOtherClass.getRegNo()" to do some other thing, is called ["Polymorphism"](http://www.tutorialspoint.com/java/java_polymorphism.htm) : https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html. "Polymorphism" is definitely as important a concept as ["inheritance"](http://www.tutorialspoint.com/java/java_inheritance.htm) and ["encapsulation'](http://www.tutorialspoint.com/java/java_encapsulation.htm) – paulsm4 Sep 04 '16 at 20:17
  • @paulsm4 thank you, I added your comment to my answer. – Esterlinkof Sep 04 '16 at 21:14