JAVA features and basic classes, abstract classes, interfaces

Source: Internet
Author: User

Java is an object-oriented language, Java object-oriented generally have three major characteristics: encapsulation, inheritance, polymorphism.

Encapsulation: It is the encapsulation of properties and methods into a class.

Inheritance: As subclasses inherit some properties and methods of the parent class.

Polymorphic: Just as a parent class has multiple subclasses of different characteristics.

Here I do not explain more, below I mainly explain an inheritance. Inheritance is a feature of OOP (object oriented), Java only supports the single inheritance of Tianjin Web site production company (if you inherit two parent class with the same method, then you do not know the inheritance to that parent class, so Java only supports single inheritance). Inheritance is a feature of Java, so we use the class to inherit the Objict class, so we need the method of object class, such as ToString (), GetClass (), wait () ... So the classes we build have parent classes.

There are generally three kinds in Java:

Basic class: That is, the general class (generally referred to as the class is the basic Class), is the object template, is a collection of properties and methods. You can inherit other basic classes, abstract classes, and implement interfaces.

Abstract class: A class with an abstract method (the abstract method is that the method must be implemented by inheritance, itself defined, not implemented). Abstract classes can have one or more abstract methods, which are the excesses of basic classes and interface classes.

Interface classes: Generally called interfaces, all methods in the class are abstract methods, and the methods of the class themselves are defined not to be implemented.

Abstract class and interface A by the "abstract class + abstract class name" is defined by "interface + interface name", the interface of the method is an abstract method, and the abstract class has a part of the method is implemented by itself, some definitions are not implemented.

Definition of the base class:

public class Fruit {

public void Price () {

SYSTEM.OUT.PRINTLN ("Fruit price");

}

public void weight () {

System.out.println ("fruit weight");

}

}

Definition of abstract class:

Public abstract class Fruit {

public void Price () {

SYSTEM.OUT.PRINTLN ("Fruit price");

}

public abstract void weight ();

}

Definition of the interface class:

Public interface Fruit {

public void price ();

public void weight ();

}

From the above we can see that the interface is an upgrade version of the abstract class, because this kind of Tianjin keyword optimization method is all abstract method, so the abstract replaced interface. The method of an interface must be implemented by a subclass.

Inherited

Inherit the basic class, you can inherit the method of the parent class, or you can extend it. Here is the class that implements the basic class of the interface:

public class Apple extends Fruit {

}

In this class there are two methods in the fruit base class: Price () and weight ();

Inheriting abstract classes, you must implement abstract methods of the abstract class, modify the methods of the parent class, and add methods. Here is the class that inherits the abstract class:

public class Apple extends Fruit {

@Override

public void weight () {

System.out.println ("fruit weight");

}

}

This class inherits the price () method of the class fruit and implements the weight () method of the fruit abstract class.

Inheriting an interface class (also implementing an interface), you must implement all the abstract classes of the interface class and add it. The following is the class of the inherited interface:

public class Aple implements Fruit {

@Override

public void Price () {

SYSTEM.OUT.PRINTLN ("Fruit price");

}

@Override

public void weight () {

System.out.println ("fruit weight");

}

}

This class is an abstract method that implements the fruit interface.

Java only supports single inheritance (inheriting basic classes and abstract classes), but we can implement them with interfaces (multiple inheritance interfaces)

such as: public class Apple extends Fruit implements Fruit1, fruit2{}

Generally we inherit the basic class and abstract class with the extends keyword, implement the inheritance of the interface class with the Implements keyword. In fact, inheritance is very simple, can be not clear these two keywords, when we find out is relatively simple.

Interfaces can also inherit interfaces such as: public interface Fruit1 extends Fruit {} This is a multiple inheritance in an interface, and the same is the same for abstract and basic classes. If we add the final adornment to the base class, we also define that the class is not inherited and that the class cannot be a parent class. At the same time, the basic class method can be modified with public, private, proptected, and final to prevent inheriting the method.

Here is just their own understanding, some places when the use of the word is inappropriate, just feel that the word better understanding, please understand. Some places because not the emphasis here, did not speak clearly, hoped everybody through other ways understand.

JAVA features and basic classes, abstract classes, interfaces

Related Article

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.