This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 18101
    
  39

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: 19538
    
  16

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: please help out about object reference...
 
Similar Threads
Inner Classes Help!!!
please tel me how the object reference get to know that object is that type?
Overriding/overloading
suggestions required for J2ME
regarding protected scope