File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assigning up the hierarchy

 
Mathew Lee
Ranch Hand
Posts: 238
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reading following lines from link
http://www.jchq.net/certkey/0501certkey.htm

did not understand it clearly.

Object references can be assigned up the hierarchy from child to base.



The following example illustrates how you can cast an object reference up the hierarchy

class Base{}

public class ObRef extends Base{
public static void main(String argv[]){
ObRef o = new ObRef();
Base b = new Base();
b=o;//This will compile OK
/*o=b; This would cause an error indicating
an explicit cast is needed to cast Base
to ObRef */

}
}




I did not understand why b=o is ok where as o=b is not ok without casting.
Any ideas, resources,sample code,links, highly appreciated. thanks in advance.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

After posting so many topics, mostly with some code snippets, you really should consider using code tags -- it makes your post much more readable.

Henry
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mathew Lee wrote:I did not understand why b=o is ok where as o=b is not ok without casting.

Because that's how inheritance works in object oriented programming.

When class A extends class B, then there is an "is a" relationship going from B to A: an instance of B is an instance of A. This will become more clear when you fill in concrete names for the classes:

Study this tutorial: Object Oriented Programming Concepts.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic