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

Importing Vs Subclassing

 
John Paterson
Ranch Hand
Posts: 171
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

What is the difference between importing a class and subclassing it? Either way I gain access to the members of the 'other' class right?

regards
John
 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you dont.

Importing just tells the compiler you are going to use the package/class in your code.
Subclassing is usually done, when you need to add some functionality. Even if you subclass, you do not always gain access to members with private scope.

Check out http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html and http://docs.oracle.com/javase/tutorial/java/concepts/inheritance.html
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Importing has precisely zero effect on the generated byte code. It's just syntactic sugar to save typing and keep your code less cluttered. All it does is tell the compiler that when you write "X" you really mean "com.a.b.c.X".

Subclassing is totally unrelated to that, but it's not what you seem to think it is either. We don't subclass X to get access to members of X. (The only thing that would give us that we didn't already have would be those members which are protected, and there aren't usually that many of those anyway.) We subclass in order to specialize a type--to implement or change the behaviors or certain methods. The main purpose of inheritance is not code sharing.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic