Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the difference between extending a class and importing a class [duplicate]

I have seen several threads that define extending a class as a way for a personalized class to inherit the methods of the class that it is extended to. When you import a class and create an instance of that class you have access to its methods, can someone please explain to me how extending a class to provide those methods to your own class is effectively different, in other words, the only difference I see is that when you import you create an instance of a standardized class, and when you extend you effectively turn your personalized class into the standardized class only with a different name. I am aware I am wrong, but the answers I have read have failed to help me fundamentally understand the difference.

like image 269
user3310769 Avatar asked Dec 06 '22 03:12

user3310769


1 Answers

Importing and extending are two very different things.

Importing

Classes are organized in packages, which provide a namespace facility that avoids name conflicts. Importing allows you to use the class in your code without the namespace information.

Importing is optional. You never have to import anything if you always use the fully qualified name of the class, but that makes your code hard to read.

If you want to make a list of Calendar objects, for example, you either import java.util.List, java.util.ArrayList and java.util.Calendar and use:

List<Calendar> array = new ArrayList<>();

Or import nothing and use:

java.util.List<java.util.Calendar> array = new java.util.ArrayList<>();

Sometimes you have two classes with the same name in different packages. In that case, if you use both of them in your code you can't import both. You will have to refer to one of them by their fully qualified name. For example:

List<java.awt.List> array; // you have to import java.util.List, but can't also import java.awt.List

Extending

When you extend in Java you are saying that the subclass is a type of the original class. That's the most important aspect you have to be aware of when using extends. Is you say Bus extends Vehicle you are saying that Bus is a Vehicle. You not only inherit all the non-private methods and fields of the superclass, but also can use the subclass anywhere you could legally use the superclass. For example, if you have this method:

public park(Vehicle v) {
   v.drive();
   v.turn(Direction.LEFT);
   v.stop();
}

you could pass a Bus as an argument, because Bus is a Vehicle.

parkingLot.park(new Bus());

and the drive(), turn() and stop() methods will be called in the Bus. That is polymorphism.

Although you inherit methods, inheritance is not the best way to reuse code. Most of the time when you need to reuse code you can do it by using composition (making your class have a reference to another class, instead of being one). A Car shouldn't extend Motor because a car is not a motor, but it could have a motor and delegate a call to the motor's turnOn() method when the car's drive() method is called.

You can also have polymorphism without inheritance in Java using interfaces.

like image 108
helderdarocha Avatar answered Jan 03 '23 02:01

helderdarocha