• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Importing Vs Subclassing

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic