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:
- inherit
- Method coverage
- 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:
- 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)
- 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:
- You can't use the abstract class new. You can only rely on subclasses to implement it
- Ordinary methods can be written in abstract classes
- Abstract methods must be in abstract classes
- 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:
- Subclasses of abstract classes can be concrete classes
- It can also be an abstract class (parent debt and child repayment)
Membership of abstract classes
- Member variable: it is no different from the member variable of ordinary class
- Construction method: it is no different from the construction method of ordinary classes
- 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{}
- There is a relationship between classes
- Characteristics of interface
- Instantiation feature
- Interfaces cannot be instantiated directly, but they can be instantiated concisely
- Only abstract methods can exist in an interface
- Instantiation feature
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:
- The member method has public abstract by default
- Member variables have public static final by default
- Interface cannot be instantiated
- There is no constructor in the interface
- You must override the methods in the interface
- A class that can implement multiple interfaces through implementation
- Multiple inheritance can be realized between interfaces
- Complete class definition syntax:
- Modifier
java programming ideas describes multiple inheritance
Comparison of abstract classes and interfaces
-
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))
-
Relationship difference
Class and class inheritance
Class and interface implementation, single implementation, multiple implementation
Interface and interface inheritance, single inheritance, multiple inheritance -
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)
number | Distinguishing points | abstract class | Interface |
---|---|---|---|
1 | definition | Class containing abstract methods | A collection of abstract methods and global constants |
2 | form | Construction method, abstract method, ordinary method, constant, variable | Constant, abstract method, (jdk8: default method, static method) |
3 | use | Subclasses inherit abstract classes (extensions) | Subclass implements interfaces |
4 | relationship | Abstract classes can implement multiple interfaces | Interfaces cannot inherit abstract classes, but multiple interfaces are allowed |
5 | object | Cannot create object, but has construction method | You cannot create an object and there is no construction method |
6 | limit | Abstract classes cannot be inherited by more than one class | Interfaces can be inherited and implemented in multiple ways |
7 | thought | As a template or abstraction of commonness, is-a | As 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