wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unable to understand  . help Needed (inheritance) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unable to understand  . help Needed (inheritance)" Watch "Unable to understand  . help Needed (inheritance)" New topic
Author

Unable to understand . help Needed (inheritance)

Anshul Mathur
Greenhorn

Joined: Nov 07, 2003
Posts: 22
hello all

I have a problem with the following code unable to understand the concept
why

first f = new second(); // Works Fine
second s = new first(); // Does not work gives Error

Thanks
Anshul Mathur


Code follows


[ May 23, 2004: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You know second isa first because second extends first.

Is first necessarily a second?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Anshul Mathur
Greenhorn

Joined: Nov 07, 2003
Posts: 22
Thanks.
Voggy Horla
Greenhorn

Joined: Dec 23, 2003
Posts: 13
i always determine by this
A = new B =>is that capatabled with that
B instanceof A
that might be good to you!
Wendal Park
Ranch Hand

Joined: Aug 28, 2001
Posts: 39
Or you can read it this way:

class B extends A { ... } => Read as "Class B is an A" ...
But you should also know A may not be a B.

Those "is-a" and "has-a" relationship would help your understanding ...


MSc, BSc, SCJP 1.4<br /> <img src="graemlins/banghead.gif" border="0" alt="[banghead]" /> SCBCD
Preetham Chandrasekhar
Ranch Hand

Joined: Nov 05, 2003
Posts: 98
second is a first....since second extends first....
but .....
obviously
first is NOT a second....so your second statement wont work
Preetham
SCJP 1.4


"In theory, there is no difference between theory and practice. But, in practice, there is."<br /> - Jan L.A. van de Snepscheut
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hello Anshul. I'd like to try to explain the concept.

second s = new first(); // Does not work gives Error

s is the name of a variable. A variable has a type and a value.

The type of the variable s is second.

The value of the variable s is a reference to an object. The variable s holds a reference to an object.

The object also has a type, which might be different from the type of the variable. The type of the object must be ***the same as or a subtype of*** the variable. Why?

class first { int x; void m() {} }
class second extends first { int y; void n() {} }

f.x and f.m() make sense if f refers to an object of type first or second. Both objects have members x and m().

s.y and s.n() make sense only if s refers to an object of type second, not type first. Only an object of type second has members y and n().
[ May 25, 2004: Message edited by: Marlene Miller ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to understand . help Needed (inheritance)
 
Similar Threads
Package does not exist error..
unable to understand "overriding"
Doubt from SCJP mock test
JQuest: String & StringBuffer
Simple question