File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Inheritance

 
Mike Jeya
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Charlie Goth
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Darin Niard
Ranch Hand
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Nalini Reddy
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic