Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes difference in instances Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference in instances" Watch "difference in instances" New topic
Author

difference in instances

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
Hi,
Very simple question

I have 2 classes

class Parent{ Parent(){ System.out.println("Parent Constructor"); } void move(){ System.out.println("Parnet Move"); } }

class Child extends Parent{Child(){System.out.println("Child Constructor");} void run(){System.out.println("child run");}}



public class Test {


public static void main(String[] args) {
Parent p = new Child();
Child c = new Child();

//what is the difference between these 2 below statements

p.run();

c.run();

}}

What happens internally when I use parent reference which holds child object and child reference for holding the hcild object here?

Please thsi is very simple question but still I have some problem to understand the differences



Thanks
Sai


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Although p refers to a Child and actually has a run() method, it can't be called because p thinks it is only a Parent. Since Parent does not have a run() method your code will fail to compile.
[ October 03, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
praveena reddyk
Greenhorn

Joined: Sep 25, 2008
Posts: 11
What happens internally when I use parent reference which holds child object and child reference for holding the hcild object here?

Hi

Parent p = new Child();
p.run();
Here your using parent class reference to point to the sub class object during runtime so if you use p.run() it will call the run() method in child class.
But the code you have written gives compilation error because during compilation compiler sees that p is a reference of type parent and checks for the run() method but it didn't find that in parent class gives method run() undefined for parent.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38456
    
  23
Since the compiler sees the reference declared as Parent type, it won't believe the runtime object has a run() method; as Rob says, it will be a compiler error.
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
ok thanks for your valuable answers.
If I change this to below code:
what will happen interenally?

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
The output will be:

Parent move
Parent move

here Child is the subclass of Parent, so it can access the methods in parent(its superclass).
[ October 06, 2008: Message edited by: Abhi vijay ]
praveena reddyk
Greenhorn

Joined: Sep 25, 2008
Posts: 11
Hi,
Parent parent=new Child(); at this line both parent class and child class constructors executes and when parent.move() is called the method in child class since it inherits that from parent class.
Child child=new Child(); at this line again the construtors execute and child.move(); is called the method in the child class is executed.
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
your explanation is perfect...
but my question is why we need to refer child class object with parent class reference?

which line is much better to use and why?
line1:parent parent=new Child(); parent.move();
line2:Child child=new Child(); child.move();

What is the difference?


thanks
Sai

[edit]Disable smilies. CR[/edit]
[ October 06, 2008: Message edited by: Campbell Ritchie ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Polymorphism and the ability to change implementations fast.

Let's say Child isn't good enough but we can't change it. We can the create a new class BetterChild extends Parent. We then only have to change one line:

The rest of the code will work just fine, but with the BetterChild implementation.

This technique of declaring your variables as wide as possible will decrease the effort when you need to change your implementation.
Some examples:
- declare as List or if possible even Collection instead of ArrayList or Vector
- declare as Map instead of as HashMap or TreeMap
- declare as DateFormat instead of SimpleDateFormat

As you see it also applies to interfaces.
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
wonderful!

The advantages are fine :-)
There should be some disadvantages too

Can you please tell me the biggest demerits/disadvantage by following this process...

and the other thing is I guess there is problem with performance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38456
    
  23
I don't think there will be a performance issue at all.
The problem is that you have to follow the rules for inheritance:
  • You can only override accessible instance methods.
  • You must ensure that there is an "IS-A" relationship between the superclass and the subclass.
  • The overridden subclass methods are refinements of the superclass methods; they must not have a tighter precondition, nor a laxer postcondition.
  • You must follow Barbara Liskov's substitution principle (often called LSP).
  • The bit about refinement means that every time the superclass method works, the subclass method works too, so you can't declare new Exceptions or new preconditions. If you Google for the LSP you get lots of links, eg 1, 2.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19674
        
      18

    The LSP means you can't call methods defined in Child but not in Parent, like run() in your example.
    saikrishna cinux
    Ranch Hand

    Joined: Apr 16, 2005
    Posts: 689
    nice explanations but still I have last final question.

    When i use Parent parent = new Child();

    parent.move();

    when will be this object instantiation is done? in runtime or compile time?

    and if suppose if I override the method move() when it will refer to tht particular method

    and one more thing is why overloading is called as compile time polymorphism and why overriding is called as runtime polymorphism?

    thanks so much for your answers so far...
    Sai
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38456
        
      23
    "Compile time polymorphism" is probably a bad term; real polymorphism only applies to overridden instance methods and the method types are bound at run-time. Well, that's what I think and lots of people will now disagree with me!

    And whether object instantiation is done at compile time or run-time? Well, the answer should be obvious!
    saikrishna cinux
    Ranch Hand

    Joined: Apr 16, 2005
    Posts: 689
    Originally posted by Campbell Ritchie:
    "Compile time polymorphism" is probably a bad term; real polymorphism only applies to overridden instance methods and the method types are bound at run-time. Well, that's what I think and lots of people will now disagree with me!

    And whether object instantiation is done at compile time or run-time? Well, the answer should be obvious!


    Hi Campbell,
    So you say that there is no compile time polymorphism in java?
    and why do you say it is bad term?

    when you say that the overriden method types are bound at runtime in the runtime polymorphims concept.

    when what will happen in overloading ? I guess this is binding in the compile time rather than at runtime


    thanks
    Sai
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38456
        
      23
    Some people do call overloading compile-time polymorphism.
    Amit Ghorpade
    Bartender

    Joined: Jun 06, 2007
    Posts: 2716
        
        6

    Originally posted by Campbell Ritchie:
    Some people do call overloading compile-time polymorphism.


    And many people call me the genius

    Even overloading should not be called compile-time polymorphism or anything similar, its just overloading. It maybe true in C++ or other languages that which overloaded versions are called is decided at compile time, to be more precise at link time. But this is not true in Java where the overloaded version is also decided at runtime.

    Hope this helps


    SCJP, SCWCD.
    |Asking Good Questions|
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: difference in instances