“This is the sixth day of my participation in the First Challenge 2022. For details: First Challenge 2022”

One, foreword

We learned Java methods last time, and now we’re going to learn a new one, which is also an important one in Java

Object-oriented fundamentals are at the heart of Java.

Object – oriented mainly includes encapsulation, inheritance and polymorphism

This section focuses on encapsulation, but before we do that, let’s look at the definition and relationship between classes and objects

What is an object?

Everything in the world is an object, everything interprets an object, and an object is something visible and tangible.

What is a class?

Class is a general term for the same class with properties and behaviors in real life.

Class and object relationships?

A class is an abstraction of an object, and an object is an instantiation of a class

Class definition?

Class is a basic concept in Java, which is based on classes and composed of classes

How do I create objects?

Creating objects is a common form in Java

Format:

Class name Object name =new class name (); Example Phone p=new Phone();

What does creating an object do?

By creating an object, instantiating the class, we can access all the members of that class through the object we created and we can modify it

The member variable accessed. (Excluding private modified members)

Create two classes,

Under the Phone class:

PhoneDemo under:

Under the Phone class:

public class Phone { public int price=3999; Public String brand=" Huawei "; Public static void playGame(){system.out.println (" this phone can play games "); }}Copy the code

PhoneDemo under:

public class PhoneDemo { public static void main(String[] args) { Phone p=new Phone(); // Create an object system.out.println (p.rand); System.out.println(p.price); p.playGame(); }}Copy the code

Above we demonstrated the function of creating objects.

I’m not going to show you that by modifying the object.

Second, the encapsulation

Encapsulation encapsulates a member of a class by making it private so that it cannot be changed, and then provides a get/set method to encapsulate the lock

Members of and assignment limits.

The benefits of encapsulation?

Encapsulation makes the data more organized, and we provide conditions for filtering and judging the values of the external input, such as

Set an age, we can set the range of age, and age non-negative, and give the corresponding prompt.

Encapsulation illustration:

Under the Student class:

Code:

public class Student { private String name; private int age; Public Student(String name,int age){// Public Student(String name,int age){// Public Student(String name,int age){// Public Student(String name,int age){// Public Student(String name,int age){// Public Student(String name,int age){// Public Student(String name,int age){// Public Student(String name,int age){// Public Student(String name,int age){// Public Student(String name,int age); this.age=18; this.name=name; } else{ this.age=age; this.name=name; }} public void setName(String name){//get/set this.name=name; } public String getName(){ return name; } public void setAge(int age){if(age<0){system.out.println (); this.age=18; }else{ this.age=age; } } public int getAge(){ return age; }}Copy the code

StudentDemo class:

Code:

public class StudentDemo { public static void main(String[] args) { Student s=new Student(); // create s.age (20); S.s etName (" * * "); System.out.println(s.getName()+" ,"+s.getAge()); S =new Student(" 四",-1); / / create the object assignment System. At the same time out. The println (s.g etName () + ", "+ s.g etAge ()); }}Copy the code

We can also type in age with Sanner on the keyboard and we won’t do it here

Second, inheritance,

What is inheritance?

Inheritance is also very common in display life, such as inheritance of property and so on, we also have similar use in Java learning,

The inheritor is called a subclass or derived class. The inheritor is called a parent class, a base class, or a superclass. The objec class is the parent class of all classes

(Later introduction)

Advantages and disadvantages of inheritance

** Benefits: ** is to improve the maintainability of code (multiple code changes need to be made, only need to fix this one).

Improved code reuse (multiple members of the same class can be called into the same class)

Disadvantages: The disadvantage of inheritance is that it makes code highly coupled, and changes to the parent and child classes will also change

Use scenarios for inheritance?

Two that have a subordinate relationship, such as cat and animal, student and person, etc.

The keyword for inheritance is extends.

Inherited format:

Public class extends extends parent class {}Copy the code

For example:

Public class Cat extends Animal{} public class Cat extends Animal{Copy the code

Characteristics of inheritance:

A subclass can own the parent’s non-private methods and member variables, and it can override the parent’s non-private (private-modified) methods. All subclass methods access the parent class’s no-argument construct by default

Rewriting concepts:

Rewriting is a restatement of non-private methods in the parent class by subclass. The general characteristics of rewriting are the same method name, same format, same return type and different method body

Such as:

Fu class:

Public class fu{public void play(){system.out.println ("fu likes playing badminton "); }}Copy the code

Zi class:

Public class zi extends fu{public void play(){system.out.println ("zi likes playing basketball "); }}Copy the code

You can also write that the zi class has access permissions greater than or equal to the FU class

The default modifier for fu classes is:

Public class fu{void eat(){system.out.println (" foo eat method "); }}Copy the code

Zi class:

Public class Zi extends Fu {@override public void eat() {system.out.println (" eat in Zi "); }}Copy the code

Zi class overwrite:

Public class Zi extends Fu {// @override public class Zi extends Fu {// @override void eat() {system.out.println (" eat "); }}Copy the code

Zi can also be:

Public class Zi extends Fu {@override protected void eat() {system.out.println (" eat in Zi "); }}Copy the code

Modify the access relationship: public>protected> Default (not written)>private

When the parent’s permission modifier is the default (that is, no write modifier), the modifier overridden by subclasses can be the default and

Default and so on. ** When the permission modifier of the parent class is private, the subclass is not private

Before. Subclasses cannot override such methods when the parent class is private.

The super keyword

When a member variable in a subclass has the same name as a member variable in a parent class, and we want to use a member variable in a parent class,

Or when we overwrite a method in the parent class, and we want to call a method in the parent class. So we’re going to use super

Keyword to call a member of the parent class.

Super versus this

Super works in the same way as this, which solves the problem of local variable pairs with the same name as member variables

Member overwriting super addresses subclasses overwriting their parent classes.

Local variables are accessed when a local variable, a member variable, or a variable in a parent class has the same name. You can change it with this and super.

public class fu{ public int age=40; Public fu(){} public fu(int age){this.age=age; } public void eat(){system.out.println (" foo eat "); } } public class Zi extends Fu { public int age=18; public Zi(){} public Zi(int age){ this.age=age; } public void eat() { super.eat(); System.out.println(" eat method in zi "); } public void show(){ int age=1; System.out.println(age); //1 The value of the local variable system.out.println (this.age); System.out.println(super.age); } public class Demo{public static void main(String[] args){zi z=new zi(); z.eat(); z.show(); }}Copy the code

Next we continue!!