Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object ref types trip me up all the time

 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is some example code of what I'm goofing up on all the time:



What will be the effect of compiling and running this class Test?
Generates a Compiler error on the statement v= c
Generates runtime error on the statement v= c
Prints out:

A. Does not complie becasue the v=c assignment.

B. Generate a runtime error.

C. Prints out:

Vehicle : drive
Car : drive
Car : drive

D. Prints out:

Vehicle : drive
Car : drive
Vehicle : drive

The answer is 'C' and I put 'A'


I'm thinking the reference variable of type 'c' cannot be
assigned to a reference variable of type 'v', because 'v'
is a parent type and doesn't maybe have all the code/guts
of the sub type 'c'.

How can I get this right in my mind. I know a object doesn't forget
its type....



[HENRY: Added code tags and formatted code]
[ November 21, 2006: Message edited by: Henry Wong ]
 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, this came from "www.4Tests.com question number 15.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When you say v = c in the above code, you are doing Vehicle v = new Car(); which is perfectly legal to say. This just means that a superclass reference is pointing to a subclass object. Now when you invoke any methods using v, the method in the subclass is called if that method is overriden.....if not, the method in the superclass is called because the reference is of superclass type.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this one,

 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Digest it, digest it, digest it,,,,,.....

thank you so much

I hope this sinks in.
 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your example above.

A a = new A();
Main m = new Main();
a = m;


"a = m" is the same as " A a = new Main();"

therefore:


It will call the Main method:

hence, print out "From subclass".
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right.
 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thank you soooo much!!!
 
Wil Guo
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a little confusing, in the above example of class A and Main, if class Main has its own method as

class A{
public void aMethod(){
System.out.println("From superclass");
}
public void aMethod(String s){
System.out.println(s);
}
}


public class Main extends A {
public void aMethod(){
System.out.println("From subclass");

public void bMethod(){
System.out.println("From subclass, non-inherit method");
}
}

public class Test {
public static void main(String[] args){
A a =new A();
Main m=new Main();
a = m;
a.bMethod();//this line doesn't compile, why?
}
}
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Above,

It dosn't compile simply because, the reference type is of class A and when you say A a = new Main() and do a.bMethod(), it checks for the method in class A which is not there, so it results in a compile time error.
 
marko salonen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jothi Shankar Kumar Sankararaj:





Originally posted by donald rieck:
From your example above.

A a = new A();
Main m = new Main();
a = m;


"a = m" is the same as " A a = new Main();"

therefore:


It will call the Main method:

hence, print out "From subclass".


I really dont understand how this could be right.
Method aMethod("2") is called for instance referenced by A which is Main.
Main doesnt have aMethod(String) so superclass method is used instead. There fore outpu should be "2"
 
Andy Morris
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a previous post someone posted a good link to Polymorphism. I'd look for it and read it as it's very clear and should resolve your worries.
 
Andy Morris
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Polymorphism

There you are.
[ November 22, 2006: Message edited by: Andy Morris ]
 
Wil Guo
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jothi Shankar Kumar Sankararaj:
Hi Above,

It dosn't compile simply because, the reference type is of class A and when you say A a = new Main() and do a.bMethod(), it checks for the method in class A which is not there, so it results in a compile time error.


Jothi,
According to the polymorphism link posted, java uses later-binding and should call method in child class, in this case, should call method in Main.
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic