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

Source: Internet
Author: User

1. Java Virtual functions

The existence of virtual functions is for polymorphism.

The ordinary member function plus the virtual keyword in C + + becomes a virtual function

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

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

2. Java abstract function ( pure virtual function )

The existence of abstract functions, or pure virtual functions, is to define interfaces.

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

In Java, the form of pure virtual function 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 each. Abstract classes can have data members and non-abstract methods.

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

The Java abstract class is a class declared with an abstract adornment.

PS: Abstract class is actually a semi-imaginary and semi-real thing, can all be virtual, this time become an interface.

4. Java Interface

The existence of an interface is to form a protocol. An interface cannot have normal member variables, nor can it have non-pure virtual functions.

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

The interface in Java is a class decorated with interface.

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

5. Summary

C + + virtual function = = Java Common function

C + + pure virtual function = = Java abstract function

C + + abstract class = = Java abstract class

C + + Virtual base class = = Java interface

Transferred from: http://blog.csdn.net/trojanpizza/article/details/6556604

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

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.