views:

89

answers:

5

can any one explain " While we are importing a package we can use its all public functions, Then what is the use of extending a class ?" " I m studying OOP Langusges for 3 years and i didnt get ans to this question "

A: 

In Java, extending a class lets you substitute for it, as well as use its protected members. Protected methods can not be called by arbitrary classes in other packages.

Matthew Flaschen
OK , so you are saying that extend is only for accessing protected methods?
new_programmer
Extending a class lets you access protected methods, but you should think of that as a tool for effective substitution.
Matthew Flaschen
+3  A: 

Extending a class if for inheritance, an OOP mechanism, it has nothing to do with importing, when you make an import you actually import all the classes regarding the package imported, unless you import specific classes of the package.

You have access to this classes and their public members or functions or even protected if the class where you are referring to them is in the same package. Packages and importing let's you organize your software project and have somewhat modularity if i can use the concept here... at a class-level

Meanwhile, inheritance as an OOP mechanism let's you organize you classes but this time in their relationship, for a lot of purposes, like reusing members and functions, for code readability and why not syntactic-sugar

Inheritance and the rest of the OOP mechanisms, allow you code in such a way you objects and code semantic are similar like the real life, and produce code in a more human-readable way

hope this helps

jgemedina
Ok, thank you.What should i do , if i want to use two protected methods to a single class from two different classes.
new_programmer
it really depends on the programming language you are using, if using C++ you can take advantage of multiple, inheritance if using c#, java for example you should find a way around since these languages do not support multiple inheritance, perhaps use Interfaces instead.Base classes are for inheriting purposes, interfaces are for implementing specific behaviors to classes
jgemedina
Could you tell me you scenario for the problem, maybe a could tell you a posible object design solution.
jgemedina
+3  A: 

Two separate things.

When you import a package, as in importa java.util.*, you are importing all of the classes in it. That means that you instantiate (when possible) all of the clases in there, and access their interfaces (their public methods). In other words, you can use its classes.

When you extend (inheritance is the key word) a class, you can redefine its behaviour. A class behaviour its implemented through its methods. By extending them you can keep a method signature, thats its accessor (public,protected,etc), return type (void,int , someObject, etc) and its parameters, but make it act completely different. Also, you can add new methods to a class through inheritance. That is, you write your own class, and inherit all of the (public /protected) method of the upper class.

Inheritance relationships should satisfy the is a relationship. Here's an example of what inheritance is.

Tom
new_programmer this a good example too, you should follow the links
jgemedina
+1  A: 

Imports and extends provide different functions. By importing a package you have the ability to utilize any public class and public method within that package. Extending a class provides a much closer relationship to the class in question. You will be able to access protected members, as well as override members to modify or improve their functionality. The extending class is also considered a type of the original class.

For example, assume if I have a Finance package that contains a EuropeanBanker class. This class has public methods that Add, Subtract, and report the balance of Euros and Pounds. Let's assume that these operations are very complicated to implement (exchange rates, transaction fees, whatever). By importing the Finance package I can utilize the Banker class to keep track of any Euros that I have. However, I will not be able to modify the Banker class. I cannot, for example, use the Banker class to keep track of dollars, pesos, or yen.

But if I extend the EuropeanBanker class to create a UniversalBanker, for example, then I have a much deeper access to the class. I could add methods to allow transactions in dollars, yen, pesos, or any other currency. This could all be done leveraging the functionality that already exists in EuropeanBanker. I may be able to take advantage of protected methods for currency exchange rates, transaction fees, etc.

Therefore, importing a package I can utilize existing functionality. But by extending a class I can add new functionality or override existing functionality.

sgriffinusa
+1  A: 

While you import a class from a package,you can use it through instances of it,but if you do extend a class,you will have access to the members of the class through 'this',and ,'super' keywords.

Jinjavajin