Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java Foundation - The relationship between abstract classes and interfaces     - GNU Linux use diff to generate a patch with the patch (Linux)

- PostgreSQL query result area is removed and precision (Database)

- CentOS 5.5 kernel upgrade installation iftop (Linux)

- MySQL composite partition (Database)

- Learning UNIX good habits (Linux)

- zBackup: A versatile tool to remove duplicate backup (Linux)

- Nginx DHCP TFTP Kickstart set up automatic installation system (Server)

- Linux System Getting Started Learning: Linux in the last command (Linux)

- To use slay kill user processes (Linux)

- The method to mount the CD under Linux (Linux)

- VNC configuration detailed analysis under Linux (Linux)

- Linux System Getting Started Tutorial: How to automatically set the JAVA_HOME environment variable on Linux (Linux)

- AWK principles and commands and file input (Linux)

- Packages with Snort intrusion monitoring light (Linux)

- C ++ pointer two third memory model (Programming)

- The top command under Linux (Linux)

- Oracle SQL statement to retrieve data paging table (Database)

- Hadoop 2.2.0 installation development environment (standalone pseudo-distributed mode) (Server)

- Analysis of MySQL Dockerfile 5.6 (Database)

- Ubuntu Thunderbird 24.4.0 (Linux)

 
         
  Java Foundation - The relationship between abstract classes and interfaces
     
  Add Date : 2017-08-31      
         
         
         
  In the object-oriented concept, we know that all objects are represented by the class, but not all classes can be used to describe the object, if a class does not contain enough information to describe a specific object, Such a class should be defined as an abstract class.

Abstract class

Overview

For example, we have to describe the "animal", it has the age, volume, will be called, can run. But lacking enough traits (compared to a cat, a dog, they are all animals, they have their own characteristics), we do not have the only one can represent the animal thing (of course, this is relative to cats, dogs Say, in biology, the animal is still a way to define), then we can use abstract class to describe it. When we use a class to specifically describe the "cat", this class can inherit the "animal" the abstract class, and then be described in detail. Subclasses of abstract classes must implement abstract methods in abstract classes, such as running and calling in animal classes, in subclasses (cat classes, dogs) to achieve how they run, how to call. The abstract method has no method body and is decorated with abstract.

Here we introduce the system to abstract class:

Features

Abstract methods and abstract classes are represented by the keyword abstract.

B. Abstract classes can not be instantiated

C. Classes with abstract methods must be abstract classes (or interfaces); abstract classes do not necessarily have abstract methods.

D. If a class inherits an abstract class, then the class itself is either an abstract class, or the class overrides any abstract methods of the parent class.

use

Forcing subclasses to perform certain functions.

Member Features

A. Member variables - can have a member variable, you can also have a constant

B. constructor - a constructor for subclass access to the parent class, the parent class data to initialize.

C. Member methods - can have abstract methods, can also be related to non-abstract methods.

The abstract method is to ask the subclass to do something.

Ii. Non-abstract methods are subclassed to improve code reusability.

note:

Can not be final, private, static coexistence.

Abstract class (abstract class) is defined as follows:

Public abstract class AbstractClass / / There is at least one abstract method
{
 Public int a; // Ordinary data members
// abstract methods, abstract class subclasses In the class must implement the abstract class abstract method
 Public abstract void method2 ();
 Public void method3 (); // Non-abstract method
        Publicvoid method3 () {
    ... ... / / abstract class can be given to non-abstract method of the default behavior, that is, the specific realization of the method
 }}
}}

interface

definition

Is a special abstract class. Than abstract class more abstract, because it inside the method is abstract.

format

Class class name The name of the interface

      {

      }}

Features

A. The interface can not be instantiated.

B. Interface method:

A) be overridden by subclasses

B) Either the subclass is also an abstract class.

Member Features

Member Variables - There are only constants in the interface.

Because the interface member variables have default modifiers. Recommendation: Always give their own modifier, public static final

Constructors - There are no constructors in the interface.

If any class does not inherit the parent class, then this class will inherit the Object class

Member methods - methods in the interface are abstract.

Because the member methods in the interface have default modifiers. Recommendation: always give their own modifier, public abstract

Thought characteristics:

1. Rules for External Exposure.

2. Function expansion of the program

3. Reduce coupling

4. Used to achieve more.

Interfaces are defined as follows:

