Hello, everyone, last time we talked about Java polymorphism example, this time we say the example is Java Object-oriented synthesis exercise. Stop gossiping and return to the truth. Let’s Talk Android!

Look at the officers, we have introduced the object oriented content in the previous chapter, and most of it is conceptual or theoretical content, the code is pseudo-code, it is estimated that many of the officers want to try their own hands. In this chapter, we will try out object-oriented content with you.

public class oop {
    public static void main(String args[])
    {
        Father father = new Father(60."Zhang DaGou");
        Father.familyName = "Zhang";
        Son son = new Son(30."Zhang XiaoGou");

        System.out.println("Father and Son have the same family name:"+Father.familyName);
        System.out.println("Father is:"+father.getName()+", Father age is:"+father.getAge());
        father.mySon();
        father.myDaughter();
        System.out.println("Son is:"+son.getName()+", Son age is:"+son.getAge());
        son.mySon();
        son.myDaughter(); // call the func of Class Father
        son.myDaughter("Zhang MaoMao");
        son.printWork();
        System.out.println();

        System.out.println("----------- Before changing the type of object -----------");
        //son = father; // this is wrong
        System.out.println("father instanceof Father is:"+(father instanceof Father));
        System.out.println("father instanceof Son is:"+(father instanceof Son));        
        System.out.println("son instanceof Father is:"+(son instanceof Father));
        System.out.println("Son instanceof Son is:"+(son instanceof Son));

        father = son;   
        System.out.println("----------- After changing the type of object -----------");

        System.out.println("father instanceof Father is:"+(father instanceof Father));
        System.out.println("father instanceof Son is:"+(father instanceof Son));        
        System.out.println("son instanceof Father is:"+(son instanceof Father));
        System.out.println("Son instanceof Son is:"+(son instanceof Son));

        System.out.println();   
        father.mySon(); // call the func of Class Son
        father.myDaughter(); // call the func of Class Father
        //father.myDaughter("Zhang MaoMao"); //this is wrong, it can't call the func of Class Son       }}class Father {
    public int age;
    public String name;
    public static String familyName;

    public Father(int a, String n){
        age = a;
        name = n;
    }

    public int getAge(a){
        return this.age;
    }

    public String getName(a){
        return this.name;
    }   

    public void mySon(a){ 
        System.out.println("My son is Zhang XiaoGou");
    }

    public void myDaughter(a){
        System.out.println("I have not daughter"); }}class Son extends Father implements Work{
    public Son(int a, String n){
        super(60."MyFather");
        this.age = a;
        this.name = n;      
    }

    // Overriding this function
    public void mySon(a){ 
        System.out.println("My son is Zhang XiaoXiaoGou");
    }

    // Overloading this function
    public void myDaughter(String name){  
        System.out.println("My daghter is not born,but we make a name for her: "+name);
    }

    public void printWork(a)
    {
        System.out.println("I am working at Zoo"); }}interface Work{
    public void printWork(a);
}
Copy the code

As you can see from the code above, we define two classes: Father and Son; One interface: Work. These three encapsulate the corresponding member variables and member functions, as you can see from the code, but we won’t list them all. When defining the Son class, it inherits the Father class and implements the Work interface. Son implements abstract functions in the Work interface. Of course, the functionality is relatively simple, just a print statement, but you need to get a sense of how to implement the interface. Son inherits all of Father’s member variables and functions, overwriting mySon() as well. It also overrides the myDaughter() function. You need to see the difference between overwriting and overloading. And then we determine the types of father and son, and we print out the types before and after the conversion, and you can see that in the program result. After determining the type, you can convert the type of the object, which is a manifestation of polymorphism.

Another manifestation of polymorphism is to call a function of a subclass. The code calls the overridden function mySon(). The following is the running results of the program, please refer to:

Father and Son have the same family name:Zhang
Father is:Zhang DaGou, Father age is:60
My son is Zhang XiaoGou
I have not daughter
Son is:Zhang XiaoGou, Son age is:30
My son is Zhang XiaoXiaoGou
I have not daughter
My daghter is not born,but we make a name for her: Zhang MaoMao
I am working at Zoo

----------- Before changing the type of object -----------
father instanceof Father is:true
father instanceof Son is:false
son instanceof Father is:true
Son instanceof Son is:true
----------- After changing the type of object -----------
father instanceof Father is:true
father instanceof Son is:true
son instanceof Father is:true
Son instanceof Son is:true

My son is Zhang XiaoXiaoGou
I have not daughter
Copy the code

See officers, the above code belongs to the more basic code, we can combine the code annotations, as well as the results of the code to analyze the meaning of the code. I hope you can understand the meaning of object orientation: encapsulation, inheritance and polymorphism. In addition, the code in the name, age and so on are virtual construction, the cat cat dog family is very lively ah, we look happy.

Everyone see officer, about Java object-oriented comprehensive exercise examples we are introduced here, want to know what examples there are behind, and listen to the next decomposition!