can any one explain " While we are imp开发者_JS百科orting 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 "
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
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.
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.
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.
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.
精彩评论