Java Object-Oriented Learning Notes

constructor

Parametric constructs: once a parametric construct is defined, a parametric construct must display the definition

Constructor:

  1. Same as class name
  2. no return value

Effect:

  1. new is essentially calling a construction method
  2. Initialize the value of the object

Encapsulation (property private get/set)

  1. Enhance program security and protect data
  2. Hide implementation details of code
  3. Unified Interface
  4. System maintenance enhancements
class Student{
    //Property private
    private int id;
    private String name;
    private String sex;

    //Use get, set for external use
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

Inheritance (Private things cannot be inherited, only single inheritance in java classes! No more inheritance!!)

  1. If a child inherits the parent class, it will have all its methods
  2. In java, all classes inherit Object directly by default (ancestor class)
  3. extends means extension, and subclasses are extensions of parent classes
  4. Hide the code and call the parameterless construction of the parent class.super();Parent constructor, must be on the first line of child constructor
  5. super and this cannot call construction methods at the same time

this and super Notes:

  1. Represents objects differently; this calls itself; super represents the application of parent objects
  2. this can be used without inheritance; super can only be used under inheritance conditions
  3. The construction method of this() class; the construction method of super() parent class
public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.say();//Since students inherit Person, you can use the Person class method!
    }
}

class Student extends Person{
    //The student inherited Person
}

class Person {
    public void say(){
        System.out.println("A sentence was said");
    }
}

Polymorphism (inheritance relationship, subclass override parent method, parent reference pointing to subclass object)

  • Note: Polymorphisms are polymorphisms of methods, and attributes do not have polymorphisms
public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.say();//Since students inherit Person, you can use the Person class method!

        //References to parent classes point to subclasses!
        Person person = new Student();
        person.say();
    }
}

class Student extends Person{
    public void say(){
        System.out.println("Subclass overrides parent's say()Method!");
    }
}

class Person {
    public void say(){
        System.out.println("A sentence was said");
    }
}

Abstract class (abstract class can not be new, it can only be implemented by subclasses, single inheritance! Only interfaces can inherit more)

  1. Abstract, abstract method only has method name, no method implementation
  2. Common methods can be written in abstract classes
  3. Abstract methods must be in abstract classes
  4. All methods of an abstract class must be implemented by a subclass unless the subclass is also abstract
public class Application {
    public static void main(String[] args) {
        //Abstract classes cannot be new out!
    }
}

//Define abstract classes
abstract class Action {
    //Define abstract methods
    public abstract void doSomething();
}

Interface

  1. Keyword defined by interface
  2. Interface needs implementation class!!!
  3. All definitions in an interface are abstract
  4. Class implements interface implements interface
  5. Classes that implement interfaces must override methods in the interface
  6. Interfaces can inherit multiple interfaces, and a class can inherit multiple interfaces
  7. Abstraction cannot be instantiated! Because of abstraction, there are no constructors in the interface!!!
public class Application {
    public static void main(String[] args) {
        //
    }
}

//interface defines interfaces, interfaces need to implement classes!!!
interface UserService {
    //Methods for defining interfaces
    void add(int no,String name,String sex);
    void delete(int no);
}

//Second interface
interface TimeService{
    void getTime();
}

//Implementation class; interface implementation class can inherit multiple interfaces!
class UserServiceImpl implements UserService,TimeService{
    //The method of the interface must be overridden!
    @Override
    public void add(int no, String name, String sex) {
        //Method Body
    }

    @Override
    public void delete(int no) {
        //Method Body
    }

    @Override
    public void getTime() {
        //Method Body
    }
}

Rewrite

  1. Need inheritance, subclass overrides parent's method
  2. Method names must be the same
  3. Parameter list must be the same
  4. Modifier, which can be expanded but not reduced: public>protected>default>private
  5. Override, the method of the subclass should be the same as the parent; the body of the method is different
  6. Why should I override it?: The functions of the parent class, the subclasses, are not necessarily required or satisfied

Be careful:

  1. Static methods cannot be overridden, only non-static methods can be overridden
  2. Final constants and private methods cannot be overridden and cannot be inherited once they are final parents
  3. There is no polymorphism without rewriting!
public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.say();//Since students inherit Person, you can use the Person class method!
    }
}

class Student extends Person{
    public void say(){
        System.out.println("Subclass overrides parent's say()Method!");
    }
}

class Person {
    public void say(){
        System.out.println("A sentence was said");
    }
}

Tags: Java OOP

Posted on Sat, 25 Sep 2021 13:20:53 -0400 by audiodef