JAVA inheritance Basic class, abstract class, Interface introduction _java

Source: Internet
Author: User
Tags inheritance
Encapsulation: is to encapsulate some attributes and methods into a class.
Inheritance: Just as subclasses inherit some of the properties and methods of the parent class.
Polymorphism: Just as a parent class has several distinct subclasses.
Here I do not explain more, the following I mainly explain an inheritance. Inheritance is a feature of OOP (object-oriented), Java only supports single inheritance (if you inherit two parent classes that have the same method, then you don't know that the parent class is inherited, 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 object class method, such as ToString (), GetClass (), wait () ... So the classes we build have a parent class.

There are generally three kinds of 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 that has an abstract method (the abstract method is that the method must be implemented by inheritance, itself defined, not implemented). An abstract class can have one or more abstract methods, which is an excess of the base class and the interface class.
Interface class: Typically called an interface, all the methods in the class are abstract methods, and the method itself is defined as not implemented.
Abstract classes and interfaces are defined by the "abstract class + Abstract classes name" by "Interface + interface name", so the methods in the interface are abstract methods, while some of the abstract classes are implemented by themselves, and some are only undefined.

definition of basic class:

Copy Code code as follows:

public class Fruit {
public void Price () {
SYSTEM.OUT.PRINTLN ("Fruit price");
}
public void weight () {
System.out.println ("fruit weight");
}
}

definition of abstract class:
Copy Code code as follows:

Public abstract class Fruit {
public void Price () {
SYSTEM.OUT.PRINTLN ("Fruit price");
}
public abstract void weight ();
}

definition of interface class:
Copy Code code as follows:

Public interface Fruit {
public void price ();
public void weight ();
}

From the above we can see that the interface is an upgraded version of the abstract class, because the methods of this class are all abstract methods, so replace abstract with interface. The method of an interface must be implemented by a subclass.
Inherited

Inherits the basic class, can inherit the method of the parent class, can also from some, also can expand. The following are classes that implement the basic classes of the interface:
Copy Code code as follows:

public class Apple extends Fruit {

}

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

To inherit an abstract class, you must implement an abstract method of an abstract class, you can modify the method of the parent class, and add methods. The following are classes that inherit abstract classes:
Copy Code code as follows:

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 of the abstract classes and additions to the interface class. The following are the classes that inherit the interface:

Copy Code code as follows:

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 way to implement the fruit interface.

Java supports only single inheritance (inheriting basic classes and abstract classes), but we can implement them using interfaces (multiple inheritance interfaces)
such as: public class Apple extends Fruit implements Fruit1, fruit2{}
Generally we inherit basic class and abstract class with extends keyword, Implement Interface class inheritance with implements keyword. In fact, inheritance is very simple, can just not understand these two keywords, when we find out that is relatively simple.

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

Here is only their own understanding, some places when the wrong words, just feel that the word better understanding, please understand. Some places are not clear because they are not the focus here, and I hope that we can understand them by other means.

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.