aspose file tools*
The moose likes Beginning Java and the fly likes please help out about object reference... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "please help out about object reference..." Watch "please help out about object reference..." New topic
Author

please help out about object reference...

shankar dengi
Greenhorn

Joined: Aug 09, 2008
Posts: 10
class Display
{
void display()
{
System.out.println("hi");
}
}
class Disp1 extends Display
{
void disp1()
{
System.out.println("hi1");
}
}
class Dem

{
public static void main(String [] arg)
{
Display b=new Disp1();//this compile fine
Disp1 a=new Display();//y dis wont compile?

}
}
in above "b" reference know about only members of Display in Disp1 object so it compiles fine even reference "a" know about both den why it give compile error?
please help me...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

shankar dengi wrote:
Display b=new Disp1();//this compile fine
Disp1 a=new Display();//y dis wont compile?


A Disp1 object IS-A Display. The reverse is not necessary true. And in this example, is definitely not true.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
shankar dengi
Greenhorn

Joined: Aug 09, 2008
Posts: 10
Henry Wong wrote:
shankar dengi wrote:
Display b=new Disp1();//this compile fine
Disp1 a=new Display();//y dis wont compile?


A Disp1 object IS-A Display. The reverse is not necessary true. And in this example, is definitely not true.

Henry
I dint understood please explain me in detail..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

Let's replace Display and Disp1 with Animal and Cat.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
shankar dengi
Greenhorn

Joined: Aug 09, 2008
Posts: 10
Rob Prime wrote:Let's replace Display and Disp1 with Animal and Cat.
ya but how it works internally?
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
From a practical sense, if the compiler allowed you to assign a reference to a Display instance to a Disp1 reference variable as in:
you could call the method a.disp1(), which is in the Disp1 class - not the Display class. But the instance you created is a Display. So at runtime, the JVM would at best throw an exception and at worst, crash.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please help out about object reference...