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

What are the differences between interfaces and abstract classes in JAVA

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about the differences between interfaces and abstract classes in JAVA. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The difference between interfaces and abstract classes:

Similarities:

L are at the top of inheritance and are used to be implemented or inherited by other classes

L cannot instantiate an object directly.

All contain abstract methods, and their subclasses must override these abstract methods

Difference:

Abstract classes provide implementations for some methods to avoid repeated implementation of these methods by subclasses and improve code reusability; interfaces can only contain abstract methods

A class can inherit only one direct parent class (possibly an abstract class), but it can implement multiple interfaces; (the interface makes up for the single inheritance of Java)

Abstract class is what you should have in this thing, and inheritance system is a kind of is..a relationship.

L interface is the extra content in this thing, and inheritance system is a kind of like..a relationship.

The choice of the two:

L give priority to interfaces and use as few abstract classes as possible

The definition and usage format of Polymorphism

The definition format of polymorphism: the reference variable of the parent class points to the subclass object

Parent class type variable name = new subclass type ()

Variable name. Method name ()

L the format of general class polymorphism definition

Parent variable name = new subclass ()

Such as: class Fu {}

Class Zi extends Fu {}

/ / Polymorphic use of classes

Fu f = new Zi ()

L format of abstract class polymorphic definition

Abstract class variable name = new abstract class subclass ()

Such as: abstract class Fu {

Public abstract void method ()

}

Class Zi extends Fu {

Public void method () {

System.out.println ("override parent abstract methods")

}

}

/ / Polymorphic use of classes

Fu fu= new Zi ()

Format of interface polymorphism definition

Interface variable name = new interface implementation class ()

Such as: interface Fu {

Public abstract void method ()

}

Class Zi implements Fu {

Public void method () {

System.out.println ("override interface abstract methods")

}

}

/ / polymorphic use of the interface

Fu fu = new Zi ()

Instanceof keyword

We can use the instanceof keyword to determine whether an object belongs to a certain data type. For example, the object of the student belongs to the student category, and the object of the student also belongs to human beings.

Use format:

Boolean b = object instanceof data type

Such as

Person p1 = new Student (); / / prerequisites, students have inherited human beings

Boolean flag = p1 instanceof Student; / / flag result is true

Boolean flag2 = p2 instanceof Teacher; / / flag result is false

Thank you for reading! This is the end of the article on "what are the differences between interfaces and abstract classes in JAVA". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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