Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to compare the similarities and differences between Abstract Class and Interface in Java

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to compare the similarities and differences between abstract classes and interfaces in Java". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to compare the similarities and differences between abstract classes and interfaces in Java.

one。 Abstract Class (1) concept

In the inherited hierarchy, each new subclass makes the class more specific and specific. If you trace back from a subclass to a parent class, the class becomes more generic and ambiguous. The class should be designed to ensure that the parent class contains the common characteristics of its subclasses. Sometimes a parent class is designed so abstractly that it doesn't have any concrete instances. Such a class is called an abstract abstract class. Use the abstract modifier at the head of the class to indicate that the class is abstract.

(II) Abstract classes and abstract methods

Abstract is also used to declare abstract methods. Abstract methods (abstract method) only define the interface form of member methods, not concrete operations. Only the redefinition of the abstract member method by the derived class can really implement the operations related to the derived class. After each subclass inherits the abstract method of the parent class, it is redefined with different statements and method bodies to form several methods with the same name, the same return value, the same parameter list, the same purpose, but different concrete implementation. The purpose of defining abstract methods in abstract classes is to implement an interface in which all subclasses present a method with the same name.

1. A class that contains abstract methods must be declared as an abstract class

The following points are explained:

1. An abstract class does not necessarily have abstract methods, but a class with abstract methods must be an abstract class.

two。 Even if the parent of a subclass is concrete, the subclass can be abstract.

3. When the method implementation of the parent class becomes invalid in the subclass, the subclass can override the method of the parent class and define it as abstract.

two。 Abstract methods are only defined but not implemented, and their implementation is provided by subclasses

An abstract class, Object class, is defined below:

Public abstract class Object {public abstract double getArea (); public abstract double getPerimeter ();}

The Object class is the parent class of multiple subclasses, and the getArea () method and getPermeter () method are used in multiple subclasses, so it is better defined in the parent class; because the getArea () method and getPermeter () method for calculating area and perimeter are implemented differently in different subclasses, the implementation of the two methods defined in the Object class can only be provided by the subclass.

3. Abstract classes cannot be instantiated directly, you can let subclasses inherit and then instantiate subclasses

An example is given to illustrate:

Public abstract class Person {/ / defines an abstract method public abstract void isSmoke ();} / the student class class Students extends Person {/ / the abstract method in the abstract class after inheriting the abstract class must implement public void isSmoke () {System.out.print ("Students can't smoke.") in the subclass. }} / / teacher class class Teachers extends Person {public void isSmoke () {System.out.print ("Teachers can smoke.");} (3) the meaning of using abstract classes

Because an abstract class cannot instantiate an object, it must have a subclass to implement it before it can be used. In this way, some components with the same properties and methods can be abstracted, which is more conducive to the maintenance of code and programs. At the same time, abstract class is also an important embodiment of Java object-oriented.

two。 Interface (1) concept

Interface (Interface), in the Java programming language, is an abstract type, is a collection of like methods.

Interfaces are usually declared as interface. A class inherits the abstract methods of an interface by inheriting the interface.

The interface has the following characteristics:

Interfaces are implicitly abstract, and you don't have to use the abstract keyword when declaring an interface. Each method in the interface is also implicitly abstract, and the abstract keyword is also not required for declaration. The methods in the interface are public. (2) Grammar

1. Definition: a simple interface has global variables and abstract methods. All methods in an interface must declare only the method identity, not the specific method body, because the implementation of the specific method body is implemented by the class that inherits the interface.

The general form of interface definition is:

[access control character] interface {

Type identifier final symbol constant name n = constant

Return value type method name ([parameter list])

...

}

Define an instance of the interface:

Interface Human {/ / define a global variable final String txt= "smoking is harmful to health"; / / define an abstract method public void isSmoke ();}

two。 Implementation: to implement the interface, it is necessary to implement the methods specified by the interface. Only when all the abstract methods specified in the interface are implemented, can it be recognized that this class implements the interface and implements some of the functions represented by the interface. Implement the interface using the implements keyword.

An example of implementing an interface:

Class Student implements Human {public void isSmoke () {System.out.println ("Students can't smoke.");} public static void main (String [] args) {Student h=new Student (); h.isSmoke (); System.out.println (txt);}}

3. Inheritance of interface

Subinterfaces can override the methods and constants of the parent interface. For example:

/ / Interface Student inherits Humanpublic interface Student extends Human {/ / overrides txt String txt in the parent interface = "No smoking under age"; / / overrides the isSmoke () method void isSmoke () in the parent interface ();}

Note: in Java, multiple inheritance of a class is illegal, but the interface allows multiple inheritance.

Public interface C extends A, B three. Compare abstract classes with interfaces Q: why do you need interfaces when you have abstract classes?

It seems that abstract classes can do what interfaces do in addition to multi-inheritance, so why do you need to define interfaces?

1. Avoid the limitation of java single inheritance

2. Avoid the complexity and inefficiency caused by multi-inheritance

3. The advantages of interface-oriented programming. Reduce the coupling between programs, easy to expand and easy to maintain

Q: how do you determine when interfaces should be used and when classes should be used?

Abstract classes and interfaces are used to specify the common characteristics of multiple objects. So how do you determine when interfaces should be used and when classes should be used? In general, a strong "yes" that clearly describes the parent-child relationship should be modeled as a class. Weak "yes." a "relationship", also known as a generic relationship, indicates that an object has an attribute that can be modeled with an interface.

Type abstract class interface definition abstract keyword interface keyword inheritance can inherit a class and implement multiple interfaces; subclasses can inherit only one abstract class interface can only inherit interfaces (one or more) Subclasses can implement multiple interface methods to implement definable construction methods, there can be abstract methods and concrete methods without construction methods, and the methods are abstract, and there is no method implementation. The subclass uses the extend keyword to inherit the abstract class. If the subclass is not an abstract class, the implementation subclass that provides all declared methods in the abstract class uses the keyword implements to implement the interface. It is necessary to provide the implementation of all the declared methods in the interface to extract the same thing, that is, to reuse the contract that solidifies the program module and reduce coupling.

At this point, I believe you have a deeper understanding of "how to compare the similarities and differences between abstract classes and interfaces in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report