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

Analysis of Abstract classes and Interface examples in Java

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the Java abstract classes and interface instance analysis related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will get something after reading this Java abstract class and interface instance analysis article, let's take a look.

Abstract class what is an abstract class?

There are common features between classes. If these common features are extracted and further abstracted, abstract classes are formed. Because the class does not exist, the abstract class cannot create an object.

What type does the abstract class belong to?

Abstract classes also belong to the reference data type.

How do you define abstract classes? [modifier list] abstract class class name {class body;} what is an abstract class for?

Abstract classes cannot be instantiated and objects cannot be created, so abstract classes are inherited by subclasses, and constructors in abstract classes can be used by subclasses.

Final and abstract are two opposite keywords and cannot be used together.

What is an abstract method?

Abstract methods represent methods that are not implemented and have no method body.

For example:

Public abstract void doSome ()

Feature 1: abstract methods have no method body and end with a semicolon.

Feature 2: the list of modifiers in front of abstract methods must have the abstract keyword.

Feature 3: there can be no abstract methods in abstract classes, but abstract methods can only appear in abstract classes and interfaces.

Error-prone: not all methods without a method body are called abstract methods!

Non-abstract classes inherit abstract classes

Take the following code as an example:

Code

/ / Abstract class public abstract class Animal {/ / Abstract method public abstract void move (); non-abstract class inherits abstract class class Bird extends Animal {}

Is there a problem with writing like this? There's a problem!

The compiler gives the following results:

Abstract methods can only appear in abstract classes, and when the Bird class inherits the Animal class, it will inherit the abstract methods, so we need to implement the abstract methods in the Bird class by overriding it.

Code

/ / Abstract class public abstract class Animal {} / / non-abstract class inherits abstract class class Bird extends Animal {/ / upward transformation Animal a = new Bird ();}

Is there a problem with writing like this? No problem!

The type of an is Animal, which is abstract, so it is oriented to abstract programming, which reduces the coupling degree of the program and improves the expansibility of the program.

Summary of abstract class

Abstract classes are defined by adding abstract before class.

Abstract classes cannot be instantiated and objects cannot be created, so abstract classes are inherited by subclasses.

Final and abstract are opposite and cannot be used in conjunction with each other.

An abstract class subclass can be an abstract class or a non-abstract class.

Abstract classes cannot be instantiated, but abstract classes have constructors that can be used by subclasses.

There may not be abstract methods in abstract classes, but abstract methods can only appear in abstract classes and interfaces.

Definition of abstract method: public abstract void doSome ()

A non-abstract class inherits the abstract class and must override the abstract methods in the abstract class.

Basic syntax of interface

The interface is also a "reference data type", and a class bytecode file is also formed after compilation.

Interfaces are completely abstract (abstract classes are semi-abstract).

The syntax of the interface is as follows:

[modifier list] interface API name {.} / / modifier list can be left unwritten

Interfaces support multiple inheritance, and an interface can inherit multiple interfaces.

The interface contains only two parts: constants and abstract methods.

All elements in the interface are public-decorated and exposed.

The public abstract modifier can be omitted when an abstract method is defined in the interface.

When a constant is defined in the interface: the public static final modifier can be omitted.

Implementation of interface

There is inheritance between classes, and there is a similar relationship between interfaces and classes, called implementation.

Key word of API implementation: implements

The syntax is as follows:

Class An implements B {.}

We can also think of the implementation as a special inheritance (the relationship between interfaces and classes), but pay attention to the differences in keywords.

Note:

One: the keyword for implementation is different from inheritance, which is implements.

Second: when a non-abstract class implements an interface, be sure to rewrite the abstract methods in the interface.

Third: when implementing abstract methods in an interface in a class, the list of modifiers cannot be omitted.

Four: a class can implement multiple interfaces, which makes up for the defect that there is only single inheritance between classes.

Joint use of interface and polymorphism

Interfaces are completely abstract and cannot be instantiated, but interfaces can be used in conjunction with polymorphism through implementation.

As follows:

/ / Interface interface A {/ / Abstract method int add (int a, int b);} / A class implements interface Bclass B implements A {/ / overrides abstract method public int add (int a, int b) {return aquib in the interface } public static void main (String [] args) {/ / An interface reference points to class B object / / interface-oriented programming An a = new B ();} inheritance and implementation appear at the same time

Extends and implements can appear at the same time, and when they appear at the same time, write extends first and inplements at the end.

Class Animal {} interface Flyable {void fly ();} / / when inheritance and implementation appear at the same time, class Bird extends Animal implements Flyable {public void fly () {System.out.println ("Bird learned to fly!") ;} public static void main (String [] args) {Flyable b = new Bird (); b.fly ();} the role of Interface in Development

Interface-oriented programming can reduce the degree of program coupling and improve program expansion force, which accords with the principle of ocp development.

Any interface has a caller and an implementer, the caller calls to the interface, the implementer writes the implementation for the interface, and the interface and polymorphism are used together to decouple the caller and the implementer (cut off the relationship between the two).

The relationship between types and types

Is a:

Cat is an Animal

If the representation inheritance relationship satisfies is a, you can have the Cat class inherit the Animal class.

Has a

Bird has a Wing

Any representational association that satisfies has a.

Management relationships usually exist as decorations of attributes, and Wing can be defined as an instance variable in the Bird class.

Like a

Cook like a Menu (more abstract)

For any representation implementation relationship that satisfies like a, you can use the class Cook to implement the Menu interface.

This is the end of the article on "Analysis of Abstract classes and Interface instances in Java". Thank you for reading! I believe you all have a certain understanding of the knowledge of "Abstract Class and Interface instance Analysis in Java". If you want to learn more, you are welcome to follow the industry information channel.

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