Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference in instances

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
praveena reddyk
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50245
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks for your valuable answers.
If I change this to below code:
what will happen interenally?

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50245
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Pie
    Posts: 20667
    65
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The LSP means you can't call methods defined in Child but not in Parent, like run() in your example.
     
    saikrishna cinux
    Ranch Hand
    Posts: 689
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 50245
    79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    "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
    Posts: 689
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 50245
    79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Some people do call overloading compile-time polymorphism.
     
    Amit Ghorpade
    Bartender
    Posts: 2854
    10
    Fedora Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic