Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Difference between extending and importing a class

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Difference between extending and importing a class

    Let me know if im right with the difference...

    Importing a class allows us to use the methods of another class...

    But if we wanted to use another classes properties (attributes and methods) in an object of our class we woul have to extend it.

    So basically, importing allows us to use other class methods, extending actually gives our class its properties so that objects of our class can make use of them.

    Right?

    Thanks.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Difference between extending and importing a class

    Not exactly. Importing doesn't actually do anything. It's just a shortcut. For example, I could do something like this:

    javax.swing.JPanel panel = new javax.swing.JPanel()

    or I could do this:

    import javax.swing.JPanel;
    //...
    JPanel panel = new JPanel();

    The two pieces of code are identical. One is just easier to read and write.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Difference between extending and importing a class

    I didnt quite get how that is related to my question (it obviously is, but its me, in that i dont understand).

    Am I right in saying though that importing a class allows us to use its methods and extending a class actually gives our class its properties, so that if we create an object from our class it would have the attributes/methods from the class which we extended?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Difference between extending and importing a class

    Importing tells the compiler where to find the class to get its definition, variables, methods, etc...once imported, you have the ability to use the class in whatever way you wish (through instantiation, inheritance, etc...)

    Extending a class makes the child class inherit the variables/methods/etc... of the parent class (some of which may be hidden through private variables but nonetheless are there). This allows child classes to inherit and modify the behavior of the parent class.

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Difference between extending and importing a class

    Im failing to see the differnce. Is it that when we import a class we can use its properties in our code, but if we were to create an object of our class then the object would NOT have the properties of the imported class, BUT if we extended a class into our class then if we were to create an object of our class it would have the properties of the extended class?
    Last edited by TP-Oreilly; December 2nd, 2011 at 08:13 PM.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Difference between extending and importing a class

    Quote Originally Posted by TP-Oreilly View Post
    Im failing to see the differnce. Is it that when we import a class we can use its properties in our code, but if we were to create an object of our class then the object would NOT have the properties of the imported class, BUT if we extended a class into our class then if we were to create an object of our class it would have the properties of the extended class?
    I really suggest you work through the basic tutorials, very carefully, as this stuff is explained in detail there.

    But the difference is, as I have said, that importing doesn't DO anything. It simply tells the compiler where to look for class names that can't be found in the immediate scope. It doesn't make them available- they are always available by using their full names including their package (javax.swing.JPanel instead of JPanel). Importing simply allows you to use only the class name instead of the full name. There are other complications such as static importing, but I don't think your question is about that.

    So, importing simply tells the compiler where to look for classes you use in your code. Extending a class means that your class IS AN instance of that class.

    If you are still confused, I suggest going through the basic tutorials.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Difference between extending and importing a class

    Thank you.

  8. #8
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: Difference between extending and importing a class

    Im new also but I can try and tell you what I learned, although you might know more than me i'm not really sure if your going beyond what I think your doing...

    When you import a class it's like your gaining extra shortcuts to already created set of codes like Jbuttons, or other pieces of codes. This really has nothing to do with inheritance of others classes or has nothing to do with other classes... But not to say this isn't necessary for all scripts...

    I'm pretty sure you can check all the Imports you can do on Javas API, but honestly to you and me it probably won't make sense to us... Doesn't for me yet. Work on Inheritance don't worry about imports so much until later...


    When you inheritance a class its like the guy said above, you gain the values and methods of the other class...


    Sorry if I totally killed what your trying to find. Like I said i'm just learning too, and I "thought" I understood what you were wondering.
    Last edited by Emperor_Xyn; December 5th, 2011 at 05:47 PM.

  9. #9
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Difference between extending and importing a class

    Thank you, you all have helped!

Similar Threads

  1. Extending a Servlet and sharing HttpServletRequest
    By mastervh in forum Java Servlet
    Replies: 6
    Last Post: October 12th, 2011, 03:51 AM
  2. Assigning a Value to a Class extending a Superclass
    By ubermoe in forum Object Oriented Programming
    Replies: 9
    Last Post: September 22nd, 2011, 11:17 AM
  3. class extending
    By Reem in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 18th, 2010, 01:49 AM
  4. importing class files :S
    By b3ard in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 2nd, 2010, 03:45 AM
  5. adding get mothods to a class extending thread
    By aliaa2a in forum Object Oriented Programming
    Replies: 6
    Last Post: August 3rd, 2009, 06:41 AM