Relationships and differences among classes, interfaces, and features

Source: Internet
Author: User
If you cannot read the table, review the first three articles. This is a summary table. Comparison is more intuitive.

If you cannot read the table, review the first three articles. This is a summary table. Comparison is more intuitive. -Class Interface Trait encapsulates the attributes and behaviors of a Class of objects. Abstract The behavior rules of a class object without paying attention to its specific implementation. Solve the limitation of single inheritance. (The Historical solution is to use the mixed Class (Mixin) class Class CIA

If you cannot read the table, review the first three articles. This is a summary table. Comparison is more intuitive.

- Class Interface Trait
Purpose Encapsulate the attributes and behaviors of a class of objects. Abstract The behavior rules of a class object without paying attention to its specific implementation. Solve the limitation of single inheritance. (The Historical solution is to use Mixin ))
Class Class ClassA extends ClassB Class Class implements Interface Class Class {
Use Trait;
}
Interface - Interface1 extends Interface2 -
Trait - - Trait Trait1 {
Use Trait2;
}
* Note: The above are the available relationships among the three. The specific differences are as follows!
Abstract Class (abstract class), method (abstract method) - Method (abstract method)
Final Class (not inherited), method (not overridden) - Method (not override)
Const Class Constants Has an interface constant -
Static Static variables and static methods Static Method Static variables and static methods
Access Control
Public
Protected
Private
Inheritance can change access control
Only public
Use Trait can use as to change access control.
- Use Trait to change access control using
Variable with the same name Overwrite (extends Class)
Use Trait)
- Use Trait)
Method with the same name Overwrite (extends)
Use Trait can be resolved using insteadof or
Error in redeclare (extends Interface) Use Trait can be resolved using insteadof or
* Note: The names of static member variables/functions cannot be the same as those of common member variables/functions. Otherwise, an error is returned.

(End)

All Rights Reserved: the source information of the old white is reprinted. < <类、接口、特性三者之间的关系和区别> >

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.