Java basic inheritance (taking the implementation of a Database for storing CD s and DVD s as an example)

Posted by AbiusX on Wed, 19 Jan 2022 19:42:34 +0100

In Database Java file implements a Database class, which contains the member variable list of ArrayList type to store data. In addition, there are add(Item newItem) and list() methods to add and traverse the list.

//Database.java
package database;

import java.util.ArrayList;

public class Database {
    private ArrayList<Item> list = new ArrayList<>();
    public void add(Item newItem)
    {
        list.add(newItem);
    }

    public void list(){
        for(Item item:list)
        item.print();
    }
    public static void main(String[] arg) {
    Database db = new Database();
    db.add(new CD("123","123",123,123,"123"));
    db.list();
    }
}

As a parent class, Item contains only the most basic member variables and member functions.

//Item.java
package database;

public class Item {

    private String title;
    private int playingTime;
    private boolean gotit = false;
    private String comment;

    public Item(String title, int playingTime, String comment) {
        this.title = title;
        this.playingTime = playingTime;
        this.comment = comment;
    }

    public void print()
    {
        System.out.print(title);
    }
}

CD is a class derived from Item. It adds many properties on the basis of Item and overrides the print() method. Call Item. in Database class When using the print() method, if the subclass overrides the method, only the in the subclass will be called.

//CD.java
package database;

public class CD extends Item {
    private String artist;
    private int numofTracks;

    public void print() {
        System.out.print("CD:");
        super.print();
        System.out.println(artist);
    }

    public CD(String title, String artist, int numofTracks, int playingTime, String comment) {
        super(title, playingTime, comment);
        this.artist = artist;
        this.numofTracks = numofTracks;
    }
}

What do subclasses inherit

The private attribute of the parent class can only be accessed by itself, and the protected attribute can also be accessed by subclasses and other classes in the same package.

Variables in the parent class can be initialized in the constructor of the child class by setting them to protected. However, a better method is to initialize with the constructor of which class the variable belongs to. At this time, it can be passed to the parent class constructor with the help of the super() method of the child class. The initialization sequence is: 1 Parent class construction (if super() parameter is not given or super() is not called in the subclass, the default constructor of the parent class will be called automatically; If the super() parameter is given, call the constructor corresponding to the parent class); 2. Define initialization; 3. Subclass structure.

Complex relationship between subclass and parent

If the subclass and the parent class contain member variables with the same name, the variables of the parent class will be hidden in the subclass. The function of the parent class deals with the variables of the parent class. Use super f() can call the f() function in the parent class.

Override is different from Overload. It is a re implementation of a method in the parent class rather than an extension. Adding @ override before the overridden method allows the compiler to help check.

Subclasses and subtypes

  • Class defines the type
  • Subclasses define subtypes
  • Objects of subclasses can be used as objects of parent classes
    • Variable assigned to parent class
    • Passed to a function that requires a parent object
    • Put it in the container where the parent object is stored

Polymorphic variable

Java's object variables are, and they can hold more than one type of object

They can hold objects that declare types, or objects that declare subclasses of types

This happens when the object of the subclass is assigned to the variable of the parent class. After the upward modeling, although the variable is of the parent class type, the method of the subclass is still called (for example, when the above container storing Item type performs a for each loop and calls the method, the overridden method in the subclass is called instead of the method of the parent class). Note: downward modeling is not always feasible. The only feasible situation is that the object actually managed by the parent type variable is the child type.

If the function needs an ArrayList with an Animal type parameter, it can only be passed in instead of being. Otherwise, the declaration of the function can only be changed to

public <T extends Animal> void takeThing (ArrayList<T> list)

perhaps

public void takeThing (ArrayList<? extends Animal> list)

Note: it is very complicated to implement this in C + +, which can be implemented with the new content of C++ 20:

template<typename T>
concept Another = (std::is_base_of<Animal, T>::value);
template <typename T>
void bark(vector<T*> something) requires Another<T>{}