Java class, abstract class, interface, inheritance, and Object

Source: Internet
Author: User

Class, abstract class, interface, inheritance and object (Java)

Zookeeper --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This is not a teaching material. I sometimes look at it on the Forum. Many beginners have asked a lot of questions. These questions are something Java programmers should know, but generally they won't talk about or talk about. Therefore, I am afraid to write a graffiti article and finish what I want to say here. This is the first time that I have written a technical article. Please forgive me for the poor writing of this article.

First, let's clarify the differences between classes and objects.

A class is a broad concept that represents a group with a common nature, and an object refers to a specific thing. For example, "man" is a class that represents all people on the Earth, while "Zhang San", "Li Si", and "Einstein" are objects of each other, or they are instances of the "person" class. In Java, we can define classes and create class objects.

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

Create a class:
Human human = new human ();

 

Secondly, many people have a vague understanding of object and object references.
A reference is the handle of a program operation object, which is equivalent to a pointer in C and C ++.
As mentioned above, the object is a real thing, such as the previous Code:
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. There are two steps: first create a human object, and then assign the reference of the created object to the human variable.
If an object reference is declared, but the object is not assigned to it, the reference points to an empty object, or references an object that does not exist. If you want to access the object through this reference, a null pointer exception will be thrown, for example:
Human human;
//......
Human. setname ("James ");

The following describes the relationships between classes, abstract classes, interfaces, and inheritance.
Many attentive beginners asked such questions on the Forum:
1. The interface does not implement the method, but I can call the interface method in the program. Why? For example, connection, statement, and resultset in the Java. SQL package are all interfaces. How can we call them?
2. abstract classes cannot be instantiated, but JDK has many abstract class objects. Why? For example, system. In is an inputstream object, but inputstream is an abstract class. How can we get its object?

In any case, you should understand that both abstract methods in the abstract class and methods defined in the interface need to be called. Otherwise, these methods are meaningless.

It may not be mentioned in many books, or the readers have not noticed this point:
If a subclass inherits its base class, it indicates that this class is also a type of its base class. An object of this subclass is of the subclass type, it is also an object of its base class, and it also has the type of its class. If a class implements an interface, indicates that an object of this class is also an object of this interface.

It may not be easy to understand. It is also a subclass, base class, type, interface or something, and it is easy to confuse. In fact, for a real example, you will think it is actually very simple:
If "man" is a base class, "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.

By understanding this, we can easily understand why we can get abstract class objects: the original abstract class object is actually an object of its subclass or descendant class that has implemented the abstract method, but we use it as the base class of its abstract class. For example, "people", everyone will be "sad". When a man is sad, smoking, drinking, and crying when a woman is sad. Because different sub-classes perform different actions in "Sorrow", this action (method) is not implemented in the base class, but this method is required in the base class. Therefore, the "person" class can define an abstract method "Sorrow", and its sub-classes "man" and "woman" can implement the "Sorrow" method. However, the caller only treats the objects of men and women as one of their base class "people" and calls its "Sorrow" method.
You can try out Java. Lang. Process:
Runtime runtime = rumtime. getruntime ();
Process = rumtime.exe C ("notepad.exe ");
Class CLS = process. getclass ();
System. Out. println (CLS. getname ());
At this time, the process class name will be printed. If it is in Windows, it will be a name similar to * Win32 *, and it is a subclass of process. Because the process class is used to manage opened processes and has different implementations on different operating systems, it defines a method as an abstract method of process, specific operations can only be implemented by sub-systems corresponding to different operating systems.

Next we will talk about interfaces. We know that interfaces only define some methods, but do not implement these methods. In fact, an interface is a specification that specifies what to do to implement this interface, or specifies the required capabilities (that is, methods) of the class that implements the interface ).
Then we can compare it as follows:
A certain type of driving license requires that the person with the driver's license must be able to drive a car or a bus ". So we think this driver's license is an interface, which specifies the capabilities required to implement its class.
We can define a class driver that inherits from human and then implement the "Driver license holder" interface:
Public interface driverholder {
Public void drivercar ();
Public void driverbus ();
}
Public class driver extends human implements driverholder {
Public void drivercar (){
//......
}
Public void driverbus (){
//......
}
}

In this way, a "driver" object is also a drivreholder object. That is, a driver is also the object of a driver's license holder. In the program, we can do this:
Driverholder = new driver ();
Driverholder. drivercar ();

In this way, we will explain why "the interface has no implementation method but can get the Interface Class Object.

In this case, some people may ask: why do we need to define an interface? Why don't we directly define this method in the driver class, and then driver = new driver (); it is much easier to call its drivercar (); and driverbus () methods?

This is because Java is single-inherited and can only inherit from one class, so its type is limited to its base class or the base class of the base class. But Java can implement multiple interfaces, so that it can have multiple interface types. Just like a person, it inherits from the "spine" class, while "spine" inherits from the "animal" class. Therefore, "Michael" is an individual, he is a "spine", and of course he is also an "animal ". However, he can inherit many interfaces. For example, after obtaining a driver's license, he is a "Driver's license holder" type. He can also take the English level 6 certificate, so that he is a Level 6 certificate holder and so on.

After understanding this, let's take a look at the Java event mechanism.
The Java. AWT. Button class has an addactionlistener (actionlistener L); method. This method is passed in as an interface type: actionlisterner. In practice, we need to implement the actionlistener interface and pass in the object reference of the class that implements this interface as a parameter. In this way, the button object gets an actionlistener object, which knows that this actionlistener object has an actionreceivmed method, or that it can process Action events. When an Action event occurs, it can call
Actionreceivmed method.

For example, we generally do this:
Public class testbutton extends frame implements actionlistener {
Private button btn1 = new button ();
//......

Public testbutton (){
BTN. addactionlistener (this );
This. Add (BTN );
}

Public void actionreceivmed (actionevent e ){
 
}
}

Now let's assume that actionlistener is not an interface, but a class. Therefore, we can only inherit the actionlistener class and override the actionreceivmed method. However, Java is a single inheritance. If the class inherits the actionlistener class, it cannot inherit other classes (frame class) instead of the frame class, how to create a form and put the button in the form?

In fact, the interface is not completely designed to solve Java's single inheritance problem. To some extent, it can achieve the separation of call and implementation details.
For example, China's electricity usage specification is an interface: average voltage of 220 V, 50Hz, sin AC, Hydraulic Power Plant, thermal power plant, nuclear power plant, if a small diesel generator generates electricity according to this specification, it indicates that it has implemented the interface of this civil power source. refrigerators, televisions, washing machines, and other household appliances use these power supplies, indicates that they are calling this interface. Here, no matter where the electricity comes from, as long as it complies with the specifications of the civil power supply, the power supply also no matter what the electricity is used for, just provide power.

Let's look at the event mechanism of the button. You need to know that the button should ensure that when all action events occur, the programmer can handle them in his code, library management system, cash register system, purchase, inventory, and so on. The interface can achieve this: Find a class to implement the actionlistener interface, and let the button get the reference of the Class Object (call the addactionlistener method), so that when the action event occurs, the button creates an actionevent that contains the event information, and then calls the method of this interface object to process the event. This is the class that implements the interface. Here, button
I have no idea what the actionreceivmed method has done or what it should have known. The button is completely separated from the specific business logic. It can be applied to all occasions.

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.