In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of virtual functions, abstract functions, abstract classes and interfaces in Java. It is very detailed and has a certain reference value. Interested friends must read it!
one。 Java virtual function
Virtual functions exist for polymorphism.
It lies in the so-called "deferred binding" or "dynamic binding". The call to a class function is not determined at compile time, but at run time. Because it is not possible to determine whether the function of the base class or which derived class is called when writing the code, it is called a "virtual" function.
The ordinary member function in C++ plus the virtual keyword becomes a virtual function.
In fact, there is no concept of virtual function in Java, its ordinary function is equivalent to the virtual function of C++, and dynamic binding is the default behavior of Java. If you don't want a function to be virtual in Java, you can add the final keyword to become a non-virtual function.
PS: in fact, C++ and Java have more or less the same point of view on virtual functions.
two。 Java abstract function (pure virtual function)
Abstract functions or pure virtual functions exist to define interfaces.
The form of pure virtual function in C++ is: virtual void print () = 0
The form of pure virtual function in Java is: abstract void print ()
PS: C++ and Java are still the same in terms of abstract functions.
three。 Java Abstract Class
Abstract classes exist because the parent class includes not only the specific definition of the common functions of the subclasses, but also the function interfaces that need to be implemented by the subclasses. Abstract classes can have data members and non-abstract methods.
C++ abstract class only needs to include pure virtual functions, which is an abstract class. If only virtual functions are included, they cannot be defined as abstract classes, because there is no abstract concept in the class.
Java abstract classes are classes declared with abstract decorations.
PS: an abstract class is actually a semi-virtual and semi-real thing that can all be virtual and become an interface at this time.
four。 Java interface
The interface exists to form a protocol. There can be no ordinary member variables or impure virtual functions in the interface.
The interface in C++ is actually a completely virtual base class.
Interfaces in Java are classes decorated with interface.
PS: interfaces are abstract classes that are virtual to the extreme.
The difference between abstract classes and interfaces
An interface is not a class, and an abstract class is a class that is not fully functional and cannot instantiate objects.
A class can implements multiple interfaces. A class can extends only one abstract class.
Interface has no constructor, all methods are public abstract, and generally no member variables are defined. All member variables are static final and must be initialized. Except that an abstract class cannot instantiate an object, other functions of the class still exist, and member variables, member methods, and constructors are accessed in the same way as ordinary classes.
A class that implements an interface must implement all the methods described in the interface (all methods are abstract methods), otherwise it must be declared as an abstract class.
If a class contains abstract methods, the class must be abstract. Any subclass must override the abstract method of the parent class or declare itself an abstract class.
These are all the contents of this article entitled "sample Analysis of Virtual functions, Abstract functions, Abstract classes and Interfaces in Java". Thank you for reading! Hope to share the content to help you, more related knowledge, 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.
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.