java– virtual function, abstract function, abstract class, interface __linux

Source: Internet
Author: User

java– virtual functions, abstract functions, abstract classes, interfaces

1. Java virtual functions

The existence of virtual functions is for polymorphism.

C + + Ordinary member function plus virtual keyword becomes virtual function

In Java, there is no concept of virtual function, its ordinary function is equivalent to C + + virtual functions, dynamic binding is the default behavior of Java. If you do not want a function to have virtual function properties in Java, you can add the final keyword to a non-virtual function

PS: In fact, C + + and Java in the virtual function of the same point of view, similar.

2. Java abstract function ( pure virtual function )

An abstract function, or a pure virtual function, exists to define an interface.

The pure virtual function in C + + is as follows: virtual void print () = 0;

The pure virtual function in Java is: abstract void print ();

PS: In the abstract function of C + + and Java or the same.

3. Java abstract class

Abstract classes exist because the parent class includes both a specific definition of the subclass's generic function and a function interface that requires subclasses to implement them. The abstract class can have data members and Non-abstract methods.

Abstract classes in C + + only need to include pure virtual functions as an abstract class. If you include only virtual functions, you cannot define them as abstract classes, because there is actually no abstract concept in the class.

A Java abstract class is a class declared with an abstract decoration.

PS: Abstract class is actually a half virtual and half real things, can be all virtual, this time into an interface.

4. Java interface

Interfaces exist for the purpose of forming a statute. You cannot have ordinary member variables in an interface, and you cannot have a non-virtual virtual function.

The interface in C + + is actually a completely virtual base class.

In Java, an interface is a class decorated with interface.

PS: Interfaces are abstract classes that are virtual to the extreme.

5. Summary

C + + virtual function = = Java normal function

C + + pure virtual function = = Java abstract function

C + + abstract class = = Java abstract class

C + + Virtual base class = = Java interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.