2.35 Java Basics Summary ① abstract ② Interface ③ design abstract class and interface principles ④ Interface and abstract class differences

Source: Internet
Author: User

Java Basics Summary ① abstract ② Interface ③ Design the principles of abstract classes and interfaces ④ interface and abstract class differences

I. Abstract abstraction
Function: Cannot produce object, act as parent class, force subclass to implement overriding method correctly
The only change that can be compared to a class is that it cannot produce objects, others include constructs, attributes, and so on.
Any class that has an abstract method becomes an abstract class.

Abstract method public abstract A ();
The ① method is abstract, and the class is abstract;
② subclasses must override abstract methods, unless subclasses are also abstract classes

Abstract classes can have no abstract methods, but they are not generally designed


Second, interface interface

Interfaces are also a reference data type for Java (Java Reference data types include: classes, arrays, interfaces)
Role: Allow classes that do not inherit relationships to share behavior
Naming is usually followed by a able, which means the ability to have what.
① property: Can only be public, static, constant property
It's not written, it's the default.
int d = 10;//public static final int d = 10;
② method: can only be public abstract method (JDK1.8 previously, 1.1-1.7)
Default: Public abstract
Class implementation interface: Implements interface, there is no way to implement the interface, can only be abstract class

The reference to an interface can point to any one of the implementation class objects and see only the behavior or properties defined in the interface

public void Service (serviceable Ser) {
Ser.changoil ();
Ser.turnupenginee ();
}
An interface can inherit an interface and can inherit multiple, because the interface is not a class and is also a reference to that data type
Public interface C extends Interb, intera{
}

Iii. principles of design and design interfaces
Innate behavior is inherent in the parent class method (parent Class)
Secondary methods are written in the interface Optional (interface)

Design interface principle: Minimize the interface principle, can be a variety of combinations
Using interface principles: using interfaces to implement multiple inheritance, using interfaces to add functionality to external classes

Iv. differences between interfaces and abstract classes ① syntax ② design

① syntax

Interface
Definition: Keyword: interface. Content: ① public static constant property ② public abstract method
Use: Class implements interface implement multiple implementations. Interface inheritance interface, multiple inheritance

Abstract class
Definition: Keyword: abstract class. Content: Any property
Use: class inherits extends abstract class, single inheritance

② Design
Subordinate behavior written in the interface, as far as possible to design a small interface
Innate behavior written in abstract class inside

2.35 Java Basics Summary ① abstract ② Interface ③ design abstract class and interface principles ④ Interface and abstract class differences

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.