C++ and java inherited class polymorphism details and example code

  • 2020-06-07 04:27:56
  • OfStack

Details on polymorphism of C++/java inherited classes

Those of you who have studied C++ and Java know that since both of them can do object-oriented programming, the three main features of object-oriented programming are encapsulation, inheritance, and polymorphism, so let's take a quick look at the differences between C++ and Java in polymorphism.

So let's do one case each.

C++


// Test inheritance and polymorphism 
class Animal {

public:
 char name[128];
 char behavior[128];

 void outPut() {
  cout << "Animal" << endl;
 }

 void outPutAnimal() {
  cout << "Father" << endl;
 }

 Animal() {
  strcpy(name, "Animal");
  strcpy(behavior, "Call");
 }
};

class Dog: public Animal {

public:
 // Subclasses define 1 The same attributes as the parent class 
 char name[128];
 char sex;

 // Subclasses override superclass methods 
 void outPut() {
  cout << "Dog" << endl;
 }

 void outPutDog() {
  cout << "Son" << endl;
 }

 Dog() {
  strcpy(name, "Dog");
  sex = 'M';
 }
};

Both of these classes are simple, so let's take a look at the test code and results.


/*
 The sum of the test results without polymorphism Java The same 
 Dog dog;
 cout << dog.name << endl;
 cout << dog.sex << endl;
 cout << dog.behavior << endl;
 dog.outPut();
 dog.outPutAnimal();
 dog.outPutDog();

 // The behavior of the parent class can be accessed as follows 
 dog.Animal::outPut();
*/

// In the case of polymorphism: 
 Animal *dog = new Dog;

 cout << dog->name << endl;
 cout << dog->behavior << endl;
 dog->outPut();
 dog->outPutAnimal();

// The test results 
Animal
Call
Animal
Father

You can see that all representations are the behavior of the parent class, whether it is the same property or the overridden method. Note 1: If a subclass hides a member function of the parent class, all member functions of the parent class with the same name (overloaded functions) are hidden. If you want to access a hidden function in the parent class, you need to access it by the name of the parent class (dog.Animal ::outPut();) .

In the case of polymorphism, we are accessing the behavior of the superclass, so how can we access the functions of the subclass? The answer is to do this by defining virtual functions, which we'll explain in a later post.

Now let's look at Java's performance in 1.

Java


// The parent class 
public class Animal {

 public String name = "Animal";
 public String behavior = "Call";

 public void outPut() {
  System.out.println("Animal");
 }

 public void outPutAnimal() {
  System.out.println("Father");
 }
}

// A subclass 
public class Dog extends Animal{

 public String name = "Dog";
 public char sex = 'M';

 public void outPut() {
  System.out.println("Dog");
 }

 public void outPutDog() {
  System.out.println("Son");
 }
}

The subclass also defines the same property as the parent class and overrides the method of the parent class. Let's take a look at its test method and test result.


public static void main(String[] args) {

 Animal dog = new Dog();

 System.out.println(dog.name);
 System.out.println(dog.behavior);
 dog.outPut();
 dog.outPutAnimal();

}

// The test results 
Animal// The parent class behavior 
Call
Dog// It represents the behavior of the subclasses 
Father

As you can see here, there is a difference between Java and C++. Java's attributes represent the behavior of the parent class, but the overridden methods represent the behavior of the child class, whereas C++ is all the behavior of the parent class.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: