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

Example Analysis of Java Abstract Class and Interface

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

Share

Shulou(Shulou.com)05/31 Report--

The knowledge points of this article "Java Abstract Class and Interface instance Analysis" are not quite understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Java Abstract Class and Interface instance Analysis" article.

1. Abstract Class 1.1 Abstract Class concept

We all know that objects are described by classes, but not all classes are used to describe objects.

There is not enough information in the class of a class to describe a concrete object. This is the abstract class.

Key word of abstract class: abstract

1.2 Abstract method

First of all, let's take a look at the previously written classes:

Class Plant {String name; String source; String genus; public void trait () {System.out.println ("parent method");}} class Tree extends Plant {@ Override public void trait () {System.out.println ("subclass method");}}

Since the trait method is an overridden method, there is no need to implement the trait of the parent class at all, so we can change it to an abstract method

Note: an abstract class may not have an abstract method, but an abstract method must be in an abstract class

Abstract class Plant {String name; String source; String genus; public abstract void trait ();} class Tree extends Plant {@ Override public void trait () {System.out.println ("subclass methods");}}

1.3 details of abstract classes

1. An abstract class is used for inheritance, so it cannot be instantiated, except for this, it is no different from an ordinary class

two。 There can be no abstract methods in the abstract class, but the abstract methods must be in the abstract class.

3. An ordinary class inherits the abstract class and overrides all the abstract methods in the abstract class.

4. Abstract classes can inherit abstract classes, and there is no need to override abstract methods

5.final cannot modify abstract classes and abstract methods

6. Abstract methods cannot be decorated with private and static

Some people may wonder: a normal class can also be inherited as a parent class, so why create another abstract class?

Let's assume a scenario where you instantiate the parent object when you instantiate the object, as follows:

Public static void main (String [] args) {Plant tree=new Plant (); tree.trait ();}

If the parent class is a normal class, the compiler will not report an error and call the method of the parent class directly

If the parent class is an abstract class, the compiler will report an error directly because the abstract class cannot be instantiated, so the problem can be discovered earlier

two。 Interface 2.1 Interface concept

The so-called interface is a common code of conduct, such as A4 paper, the size of A4 paper is 210mm*297mm, then you can only produce according to this specification.

In Java, an interface can be seen as a common specification for multiple classes and a reference type data.

API keyword: interface

2.2 details of the interface

The interface also has some things to pay attention to:

1. Interfaces are also used to be implemented by other classes and cannot be instantiated

two。 The methods in the interface can only be abstract methods, and the default is public static modification.

3. The member variables of the interface are static constants by default, so you must initialize

4. The keyword used to implement the interface is implements, and the class implementation interface still has to override all abstract methods in the interface.

5. Interfaces cannot have constructors and static code blocks

6. Interface inheritance interface using extends

7. When creating an interface, the naming of the interface usually begins with the uppercase letter "I" (recommended, non-rigid)

8. The interface can also implement polymorphism.

The most important function of interface in Java is to realize multi-inheritance.

A class in Java can inherit only one parent class, but it can implement multiple interfaces

Abstract classes and interfaces are similar, but the most essential difference between them is that abstract classes can have ordinary member methods and ordinary member variables, while methods in interfaces can only be abstract methods.

After introducing abstract classes and interfaces, the next article will introduce some of the more important interfaces in Java

The above is the content of this article on "Java Abstract Class and Interface instance Analysis". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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