Interface Interface
{
 Public static final int i; / / interface can not have ordinary data members, can only have static data members can not be modified, static said the overall, final that can not be modified, you can not static final modification will be implicitly declared static And final, but the proposal is always given their own modifier public static final
/ / Interface method must be abstract method, it can not abstract, but it is recommended to always give their own modifier Public abstract
 Public void method2 (); / / interface can not be given the default behavior of the method, that is not a specific method to achieve. (Note that abstract is omitted here.)
}}
Class zi implements Interface
{
        Publicvoid method1 ()
        {
                  ... / / interface to the abstract method of rewriting
        }}
        Public void method2 ()
        {
                  ... ... / / Note that the abstract method is to rewrite all (the interface method is all abstract methods)
          }}
}}
 
// In another case, the subclass is also an abstract class, and you can not override the methods in the interface
/ *
Abstract class zi implements Interface
{
        Public abstract void method1 (); // No rewriting
        
        Public void method2 () // Override method2 ()
        {
                  System.out.println ( "Override method2 ()");
        }}
}}
* /

Abstract class and interface contrast

 

 

Abstract class

interface

 Member Features

 

 Member variables

Variable, or constant

constant

The default modifier public static final

 Construction method

Have

no

 Member methods

Abstract methods or non-abstract methods

Abstract approach

The default modifier public abstract

 design concept

idea

Is inherited, the definition of the inheritance system of common content

Is implemented, the definition of the expansion of the entire system content

reflect

Is a

Like a

 

 Relationship characteristics

 

Classes and classes

  Inheritance, only single inheritance, multi-layer inheritance

 Classes and interfaces

  Realize the relation, can realize alone, can also realize more. Can also inherit a class at the same time, to achieve multiple interfaces.

   Interface and Interface

  Inheritance, you can single inheritance, you can also inherit.

 

supplement:

Further understanding of Java on the introduction of abstract classes, the purpose of the interface, the degree of mother on the mother's reply is as follows:

1, from the class hierarchy point of view, abstract class is at the top of the hierarchy, but in the actual design, in general, abstract class should be the back will appear. why? In fact, the abstract class of access to a bit like mathematical extraction of public factors: ax + bx, x is the abstract class, if you do not have the previous formula, how do you know x is not the public factor? In this regard, but also in line with people's understanding of the world, the first concrete after the abstract. So in the design process if you get a lot of specific concepts and to find its common, the common set is abstract class should be right.

2, interface On the surface, and abstract class is very similar, but the usage is completely different. Its basic function is to put together some unrelated classes (concepts) together to form a new, centralized operation of the "new class." A typical example is the "driver". Who can be a driver? Anyone can, as long as the driver's license. So whether you are a student, white-collar, blue-collar or the boss, as long as there is driver's license is the driver.

Interface DriverLicence {
License getLicence ();
}}
 
Class StudentDriver extends Student implements DriverLicence
{
}}
Class WhtieCollarEmployeeDriver extends WhtieCollarEmployee implements DriverLicence
{
}}
Class BlueCollarEmployeeDriver extends BlueCollarEmployee implements DriverLicence
{
}}
Class BossDriver extends Boss implements DriverLicence
{
}}
 

When I define the "car" class, I can specify the "driver" of the.

Class Car {
      SetDriver (DriverLicence driver);
}}

At this time, Car's object does not care about the driver in the end is doing, and their only common ground is to receive a driver's license (all achieved DriverLicence interface). This should be the most powerful interface is the abstract class can not be compared.

summary

Abstract class is to extract the specific type of public factors, and the interface is to unrelated classes "hash" into a common group, and therefore may be a little higher. The emergence of the two is to reduce the amount of code on the one hand, improve efficiency, on the other hand, take care of the previously mentioned inheritance and polymorphism, making the program in line with the characteristics of object-oriented thinking, closed on the amendment, to expand open.
     
         
         
         
  More:      
 
- The Definitive Guide to Linux NAT network connection (Linux)
- Android components save state series - Activity (Programming)
- Linux batch copy data script (Linux)
- Security: Unix operating system intrusion tracking Strikes Back (Linux)
- Add your own kernel and ramfs based on an existing Linux LiveCD (Linux)
- How to upgrade Docker 1.6 on Fedora / CentOS (Server)
- Build Eclipse + Maven + Scala-IDEA the Scala Web development environment (Server)
- Learning how to teach safety system to prevent your own IP leakage (Linux)
- Oracle 11g user rights management study notes (Database)
- HTML5 Application Cache (Programming)
- Cool Android realization SVG animation (Programming)
- Java String and StringBuffer and StringBuilder Comments (Programming)
- Install Web-based monitoring tool: Linux-Dash (Server)
- MongoDB relations, references, index query coverage (Database)
- File compression and packaging commands under Linux (Linux)
- Learning MySQL data types (Database)
- Linux md5sum verify file integrity (Linux)
- MySQL password on those things you should know (Database)
- Linux System Getting Started Tutorial: How to change the default Java version in Linux (Linux)
- 20 Advanced Java interview questions summary (Programming)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.