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 implement Abstract Class and Interface in Java

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

Share

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

This article introduces the knowledge of "how to implement abstract classes and interfaces in Java". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Abstract class 1.1 what is an abstract class?

1.1.1 understanding of abstract classes

1.1.2 about Abstract CLA

There are common characteristics between classes, and these common features are extracted to form abstract classes.

The class itself does not exist, belonging to an abstract class cannot create an object [cannot be instantiated]

Abstract classes are used to be inherited by subclasses

Finial keyword and abstract keyword cannot appear at the same time.

A subclass of an abstract class can be an abstract class

Although abstract classes cannot be instantiated, they do have constructors that are used by subclasses.

Interfaces generally describe behavior and action information.

A non-abstract class inherits the abstract class, and the abstract methods in the abstract class must be implemented (overridden, rewritten).

Public class HelloWorld {public static void main (String [] args) {/ / how to implement polymorphism? Animal a = new Bird (); / / upward transformation (automatic type conversion) / / facing abstract object programming, reducing the coupling of the program, providing the extensibility of the program, complying with the OCP principle}} abstract class Animal {public abstract void move () } class Bird extends Animal {public void move () {} / / Bird is not an abstract class and must implement the abstract methods of the parent class} / / if Bird is an abstract class, then the inherited abstract methods can not be rewritten, overridden, implemented 1.2.What type of abstract class does the abstract class belong to?

Abstract classes also belong to reference data types

1.3 how are abstract classes defined?

[modifier list] abstract class class name {

Quasi-body

}

1.4 Abstract method

1) Abstract methods represent methods that are not implemented, have no method body, and end with a semicolon. For example:

Public abstract void doSome ()

2) there may not be abstract methods in abstract classes, but abstract methods must be in abstract classes.

3) methods without a method body are not necessarily abstract methods. Many methods in the Object class do not have a method body, ending with ";" e.g.:public native int hashCode (); the bottom layer of this method invokes the dynamic link library program written by C++; there is native in the previous modifier list, which means calling the JVM local program.

2. Interface 2.1 about the interface

1) the interface also refers to the data type all the time

2) the interface is completely abstract (the abstract class is semi-abstract) or it can be said that the interface is a special abstract class.

3) Interface supports interface inheritance and interface multi-inheritance.

Interface A {} interface B {} interface C extends A,B {}

4) the interface contains only two parts, one is constant and the other is abstract method. All the contents are public-decorated and public.

5) the public static in the abstract method in the interface can be omitted, "public abstract int sun (int aline int b);"-> "int sun (int a line int b);"

6) the public static final in the variables in the API can be omitted, "public static final double PI = 3.1415926;"-> "double PI = 3.1415926;"

7) the methods in the interface are all abstract methods, so the methods in the interface have no method body.

2.2 how is the interface defined?

[modifier list] interface API name {

}

2.3 basic interface syntax

1) between classes and classes is called inheritance, and between classes and interfaces is called implementation (which can be regarded as inheritance), with the keyword "implements"

2) when a non-abstract class implements an interface, all abstract methods in the interface must be implemented (overridden, rewritten).

Public class interface {public static void main (String [] args) {/ / how to implement polymorphism? / / the reference of the parent type points to the object of the subtype MyMath m = new MyMathImp (); / / calls the method in the interface (interface-oriented programming) int result = m.sum (10Lab); System.out.println (result);}} interface MyMath {double PI = 3.1415926; int sum (int ajeint b); int sub (int ajint b) } class MyMathImp implements MyMath {public int sum (int aline int b) {/ / override abstract method return a + b in the interface;} public int sub (int aline int b) {/ / override abstract method return a-b in the interface

Note: the abstract method public that is overridden when a non-abstract class implements an interface cannot be saved.

3) A class can implement multiple interfaces, which makes up for the defect that "classes and classes in java only support single inheritance".

When there is no inheritance relationship between the interface and the interface, it can also be forced to convert, but a ClassCastException exception may occur at run time, which needs to be judged by adding instanceof.

Public class HelloWorld {public static void main (String [] args) {/ / how to implement polymorphism? AA = new C (); if (an instanceof B) {Bb2 = (B) a * / / E () here has nothing to do with K. E implements M, but does not implement interface K, so it cannot be transformed downwards into k.m3 ();} interface A {void M1 ();} interface B {void m2 () } class C implements A Magi B {/ / C implemented An and B public void M1 () {} public void m2 () {System.out.println ("can be transformed downwards!") ;} interface K {void m3 ();} interface M {void m4 ();} class E implements M {/ / E practiced M, but did not implement K public void m3 () {} public void m4 () {}

4) if both inheritance and implementation exist, how to write the code?

The extends keyword comes before the implements keyword.

Public class interface {public static void main (String args []) {/ / create object Flyable f = new Cat (); f.fly ();}} / / Animals: parent class Animal {} / / Flying interface (a pair of wings) / / the interface that can be plugged and unplugged is the interface. / / API extracts behavior interface Flyable {void fly ();} / Animal subcategory: cat / / Flyable is an interface, which is a winged interface that is inserted into the cat through the interface, so that the cat can fly class Cat extends Animal implements Flyable {public void fly () {System.out.println ("Cats can fly"). }} "how to implement abstract classes and interfaces in Java" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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