Clarify classes, abstract classes, interfaces, inheritance, and objects in Java at one time

Source: Internet
Author: User

 

Class is a broad concept that represents a group with a common nature. Such as: human.

Code:
// Declare a class "human"
Class human {
Private string name;
Public String getname (){
Return name;
}
Public void setname (string value ){
This. Name = value;
}
//......
}

An object refers to a specific thing. For example, Zhang San and Li Si.

Code:
Human zhangsan = new human ();

Figure:

In the figure, how do we create James and James Li ?? The previous Code does not seem to be involved yet. Well, let's take a look at the following:

Human human = new human ();

After the program is executed, the Java Virtual Machine creates a human object in the memory and assigns the reference of this object to the human variable.

Next:Human. setname ("Zhang San"); Human. setname ("Li Si ");The object (Instance) has been created!

Figure:


"Man" is a base class, and "man" is a sub-class of "man. If "Zhang San" is a "man", that is, "Zhang San" is a "man" object, then it is clear that "Zhang San" is also an object of the "man" base class.
Class Nanren extends human (string ARGs []) {
//.............
}

A method is a kind of behavior, such as smiling, sitting, and walking.

Figure:


Code:
Zhangsan. Go (); zhangsan. Xiao (); zhangsan. shuijiao (); zhangsan. qita ();

An interface only defines some methods, but does not implement these methods. It specifies the methods (capabilities) required to implement the interface class. It is just a provision.

We stipulate that people can walk, laugh, sleep, and do other things, so we use code to implement this rule.
Public interface you_are_human {
Public void go ();
Public void Xiao ();
Public void shuijiao ();
Public void qita ();
}

At this moment, Michael wants to be a person, but he only wants to have the ability to walk and laugh. First, he must declare that he is a person, that is, the previous rules on people,
Then it is up to him to decide what to do:

Public class zhangsan extends human implements you_are_human {
Public void go (){
//......
}
Public void Xiao (){
//......
}
}

When using:
You_are_human zhangsan = new zhangsan ();

Zhangsan. Go ();

Zhangsan. Xiao ();

Some people will ask, what is the difference between this and the class I defined earlier;

This is because: Java class inheritance is from top to bottom, Grandpa 1, Dad 1, Son 1, Grandpa 2, dad 2, son 2, not: grandpa 2, Son 1, is a mess like this. No, it's a joke. Therefore, class rules do not inherit the relationship, prevent chaos, and make the structure clear.
Therefore, when we use a tree structure, we use classes.

There are still many such situations in our lives. For example, if Michael is a Chinese, Michael is also an American dual identity.
Another example is Li Si, a Java engineer and a cook. What should we do ??

We can do this: specify multiple identities
Public interface zhengjian {
Interface China (){
Public void china_l (){
//.................
}
}
Interface USA (){
Public void usa_l (){
//...........
}

}

Public class zhangsan extends zhengjian implements Chna, USA {
Public void china_l (){
//......
}
Public void usa_l (){
//......
}
}

When James went to the United States, he told him to get a green card:

Zhangsan. usa_l ();

 

 

 

Well, let's take an example and you will be clear:

There are two types of HP: "Samsung" and "IBM". Generally, it is impossible for me to use HP either "Samsung" or "IBM ~~~~~~~~ Class
"Family Style" and "Office Style", but we can often buy products that combine the two styles ~~~~~~~~~~~ Interface

Chinese people say that the class is a bit "feudal hereditary", and the interface is a little "violation of common sense and incest.

After reading some related articles, you will find it easy.

The rest of the abstract class is simple. It is abstract. It is just abstract.

Abstract classes are implemented for Certain types ------ abstract classes are specific abstract methods. features include: Method Name, number of parameters, and type of parameters.

Not including: return type, parameter name, and thrown exception.

Java interfaces (abstract classes) are used to declare a New Type. Java designers should primarily use interfaces and abstract classes to couple software units with internal and external elements (Content in software engineering.

Ideally, a specific class should only implement the methods declared in the interfaces and abstract classes, and should not provide redundant methods!

Abstract classes only provide partial implementation of one class. Abstract classes can have instance variables and one or more constructors.
The concept of man is abstract enough!
Public abstract class human {...}

So are men.
Public abstract class man {...}

One thing must be clarified: interfaces cannot inherit abstract classes, and interfaces can only inherit real classes,

For example, I can define a specification (Interface) for an HP Factory Product (class), but it is difficult for me to define a product for a certain type of factory due to the innovations of various manufacturers, family HP makes it difficult to draw a conclusion (interface ).

Consider class and interface issues from the perspective of software optimization. Class class is well organized and cohesive, and is used for internal implementation of software modules (internal machinery of automobiles); interface interfaces are flexible and coupled, and used for external implementation of modules (such as buttons of automobile operation boards ). So as to realize the software design idea of strong cohesion and loose coupling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.