aspose file tools*
The moose likes Beginning Java and the fly likes Doubt in Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in Inheritance" Watch "Doubt in Inheritance" New topic
Author

Doubt in Inheritance

Mike Jeya
Ranch Hand

Joined: Apr 15, 2004
Posts: 54
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 ]
Charlie Goth
Ranch Hand

Joined: Feb 26, 2004
Posts: 60
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


SCJP (77%)
Mike Jeya
Ranch Hand

Joined: Apr 15, 2004
Posts: 54
Fine,

But, Why do we use these kind of assigning?

B b= new B();

A a =b;
Mike Jeya
Ranch Hand

Joined: Apr 15, 2004
Posts: 54


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

Joined: Feb 26, 2004
Posts: 60
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

Joined: Feb 26, 2004
Posts: 60
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]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Mike Jeya
Ranch Hand

Joined: Apr 15, 2004
Posts: 54
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 ]
Darin Niard
Ranch Hand

Joined: Jun 08, 2004
Posts: 118
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.
Nalini Reddy
Greenhorn

Joined: Jul 01, 2004
Posts: 18
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 ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5019
    
    8

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 ]

Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Inheritance