----abstract classes, interfaces in the Java language

Source: Internet
Author: User

DAY12 abstract classes and interfaces in the Java language

Abstract class and interface overview;

Yesterday we learned the Java inheritance function, this is a core knowledge point, today we inherit the basis of learning about abstract classes and interfaces of the two knowledge. What is an abstract class? How do I define an abstract class? How do I use an abstract class? These are the things we need to master.

At the same time we need to know how to define an interface, it is a very important function to define the interface in development to implement the related function. How to implement an abstract method in an interface, as these are all said in this section.


II. abstract classes and interfaces

1. Abstract class:

What is an abstract class? Classes that contain abstract methods are called abstract classes. Class that is decorated with the abstract keyword.

What is the meaning of abstract classes? Abstract class existence meaning is inherited, is the definition of a template, subclasses to implement its abstract method.

An abstract class can contain member variables, methods, constructors, initialization block inner classes, and so on.

Note: Abstract classes are not instantiated at the same time.

If the final keyword is used, it cannot be present at the same time as the abstract keyword.

The final keyword-decorated class cannot be inherited, and the final keyword-decorated method cannot be overridden, and the final keyword cannot be used in conjunction with the abstract keyword.

2. Abstract Method:

An abstract method is a method that uses the abstract keyword modification in a class to be called an abstract method.

Note: Abstract methods are not method bodies, and subclass inheritance is required to implement this abstract method.


3. Example one:

Package www.com.c3;//abstract class//definition Animal class abstract class Animal{string Color;int leg;//abstract method, no concrete method body to implement it public abs    tract void eat (); }//method One, the inherited subclass is also defined as an abstract class.    Method Two, after the ordinary subclass inherits the parent class, the subclass implements the concrete abstract method in the parent class. Class Dog extends animal{//this is the implementation of the abstract method in the parent class. We need to implement it @overridepublic void Eat () {System.out.println ("The dog is going to eat food!") ");}    }    Main class public class abstract01 {public static void main (string[] args) {Dog dog = new Dog ();d og.eat ();} }

4. Interface (interface):

What is an interface? Feel good strange like, in fact otherwise, you think, do development we are divided into front-end development and background development of two main bodies, two themes are through a number of interfaces to achieve product functionality. The front-end or background definition good one interface allows you to implement functionality directly from the interface. This saves a lot of research and development time. So, the interface is a good thing.

5, Interface: is a completely abstract out of the class. The two definitions in this class are all abstract methods, which begin with the public abstract keyword.

6, in the interface can also define some variables, but these variables are all initialized to be able to use. And it's all the "public satatic final" keyword modifiers.

7, in the interface is not able to contain the "constructor".

8. Example Two

Package www.com.c3;//defines the interface: interface studets{//defines the variable and initializes string name = "ZS";//defines the method public abstract void student (); }//Note: Between classes and classes: is an inheritance relationship,//Between a class and an interface: is the implementation relationship. Implemented by the Implements keyword. It is also necessary to implement an abstract method in the interface.    Subclasses implement abstract methods in parent classes class Am implements studets{@Overridepublic void student () {System.out.println ("name:" +name);} }//main class public class Interface01 {public static void main (string[] args) {//interface is//studets st = new Studets () that cannot be instantiated;    Am A = new am (); a.student (); }       }


The difference between the interface and the abstract class in usage:

1, the interface can only contain abstract methods, static methods and default methods, can not provide methods for ordinary methods implementation;

Abstract classes can completely contain common methods.

2, the interface can only define static constants, can not define ordinary member variables;

In an abstract class, you can define either normal member variables or static constants.

3, the interface does not contain the constructor;

Abstract classes can contain constructors, and constructors in abstract classes are not used to create objects, but rather to have their subclasses call these constructors to complete initialization operations that belong to abstract classes.

4, the interface can not include the initialization block;

Abstract classes can completely contain initialization blocks.

5. A class can have at most one direct parent class, including abstract classes;

But a class can directly implement a plurality of interfaces, the lion by multiple rescue can compensate for the shortcomings of Java single inheritance.


Iv. Concluding remarks:

Here we are finished, no relationship, if you do not understand my follow-up to see the video resources to share a little to everyone.

This article from the "Program Ape" blog, reproduced please contact the author!

----abstract classes, interfaces

in the Java language

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.