2-1 abstract class, interface, polymorphism, Set

Source: Internet
Author: User
Tags comparable
1. Definition of abstract class 1.1 abstract class

The class modified with abstract is an abstract class. The example is as follows:

Public abstract class test {// abstract class definition public abstract void doitbyhand (); // abstract method definition}
1.2 role of abstract classes

By inheriting it to realize polymorphism, a specific method can be implemented in the later stage to interface the things to be implemented and realize reusability. abstract classes are the bridge between interfaces and implementation classes.

1.3 abstract Method

An abstract method is a method that only defines the method but does not implement it.

2. Definition of interface 2.1

An interface is a special abstract class that only contains the definitions of constants and methods, without the implementation of variables and methods.

The syntax format of the interface definition is as follows:
Access modifier interface name {
Abstract Attribute Set
Abstract method set

2.2 functions of interfaces
  • Declare the method, expect one or more classes to implement this method.
  • Reveals the programming interface of an object without revealing the actual program body of the class.
  • Capture the similarity between irrelevant classes without forcing the class relationship.
  • It can be passed as a parameter to a method called on other objects.
  • You can use interfaces to implement the same behavior of irrelevant classes without considering the hierarchical relationships between these classes. You can understand the interaction interface of an object based on the interface, instead of the class to which the object belongs.
2.3 comparable Interface

This interface forcibly sorts the objects of each class. This sort is called the natural sorting of the class, and the compareto method of the class is called its natural comparison method. Objects (and arrays) that implement this interface can be automatically sorted by collections. Sort (and arrays. Sort. Objects that implement this interface can be used as keys in an ordered ing table or elements in an ordered set without specifying a comparator.

To implement the comparable interface, you must implement the int compareto (t o) method. This method compares the order of the object with the specified object. If the object is smaller than, equal to, or greater than the specified object, a negative integer, zero, or positive integer is returned.
We strongly recommend (X. compareto (y) = 0) = (X. Equals (y), but not strictly.

2.4 comparator Interface

Comparator is located under the java. util package, while comparable is located under the java. lang package. The comparable interface embeds the comparison code into its own class, while the latter implements comparison in an independent class. If the class designer does not implement the comparable interface because of the Compare problem, you can use the comparator to sort the comparison algorithm and prepare for using different Sorting Standards. For example: in ascending or descending order.

2.5 relationship and difference between interfaces and abstract classes

Link: an interface is a special abstract class. They have deep origins and are similar to each other.


  • An abstract class represents an inheritance relationship in Java. A class can only use an inheritance relationship once. However, a class can implement multiple interfaces.
  • Abstract class can have its own data members or non-Abstarct member methods. In interface, only static data members that cannot be modified (that is, they must be static final, but data members are not defined in interfaces) are allowed. All member methods are abstract.
  • Abstract class and interface have different design concepts. Abstract class represents the "is-a" relation, and interface represents the "like-a" relation.
  • Classes that implement abstract classes and interfaces must implement all of these methods. Abstract classes can have non-abstract methods. There is no implementation method in the interface.
  • The variables defined in the interface are of the public static final type by default and must be given the initial values. Therefore, the Implementation class cannot be redefined or its value cannot be changed. Of course, the variable definition can also be friendly. The variables in the abstract class are friendly by default. Their values can be redefined in the subclass or assigned again.
  • The methods in the interface are public abstract by default.
3. Definitions of Polymorphism

Multiple expressions of the same behavior or multiple implementations of the same behavior are called polymorphism.

4. Method Overloading

In the same Java class (including the parent class), if the method name is the same, and the parameter list is different, it is called overload.

Method overload rules:
(1): The method name must be the same
(2): The parameter list must be different (the number, type, or order of parameters are different ).
(3): The return types of methods can be the same or different. Different return types are not enough to be considered as method overloading.

5. Method coverage

If a method is defined in the new class and its name, return type, and parameter table exactly match the name, return type, and parameter of the method in the parent class, the new method is called to overwrite the old method.

The method override rules are as follows:
(1): The return type, method name, and parameter list of the override method must be the same as that of the method it overwrites.
(2): The overwriting method cannot be less accessible than the method covered by it (that is, the access permission cannot be reduced ).
(3): The overwrite method cannot throw more exceptions than the method it overwrites.

6. Java Collection framework

A collection framework is composed of a group of interfaces used to operate objects.

Common interfaces include: Set, list, map. Common Implementation classes include hashset, treeset, arraylist, rule list, hashmap, and treemap.

6.1 use of arraylist for common collection classes

For more information, see the API documentation.


Exchange to explore my Sina Weibo: http://weibo.com/tianrui1990




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.