Understanding the Difference Between a Class Extension and a Class Import

I saw several threads that define a class extension as a way for a personalized class to inherit the methods of the class to which it extends. When you import a class and instantiate this class, you have access to its methods, someone can explain to me how the extension of the class to provide these 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 expand, you actually turn your personalized class into a standardized class with just a different name. I know that I am wrong, but the answers I read did not help me fundamentally understand the difference.

+4
source share
7 answers

Importing and expanding are two different things.

Import

Classes are organized into packages that provide a namespace that avoids name conflicts. Import allows you to use a class in your code without namespace information.

Import is optional. You never need to import anything if you always use the fully qualified class name, but this makes it difficult to read your code.

If you want to create a list of objects Calendar, for example, you or import java.util.List, java.util.ArrayListand java.util.Calendarand use:

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

Or import nothing and use:

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

. , , . . :

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

Java, , . , extends. Bus extends Vehicle, , Bus Vehicle. , , . , :

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

Bus , Bus Vehicle.

parkingLot.park(new Bus());

drive(), turn() stop() Bus. .

, . , , , ( , ). A Car Motor, , turnOn(), drive().

Java .

+8

( bad:/). , Person.

public Person 
{
    int age;
    string name;
}

, Person, .

public SoftwareDeveloper extends Person 
{
    string codingLanguage;
}

SoftwareDeveloper :

public static void main () 
{
    SoftwareDeveloper developer = new SoftwareDeveloper();
    System.print.out(developer.name);  
}

"", Person in SoftwareDevelopers . , :

public SoftwareDeveloper
{
    public Person person;
    string codingLanguage;

    public SoftwareDeveloper(){
         person = new Person();
    }
}

public static void main () 
{
    SoftwareDeveloper developer = new SoftwareDeveloper();
    System.print.out(developer.person.name);  
}
+5

, , , .

, , , . .

:

- - -

- - - - -

, 5 , apoun. 5 , , , . , . , .

, ... ... , .

, , .

+2

, "" ; , . Java (.. Object ). , , , , .

0

. import , , . , , X, , com.somepackage.X.

. ( ), , : .

API Collection java.util, java.util.AbstractList , , - java. util.ArrayList java.util.LinkedList.

0

.

, String.

public class DBupdate {
public String StrVar = "Hello";
    ...
    public void doUpdate(String expression) {
        try {
            connect();
            runExp(expression);
            disconnect();
        } catch ...
    }
}

. -

log(new DBupdate.StrVar);
String myExp = "UPDATE ..."; // SQL
new DBupdate.doUpdate(myExp);

.

log(StrVar);
String myExp = "UPDATE ..."; // SQL
doUpdate(myExp);

doUpdate() StrVar . , ( ) ().

( /) - log4j. . "log" , .

java.lang.Thread. , , java.lang.Thread "start()". ( run(), , ...)

0

, Statement Statement .

java , , - , .calss.

Extends- Java, , , . ( ), . .class, . , .. ( - http://www.javacoffeebreak.com/java104/java104.html)

0

Source: https://habr.com/ru/post/1526968/


All Articles