What'开发者_运维问答s the difference between importing and extending a class in Java
Those are two very different things.
Importing a class, is making it so you can use that class without needing to qualify the full name in the current class you are writing.
import java.util.Scanner
// now you can use the Scanner class in your code like so:
Scanner stdin = new Scanner(System.in);
// instead of having to do
java.util.Scanner stdin = new java.util.Scanner(System.in);
Extending a class is creating a new class that is a subclass of some other class. This will allow you to add or change functionality of the class you are extending.
// this is a very contrived example
public class EmptyList extends ArrayList {
@Override
public boolean add(Object o){
return false; // will not add things to a list
}
}
Import doesn't change your program, it just allows you to write the short form of declaring a class. In your own class you can use any other class from any package within the Java library.
Lets say you want to use the Scanner class to take input from the keyboard. Instead of writing java.util.Scanner sc = new java.util.Scanner(System.in);
, you can simply write Scanner sc = new Scanner(System.in);
.
If you import the package or a package followed by the class name at the top of your class which is import java.util.*;
or import java.util.Scanner;
Extending a class is not as simple as importing a class. When you extend a class you are adding all instances (fields) and methods of the extended class into your own class. In other words, you have access to all of the fields and methods of the extended class.
Importing means that you can reference it in a non-qualified way. e.g.
import java.util.List;
List list = ...
as opposed to
java.util.List list =
Extending is completely different, and means inheriting behaviour and structure from a class
When, in your class, you reference a class that is not in the same package as your class, you need to import the other one.
When you want to use oop inheritance, you extend a class - i.e. your class has the functionality of the superclass + whatever your write in your class.
The two things are rather different, so perhaps you should create some simple programs, and see for yourself the obvious difference.
In Simple:
Import refers to Has a relationship. With keyword new.
Extend refers to IS a relationship. With keyword extends.
Please refer this link for good example.
Extending
Extending means enhancing the functionality of an existing class.
Importing
Importing means use some api as a helper to your class for desinging.
first of all ,import is use to increase the efficiency of compiler to find the correct class
the main difference ,is the import use aggregation(has-a) concept & in extends we use (is-a) relation
in has-a or import , the obj of our class is not maintain a lifetime relation with import classes we need to get the obj of particular class that we want to use in our application
but in extends the obj of our class is make a life time relation with parent class...
HAPPY if i am right other wise give right ans....
importing a class provides us to access its predefined methods.we cnn't chnge those methods but we can use them. extending a class means we can override the methods defined in the class to be inherited.
Package import is just a way to tell the classloader where to look for your classes and also differentiate between classes with the same name. Class extend tells JVM the heirarchical relationship between your classes. OO rules apply once the class is found.
Import is a optional clause that declare which class you will use in you potential class interface or enumeration. The extend says to class that she can use the functionality of that parent class.
To extend a public class you need to import it first.
精彩评论