In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of abstract classes and interfaces in Java, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
1. Abstract class 1. Abstract Class 1.1 definition of Abstract Class
In Java object-oriented, all objects are depicted with classes, but not all classes are used to describe objects. If a class does not contain enough information to describe a concrete object, such a class is an abstract class.
1.2 how abstract methods are defined
Public abstract returns the value type method name (parameter)
1.3 the way abstract classes are defined is abstract class class name {} 2. The difference between abstract class and instance class
Abstract class cannot instantiate object instance class can instantiate object
3. Abstract class example
Define an abstract class People, define a method work in the class, uncertain how people work, can be defined as an abstract method to solve the uncertainty of the parent class method. Abstract methods cannot be implemented in the parent class, so there is no function body. Then in subsequent inheritance, you should override the method in the subclass and specifically implement the method. Note: abstract classes can contain abstract methods, and classes that contain abstract methods must be defined as abstract classes.
Public abstract class People {String name; int age; public abstract void work ();}
Define a Student class that inherits the abstract class People. A subclass inherits an abstract class, and all abstract methods in this abstract class must be overridden, otherwise the subclass is defined as an abstract class.
Public class Student extends People {int score; int stu_id; / / overrides the work method @ Override public void work () {System.out.println in the abstract class ("study hard! one million a year, marry Bai Fumei!") ;}}
Define a test class TestDemo.
Public class TestDemo {public static void main (String [] args) {People p = new Student (); p.work ();}}
Run to observe the results of the operation.
In the use of polymorphism, the reference variable of the base class refers to the object of the derived class and calls the work method, which implements the work method overridden to its abstract base class in the derived class Student class.
4. The characteristics of abstract classes
① abstract classes cannot instantiate objects, so abstract classes must be inherited before they can be used, and other functions are the same as normal classes. ② A class can inherit only one abstract class. In fact, in Java, a class can inherit only one class, regardless of whether its parent class is abstract or not. The following illustration explains.
If multiple inheritance is allowed, in the illustration, class D inherits both class B and class C, then two identical instance variables an appear in class D at the same time, which is not allowed because instance variables cannot be repeatedly defined in the same scope. Therefore, there can only be a single inheritance in Java, that is, a class can only have one parent class. The modifier of an ③ abstract class cannot be private. ④ abstract classes do not necessarily contain abstract methods, but classes that have abstract methods must be abstract classes. ⑤ constructor, class methods cannot be declared as abstract methods. A subclass of an ⑥ abstract class must give a concrete implementation of the abstract method in the abstract class, unless the subclass is also an abstract class.
Second, interface 1. Interface 1.1 definition of Interfac
In software engineering, interfaces generally refer to methods that can be called by others. In Java, an interface is an abstract type, which is more abstract than an abstract class and is a collection of abstract methods. A class inherits the abstract methods of an interface by inheriting the interface. By definition, an interface is just a collection, not a class. Class describes properties and methods, while interfaces contain only methods (unimplemented methods) and constants.
1.1 define an interface public interface interface name {/ / declare constant / / abstract method}
As follows:
The variable of the ① interface will be implicitly specified as the public static final variable (and can only be the public static final variable, and will be compiled with private modification) the method of the ② interface will be implicitly specified as the public abstract method.
Defining variables in the interface can omit or have fewer modifiers, because the bottom layer will complete it for you, but no mistakes are allowed.
1.2 use of the interface
Class name implements interface name [, other interface name, other interface name … ,...]
In Java, a class can inherit only one class, but a class can implement multiple interfaces. In other words, single inheritance, multiple implementation.
1.3 considerations for the interfac
All methods in the ① interface cannot have a concrete implementation, that is, all methods in the interface must be abstract methods. You can vaguely see the difference between an interface and an abstract class. An interface is an extremely abstract type, which is more "abstract" than an abstract class, and generally does not define variables in the interface. ② can contain one or more abstract methods in an abstract class, but in an interface, all methods must be abstract, have no method body, and are more abstract than abstract classes. The interface specifies what a class must do, not how it does it.
two。 Interface exampl
Define an interface A
Public interface A {/ / declare constant final int a = 10; / / Abstract method public abstract void fun ();}
Define a Class B implementation interface A
Public class B implements A {/ / A pair of abstract methods in interface A rewrite @ Override public void fun () {System.out.println ("rewrite fun ()");}}
Define a TestDemo test class implementation call
Public class TestDemo {public static void main (String [] args) {BB = new B (); b.fun (); / / reference to the polymorphic base interface An a = new B (); a.fun ();}}
Note: if an ordinary class inherits both a class and an implementation interface, it should be inherited before it is implemented, otherwise a syntax error will be reported. For example: public class An extends B implements Cpar D E …
3. Characteristics of the interface
Only abstract methods are defined in the ① interface, which are public abstract by default, and these modifiers can be omitted when the method is declared. ② defines instance variables in the interface, non-abstract instance methods and static methods are not allowed. There are no constructors in the ③ interface and cannot be instantiated. ④ one interface cannot implement another interface, but it can inherit more than one other interface. The ⑤ interface must implement its abstract methods through classes. ⑥ if a class cannot implement abstract methods in an interface, then this class should be designed as an abstract class. ⑦ does not allow you to create an instance of an interface, but allows a reference variable that defines an interface type to refer to an instance of a class that implements the interface (polymorphic).
III. The difference between abstract classes and interfaces
① abstract classes can provide implementation details of member methods, but only public abstract methods can exist in interfaces; member variables in ② abstract classes can be of various types, while member variables in interfaces can only be of public static final type; ③ interfaces cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods ④ A class can inherit only one abstract class, while a class can implement multiple interfaces
Thank you for reading this article carefully. I hope the article "sample Analysis of Abstract classes and Interfaces in Java" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.