Inheritance between classes and objects and classes in Java

  Object oriented overview

In the object-oriented definition, some basic features are also specified:
(1) Encapsulation: protect the internal operation from damage;
(2) Inheritance: continue to expand on the original basis;
(3) Polymorphism: concept transformation within a specified range.

Object oriented development process: OOA (object oriented analysis), OOD (object oriented design), OOP (object oriented programming)

  • Object: an object is an instance of a class. It has its own properties and behaviors (Methods). There are objects before classes.
  • Class: a class is a collection of multiple objects. It describes the behavior and state of a class of objects.

Example display

First, create a teacher's class

public class Teather {
//    Name, age, gender, professional title, Department, Party member or not, and courses taught;
//    Methods: class;
    protected String name;
    protected String age;
    protected String sex;
    protected String rank;
    protected String college;
    protected String isPartyMember;
    protected String SelectedCourse = "";

//Create the set and get methods of the teacher class
 public void setName(String value) {
        name = value;
    }


    public void setAge(String value) {
        age = value;
    }

    public void setSex(String value) {
        sex = value;
    }

    public void setRank(String value) {
        rank = value;
    }

    public void setCollege(String college) {
        this.college = college;
    }

    public void setIsPartyMember(String isPartyMember) {
        this.isPartyMember = isPartyMember;
    }



    public void setSelectedCourse(String selectedCourse) {
        System.out.println("You are selecting the course of " + selectedCourse + "!");
         if(SelectedCourse !=" ")
        SelectedCourse = SelectedCourse + "," + selectedCourse;
        else
        SelectedCourse = SelectedCourse  + selectedCourse;
    }

    public String getName() {
        return name;
    }
    public String getAge() {
        return age;
    }
    public String getSex() {
        return sex;
    }
    public String getRank() {
        return rank;
    }

    public String getSelectedCourse() {
        return SelectedCourse;
    }



    public String getIsPartyMember() {
        return isPartyMember;
    }


    public String getCollege() {
        return college;
    }

    public void deliverCourse(String selectedCourse) {
        System.out.println("You are delivering " + selectedCourse + "!");
        SelectedCourse = SelectedCourse + "," + selectedCourse;
    }
}

Then create a teacher's class to inherit the teacher's class (overload and rewrite at the same time)

public class Supervisor extends Teather{
//The tutor class inherits the teacher class
//At the same time, add the attributes in the tutor
    protected String direction;
    protected String tClass;
//Create a study method in the tutor class
public void study(String tDirection){
        System.out.println("You are studying the subject"+tDirection+". ");
    }
   //Overloading the study method
    public void study(String tDirection,String tRank){
        System.out.println("You're studying"+tRank+tDirection+". ");
   //Rewrite the deliverCourse method in the teacher class (changing delivery to studying)
 public void deliverCourse(String selectedCourse) {
        System.out.println("You are studying " + selectedCourse + "!");
        SelectedCourse = SelectedCourse + "," + selectedCourse;
    }

}

  Tip: in java, whether attributes, methods or classes need modifiers as prefixes

The Java language provides many modifiers, which are mainly divided into the following two categories:

  • Access modifier
  • Non access modifier (omitted here)

  Access modifier

In Java, access control characters can be used to protect access to classes, variables, methods, and constructor methods. Java supports four different access rights.

  • default   (i.e. default, write nothing): visible in the same package without any modifiers. Use objects: classes, interfaces, variables and methods.

  • private  : Visible within the same class. Use objects: variables, methods.

  • public  : Visible to all classes. Use objects: classes, interfaces, variables, methods

  • protected  : Visible to classes and all subclasses in the same package. Use objects: variables, methods..

  Note: all variables in the interface are implicitly declared as   public static final, and the access permission of the methods in the interface is by default   public

create object

Objects are created based on classes. In Java, use the keyword new to create a new object. Creating an object requires the following three steps:

  • Declaration: declare an object, including object name and object type.
  • Instantiation: use the keyword new to create an object.
  • Initialization: when using new to create an object, the constructor will be called to initialize the object.

Class inheritance

Classification: class inheritance class and class inheritance interface

Class inheritance class

Format: public class subclass extends parent class {class body}

example:

public class Student extends Person{
    void study(){
        System.out.println("student study..." + age);
    }
}

be careful:

  • The subclass has non private attributes and methods of the parent class (generally, the parent class modifies attributes with private,
                    Modify the method with public, and set the property in the subclass with method)
  • Subclasses can extend the parent class and override the methods of the parent class (overridden access modifier permissions)
                    Should be greater than or equal to the parent class) permission size public > protected > Default > private
  • super represents the parent class and this represents this class
  •   All classes inherit the object class

Features: single inheritance, multiple inheritance, but not multiple inheritance

Class inheritance interface

Format: public class class name implements interface, interface,... {class body}

example:

public class DrawMouse implements MouseListener{

 public void mouseClicked(MouseEvent e){
};
 public void mousePressed(MouseEvent e){
};
 public void mouseReleased(MouseEvent e){
};
 public void mouseEntered(MouseEvent e){
};
 public void mouseExited(MouseEvent e){
};
//All methods in the interface need to be rewritten


}

characteristic:

  •   Abstract methods of interfaces need to be rewritten
  • The attributes in the class have final and static attributes by default, that is, the variable cannot be changed after initialization
                   

Tags: Java Back-end

Posted on Tue, 30 Nov 2021 15:29:53 -0500 by aufkes