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.
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:
or I could do this:
Code java:
import javax.swing.JPanel;
//...
JPanel panel = new JPanel();
The two pieces of code are identical. One is just easier to read and write.
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?
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.
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?
Re: Difference between extending and importing a class
Quote:
Originally Posted by
TP-Oreilly
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.
Re: Difference between extending and importing a class
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.
Re: Difference between extending and importing a class
Thank you, you all have helped! :)