day06 object oriented 03

Posted by tawevolution on Fri, 14 Jan 2022 09:05:52 +0100

day06 inherits features and abstract classes

Object oriented knowledge

Definition: the behavior of the same object shows different effects at different times or conditions

Conditions for polymorphism:

  1. inherit
  2. Method coverage
  3. A parent class reference points to a child class object

eg:

cat cat = new Persian cat();

Access characteristics of polymorphic members:

  • Member variables: compile and run results on the left (not related to polymorphism)
  • Member method: the compilation is on the left (the methods that can be executed are related to the left), and the running results are on the right

be careful:

1.Polymorphism is the polymorphism of methods,The attribute is not polymorphic
2.Parent and child classes can only be referenced if they are related, otherwise type conversion exceptions will occur! ClassCastException!
3.Existing conditions: there is an inheritance relationship, the method needs to be overridden, and the parent class reference points to the object of the child class Father f1 = new Son();

Methods that cannot be overridden:

1.static Method cannot be overridden and does not belong to an instance
2.final Decorated cannot be rewritten
3.private method
public static void main(String[] args) {

    //The actual type of an object is determined, eg:
    //new Student();
	//The type of reference that can be pointed to is uncertain: the reference of the parent class points to the child class
    
    A a = new A();

    //All the methods that A can call are its own or inherit from parent class B
    //Type B objects can only call the methods of the parent class, not the methods unique to the child class
    B b = new A();
    a.test();
    b.test();
}

class B {
    public void test(){
        System.out.println("B=>test");
    }

}

class A extends B {
    @Override       //Verify an annotation of the override
    public void test(){//This is when the subclass overrides the method of the parent class
        System.out.println("A=>test");
    }
}

Benefits and disadvantages of polymorphism:

  1. It improves the scalability of the program, and only needs to (ensure that the sound of that animal can be sent to any type of animal)
  2. The Maintainability (inheritance) of the program is improved, that is, the redundant code is reduced

Disadvantages:

  • Cannot access the special functions of subclasses - > see the left for compilation

instanceof keyword

A instanceof B That is, reference variables instanceof The class name of the target class

The result is true: The value of the reference variable must be the object of the target class
 The result is false: Indicates that the object referencing the variable is not an object of the target class

Note: for null For example, null instanceof The object running results of the target class are false

effect:

Used to judge A and B To determine whether to cast

Type conversion

Type conversion: conversion from 64 bits to 32 bits does not require cast, while conversion from 32 bits to 64 bits requires cast
 Here, the type conversion of parent and child classes is also similar:
//When an object of Person type wants to access a Student method, it must force type conversion, that is (eg: the method defined in the object of Person type is go() and the method defined in the object of Student type is run())
Person student = new Student();
//Here, if you want to access run(), you need to cast
((Student) student).run();
//When a subclass is converted to a parent class, there is no need to cast, but some methods may be lost

Object oriented 12 abstract classes

definition

  • Abstract classes are modifiers
public abstract class AbstractClass {
    public abstract void dosomething();//There's no {} here. Pay attention!
}

At this time, if the subclass wants to inherit the parent class, it must rewrite function A (parent debt and child compensation), otherwise an error will be reported

public class TestAbstract extends AbstractClass {
    @Override
    public void dosomething() {   
    }
}

matters needing attention:

  1. You can't use the abstract class new. You can only rely on subclasses to implement it
  2. Ordinary methods can be written in abstract classes
  3. Abstract methods must be in abstract classes
  4. Abstract keyword cannot coexist with private, final and static keywords (reason: methods modified by abstract keyword need to be overwritten by subclass methods, but member methods and member variables modified by these three keywords cannot be overridden)

Subclass characteristics of abstract class:

  1. Subclasses of abstract classes can be concrete classes
  2. It can also be an abstract class (parent debt and child repayment)

Membership of abstract classes

  1. Member variable: it is no different from the member variable of ordinary class
  2. Construction method: it is no different from the construction method of ordinary classes
  3. Member methods: different from ordinary class methods: abstract methods (declaring a behavior, but not knowing how to implement it, just like defining a cry in an animal class), non abstract methods (if we know how to implement the behavior, we define non abstract methods to implement it for code reuse)

Object oriented 13 interface

Keyword representation of interface: Format:

interface Interface name{}
  1. There is a relationship between classes
  2. Characteristics of interface
    • Instantiation feature
      • Interfaces cannot be instantiated directly, but they can be instantiated concisely
      • Only abstract methods can exist in an interface

Common class: only concrete implementation

Abstract class: both concrete implementation and specification

Interface: only specification! I can't write professional constraints of methods! Separation of constraints and Implementation: interface oriented programming

Function of interface:

  1. The member method has public abstract by default
  2. Member variables have public static final by default
  3. Interface cannot be instantiated
  4. There is no constructor in the interface
  5. You must override the methods in the interface
  6. A class that can implement multiple interfaces through implementation
  7. Multiple inheritance can be realized between interfaces
  8. Complete class definition syntax:
    • Modifier

java programming ideas describes multiple inheritance

Comparison of abstract classes and interfaces

  1. Member difference

    • Abstract class: variable and constant, including abstract method, ordinary method, constant and variable
    • Constant, abstract method, (jdk8: default method (no need to be overridden), static method (tool method))
  2. Relationship difference

    Class and class inheritance
    Class and interface implementation, single implementation, multiple implementation
    Interface and interface inheritance, single inheritance, multiple inheritance

  3. Difference between design concepts abstract classes are the concept of is a, while interfaces are the concept of like a (because interfaces can be inherited multiple times, it can not be said that a subclass is this interface)

numberDistinguishing pointsabstract classInterface
1definitionClass containing abstract methodsA collection of abstract methods and global constants
2formConstruction method, abstract method, ordinary method, constant, variableConstant, abstract method, (jdk8: default method, static method)
3useSubclasses inherit abstract classes (extensions)Subclass implements interfaces
4relationshipAbstract classes can implement multiple interfacesInterfaces cannot inherit abstract classes, but multiple interfaces are allowed
5objectCannot create object, but has construction methodYou cannot create an object and there is no construction method
6limitAbstract classes cannot be inherited by more than one classInterfaces can be inherited and implemented in multiple ways
7thoughtAs a template or abstraction of commonness, is-aAs a standard or abstraction of common capabilities, like-a

Object oriented internal class

Member inner class: external class name Internal class name object name = external class object Internal class object, which can access private variables inside members

Anonymous inner class: new class name or interface name () {override method;}

Static inner class

Topics: Java Back-end