• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Doubt in Inheritance

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,
This my example program.
It may be silly doubt, please tolerate my ignorance

assigned subclass object to Baseclass. means it should have access to msubclass methods also..right.. This is my opinion.

While compiling it gives cannot resolve symbol.

Please explain about this

[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ July 13, 2004: Message edited by: Dirk Schreckmann ]
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only methods in the class A can be called on an instance of type A (variable type, not the type of the object it references). There is no method methodC() (or methodD()) in A, so it won't compile.

Charlie
 
Mike Jeya
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fine,

But, Why do we use these kind of assigning?

B b= new B();

A a =b;
 
Mike Jeya
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


How it is overriding in this case?

it's calling subclass methods?

<b>what is the difference between the both?(this and whatever I sent earlier)</b>

[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ July 13, 2004: Message edited by: Dirk Schreckmann ]
 
Charlie Goth
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike_Jeya:
Fine,

But, Why do we use these kind of assigning?

B b= new B();

A a =b;



In the example it's just to confuse (and ultimately teach) people.
 
Charlie Goth
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike_Jeya:
[QB]
How it is overriding in this case? - It isn't, its just adding new methods in the sub-class.

it's calling subclass methods? - Correct, without being over-ridden, the methods are inherited as they were in the super-class.

what is the difference between the both?(this and whatever I sent earlier)

The second example has calls to methodC and methodD removed, the methods being called now DO exist, so there is no compiler error.
QB]

 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike_Jeya,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Replacing the underscore character in your display name with a space would satisfy our simple rule.

Thanks Pardner! Hope to see you 'round the Ranch!
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Mike_Jeya:



How it is overriding in this case?

it's calling subclass methods?

what is the difference between the both?(this and whatever I sent earlier)

Mike, See the buttons below the box where you type your posts? The "code" button makes your code more readable. The "bold" button will make your text darker (which the html 'b' tags will not do).

Thanks.
 
Mike Jeya
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) B b= new B();
A a= b;
a.methodA();
a.methodB();
a.methodC();
a.methodD();

2)B b= new B();
A a= b;
a.methodA();
a.methodB();


In these both codes, I assigned subcalss object b to Baseclass

if the methods' names are differnt, it gives compile error (1 st code)

if methods' names are same , a accesses b methods

Explain me please..

Please bear my ignorance
[ July 08, 2004: Message edited by: Mike Jeya ]
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the edit button up at the top of your post...

Also, once you are using an object of type A, it no longer has methods C D because it is an A object. It's like if you did this:

The Object class does not have a concat method, so calling concat on it will cause an error. It doesn't make sense for it to have a concat method because it is no longer a string. However, if you cast 'o' back to a String, it will still hold "ham" and you may then call the concat method.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
In the following program when i try to compile it,it invokes the function in the sub class but variable from the super class.For the function,we are supercasting the subclass instance to that of superclass.What about the variable?Why is it calling from the superclass and not from sub class?




help me out soon.

Thanks in advance,
NaliniReddy.

[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ July 13, 2004: Message edited by: Dirk Schreckmann ]
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a great campfire story that might help you understand these concepts: "How My Dog Learned Polymorphism"

Darin Naird: Also, once you are using an object of type A, it no longer has methods C D because it is an A object.

This is not quite right. Once an object is created, it will always be the same kind of object (if you create a String, it will always be a String, even if you reference it as an Object).



As you see from the above code, even though you can refer to an object in a different way, this does not change the object's actual class; it's still the same type of object. It's kind of like using different colored lenses to look at something. If you had a red and blue ball, the ball would look different if you looked at it through a filtered lens that would only let blue through than it would with a filtered lens that only let red through. If you used a clear lens, then you'd see all the colors of the ball. Yet all this time, the ball was still the same ball and it really retained all its colors.

This is the same thing that happens when you assign a different kind of reference to an object. You are merely changing the way you see the object, you are not changing the object itself.

Hope this makes sense.
[ July 13, 2004: Message edited by: Junilu Lacar ]
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic