javaoopinheritanceimport

How are imports and extends different in Java


While we are importing a package we can use all its public functions.

Then what is the use of extending a class? I'm studying OOP languages and I didn't get the answer to this question.


Solution

  • 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