Java object-Oriented learning notes--5 (abstract class, interface)

Source: Internet
Author: User
Tags define abstract


1. Abstract class

Abstraction is the abstraction of objects that have common methods and properties, and when extracted, redesign a more generic, more popular class, called abstract class.

1) Abstract method: by the abstract modification, only the definition of methods, no method of implementation;

2) The class containing the abstract method must be an abstract class;

3) Abstract class: Modified by the abstract, cannot be instantiated, can contain abstract methods and non-abstract methods;

4) Abstract classes are required to be inherited, and subclasses inherit the following actions:

4.1) Overriding the abstract method of the parent class in the subclass

4.2) Subclasses are also declared as abstract classes

5) Meaning of abstract class:

5.1) Encapsulating the common variables and methods of subclasses--Reuse of code

5.2) Provide a common type for subclasses--upward styling

5.3) Define abstract methods, subclasses rewrite it, provide a common entry for subclasses


2. Interface

1) Develop a standard, specification

2) You can do something if you follow the standard

3) defined by interface

The interface can contain only constants and abstract methods-data defaults to constants (public static final).

Method defaults to abstract (public abstract)

4) interface cannot be instantiated

5) The interface needs to be implemented, the subclass implements the interface, and all the abstract methods in the interface must be rewritten

6) A class can implement multiple interfaces, inheriting classes, and implementing interfaces, it is necessary to inherit and implement

7) can inherit between interfaces (extends)


3. The difference between abstract classes and interfaces

① from a grammatical point of view: The abstract class method can have its own data members, can also have non-abstract member methods, and give the default behavior of the method, and in the interface way generally do not define member data variables, all methods are abstract, Method cannot have the default behavior.

② from a programmatic point of view: Abstract class represents an inheritance relationship in the Java language, and a class can only use one inheritance relationship at a time. and a class can implement multiple interface.

③ from the problem domain perspective: Abstract class embodies an inheritance relationship in the Java language, in order to make the inheritance relationship reasonable, there must be an "is a" relationship between the parent class and the derived class, that is, the parent class and the derived class should be the same in nature. For interface, it does not require that the interface and interface definitions be consistent in the concept, but only the contract that implements the interface definition.

Detailed differences:

1. Abstract classes can have construction methods, and interfaces cannot have constructors.

2. There can be ordinary member variables in the abstract class, there are no ordinary member variables in the interface

3. Abstract classes can contain non-abstract ordinary methods, all the methods in the interface must be abstract, and cannot have non-abstract ordinary methods.

4. The access type of an abstract method in an abstract class can be public,protected, but the abstract method in an interface is only of the public type, and the default is the public abstract type.

5. Abstract classes can contain static methods, and interfaces cannot contain static methods

6. Both abstract classes and interfaces can contain static member variables, and the access type of static member variables in an abstract class can be arbitrary, but the variables defined in the interface can only be public static final types.

7. A class can implement multiple interfaces, but can inherit only one abstract class.


This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1655272

Java object-Oriented learning notes--5 (abstract class, interface)

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.