Object oriented features
● three characteristics of object-oriented language
encapsulation
inherit
polymorphic
Object oriented features - encapsulation
● concept of packaging
• encapsulation: some information of the class is hidden inside the class, which is not allowed to be directly accessed by external programs, but through the methods provided by the class
Operation and access of hidden information.
• benefits of encapsulation:
1. It can only be accessed through the specified method
2. Hide the implementation details of the class
3. It is convenient to add control statements
4. Convenient modification and Implementation
• specific performance - use different access rights
public class Demo{ private String name; public String getName (){ return name; } public void setName(String name){ this.name = name; } }
Object oriented feature - inheritance
● inheritance: inheritance is an indispensable design idea of object-oriented programming, the foundation for realizing code reusability and improving code extensibility
Main ways of sex
● inheritance is to derive a new class from an existing class. The new class can absorb the properties and behaviors of the existing class and expand new capabilities
● use the extends keyword in JAVA to represent the inheritance relationship
● JAVA does not support multiple inheritance. Single inheritance makes the inheritance relationship of JAVA very simple. A class can only have one direct parent class
● after inheritance, the subclass can call all non private properties and non private methods of the parent class
● form of inheritance:
•Access modifier Subclass name extends parent class name {subclass body}
public class Animal{ public void eat(){} } public class Dog extends Animal{ public void play(){} } //Subclass objects can directly call the methods of the parent class, emphasizing reusability Dog dog = new Dog (); dog.eat();
● transmissibility of inheritance:
If class C inherits from class B and class B inherits from Class A, class C has all non private properties and non private methods of class B and class A
When a class does not inherit any class, the jvm will let the class inherit the object class by default. Object is the base class provided by java for all classes
● construction method in inheritance:
The subclass constructor will call the parent constructor first
If you use the super keyword to call any constructor of the parent class, it must be written in the first line of the constructor
If the base class constructor is not explicitly called in the subclass constructor, the system will call the base class parameterless constructor by default.
● super keyword purpose
• use the super keyword to access parent class members
• use super Member variable name to refer to the parent class member variable
• use super Method name (parameter list) the method that accesses the parent class
• use super Constructor (parameter list) accesses the constructor of the parent class
Myth: don't mistake super for a parent object
When you create a subclass object, the parent object is not created
Only the information in the parent class will be loaded into the child class object for storage
● method override
• application scenario: when the method implementation of the parent class cannot meet the requirements of the child class, the method can be overridden
• in subclasses, you can override the methods inherited from the base class as needed
● method rewrite rules
• same method name and parameter list
• the return value type is the same
• the access permission cannot be less than the parent permission
Note: static methods cannot be overridden for construction methods, and member variables cannot be overridden
abstract class
● abstract methods
• an abstract method is a special method: it has only a declaration, not a concrete implementation
• abstract methods must be decorated with the abstract keyword
● if a class does not contain enough information to describe a specific object, such a class is an abstract class.
● except that the abstract class cannot instantiate objects, other functions of the class still exist, including member variables, member methods and construction methods
● a class modified with abstract is an abstract class. If a class contains abstract methods, the class must be defined as an abstract class
● features:
• abstract classes cannot be instantiated, but can have construction methods. Because abstract classes contain methods without concrete implementation, objects cannot be created with abstract classes
• abstract classes can only be used as base classes, representing an inheritance relationship. A non abstract class that inherits an abstract class must implement all the abstract methods in it. The parameters and return values of the implemented methods must be the same as those in the abstract class. Otherwise, the class must also be declared abstract
● use the keyword abstract to define abstract classes. General syntax:
[Access rights] abstract class Class name {Member list} public abstract class Shapes { public abstract void draw(); } public abstract class Shapes { public void draw(){ //Specific code } }
● abstract classes and abstract methods are design level concepts in the process of software development. In other words, designers will design abstract classes,
Abstract methods, programmers inherit these abstract classes and override abstract methods to realize specific functions.
Object oriented feature polymorphism
● polymorphism
The same thing shows different states at different times
● three necessary conditions for the existence of polymorphism
• inheritance (including interface implementation) (prerequisite)
• rewriting (prerequisite)
• parent class references point to child class objects
● when the compile time type is a parent class and the run-time type is a child class, it is called a parent class reference and points to a child class object
class Animal{ ...... }class Cat extends Animal{ ...... }class Dog extends Animal { ...... }Animal x = new Cat() //The reference of Animal points to the object of Cat
● calling member methods in polymorphic environment
class Animal{ void show() { System.out.println("Anmial"); } } class Cat extends Animal{ void show() { System.out.println("cat"); } } Animal x = new Cat() x.show() //The method in the subclass is called
Simply put: compile to the left and run to the right
● calling static member methods in polymorphic environment
class Animal{ static void show() { System.out.println("Animal"); } }class Cat extends Animal { static void show() { System.out.println("Cat"); } } ...... Animal x = new Cat() x.show() //The static member method in the animal class is called
Simply put: compile and run on the left
● calling of member variables in polymorphic environment
class Animal{ int num = 3; }class Cat extends Animal { int num = 4; }...... Animal x = new Cat() x.num; //Called is a member variable in the animal class
To put it simply: Compilation and operation are based on the left side of the equal sign. Note: variables are not overwritten by subclasses, but only methods
● method parameters are polymorphic
class Animal{ void eat() {} }class Cat extends Animal{ void eat() {} }class Dog extends Animal{ void eat(){} } //The formal parameter type of the method is the parent type, and the actual parameter passed can be the object of any subclass method(Animal animal){ animal .eat(); }
The benefits of method parameter polymorphism: improve the scalability of the code
● upward transformation
class Animal{ void eat(){ } }class Cat extends Animal{ void look() { System.out.println("Housekeeping"); } } ......... Animal x=new Cat() //Shape up, and the Cat object is promoted to an Animal object x.eat() //Only methods in the parent class can be used x.look() //report errors! Methods in subclasses cannot be used
● downward transformation
class Animal{ void eat(){ } }class Cat extendsAnimal{ void look() { System.out.println("Housekeeping"); } } ......... Animal x=new Cat()Cat m=(Cat)x; //Downward transformation m.eat(); m.look();//Methods in child and parent classes can be used
The purpose of the downward transformation is to use the unique methods in the subclass
final keyword
● final is used to declare properties, methods and classes
• attribute: the definition must be assigned directly or in the construction method, and cannot be modified later
• methods: cannot be overridden in subclasses
• class: cannot be defined as abstract class or interface, and cannot be inherited
private int index; private static final double pai=3.14; private final int level; public Test(){ level=0; } public Test(int index){ this.index=index; level=1; }
● final attribute assignment
• assign values at the same time when declaring, which is often used together with static
• there is no assignment at the time of declaration, and it must be assigned one by one in the construction method
• general principle: ensure that the value of the final attribute is determined when creating each object
● final modification of parameters
• add the final keyword in front of the method parameters to prevent data from being modified in the method body
public class Ex{ private int a = 1; private String b; public void ww(final int a){ a=12; } }
Interface
● it can be implemented using Java interface
The USB interface itself does not realize any function
The USB interface specifies the requirements for data transmission
The USB interface can be implemented by a variety of USB devices
Write USB interface -- > design method according to requirements
Implement USB interface -- > implement all methods
Use the USB interface -- > in a polymorphic way
● interface oriented programming
During programming:
Care about the capabilities of the implementation class, not the implementation details
Interface oriented conventions without considering the specific implementation of the interface
• meaning of interface: a class in java can only have one parent class, so the interface can realize the logic of multiple inheritance
• in essence, an interface is a special abstract class that contains abstract methods
● get to know the interface
public interface MyInterface { int num = 10; public void foo(); public static void test(){ } public default void test1(){ } }
Definition and use of interfaces
Interface definition: use the interface keyword to declare an interface
[access modifier] interface interface name [extends other interface names 1,... Other interface names n]
{
//Declare constant abstract method static method default method
}
Use of interface: the class uses the implements keyword to implement the interface. In the class declaration, the implements keyword is placed after the class declaration.
[access modifier] class name implements interface name 1, interface name 2... {}
Combined inheritance:
[access modifier] class class name extends parent class name implements interface name 1, interface name 2... {}
Characteristics of interface
• interfaces are implicitly abstract. When declaring an interface, you do not need to use the abstract keyword
• methods in the interface can be abstract, static and default
• the attributes declared in the interface are public static final by default
• the interface is not inherited by the class, but implemented by the class
• the interface cannot instantiate an object and has no construction method
• a class can implement multiple interfaces
• similar to inheritance, there is polymorphism between interfaces and implementation classes
• one interface can inherit multiple other interfaces
• when a class implements an interface, the class should implement all abstract methods in the interface. Otherwise, the class must be declared abstract