1024programmer Java Java language encapsulation, inheritance, abstraction, polymorphism, interface

Java language encapsulation, inheritance, abstraction, polymorphism, interface

Table of contents

Foreword

1. Encapsulation

1.1 Definition of encapsulation

1.2 Access modification Use of symbols

2. Inheritance

2.1 Definition of inheritance

2.2 Methods of inheritance

2.3 Points to note when using inheritance

3. Polymorphism

3.1 Definition of polymorphism

3.2 Dynamic binding

3.3 Method rewriting

3.4 Upward (Downward)Transformation

4. Abstraction

4.1 Overview and definition of abstraction

4.2 Use of abstraction

p>

5. Interface

5.1 Meaning of interface

5.2 Definition of interface

Summary


😽Personal Homepage: tq02’s Blog_CSDN Blog-C Language, Java Blogger

🌈 Ideal goal: study hard and move towards Java There are regrets.
🎁Welcome → Like👍 + Collection⭐ + Comment📝+Follow✨

Contents of this chapter&# xff1a;Java encapsulation, inheritance, abstraction, polymorphism, interface

Use compiler :IDEA

Preface

Before talking about encapsulation, inheritance, abstraction, polymorphism, and interfaces, we must first understand what classes and objects are. Detailed explanation of classes and objects:tq02’s explanation of classes and objects

1 .Package

1.1 Definition of package

        Encapsulation ,Operation in the class,Encapsulate the member variables and member methods of the class,Meaning: Can be thought of as a protective barrier,preventing the code and data of this class from being randomly accessed by code defined by external classes. To encapsulate the class we need to use access modifiers, which are divided into four kinds

Access modifiers private default protected public
The same category in the same package
Different classes in the same package √ √
Subclasses of different packages √ √
Non-subclasses of different packages

Note :1. If the member variable or member method does not use encapsulation, The default modifier is used by default.

2. A package is a container for storing multiple classes – to prevent conflicts with class names written by others when the project is created.

3. Subclasses can be learned through inheritance.

1.2 Use of access modifiers

We carry out four encapsulation code example diagrams

1.private modification

Private modified price and name Variables , cannot be accessed by other classes, can only be accessed within their own class. For example, these two element variables cannot be directly accessed in the BookList class.

2.default modification

The default modifier, means that when you do not mark the modifier, the default modifier, is used by default, and there is only one of this type Other classes of the package can access “such as BookList” but the Student class of a different package cannot.

3.protected modifier

protected modifier , When a class is modified, all classes in the same package can access , other classes in non-same packages Subclasses of packages can also be used. For example, after inheriting the Student class, you can also use the Book class.

4.public modifier

The public modifier – once used – can be used by all classes – regardless of whether it is in the same package.


2. Inheritance

2.1 Definition of inheritance

What is inheritance ? is an indispensable component of all OOP languages ​​, is one of the three major characteristics of object-oriented , inheritance is equivalent to , children�� Contains other non-abstract methods, and can also contain fields. The rules of this non-abstract method are the same as ordinary methods, and can be overridden or directly called by subclasses.


5. Interface

The meaning of 5.1 interface

  The interface is to make up for the problem that code cannot be inherited from multiple sources and can only be inherited from a single source. For example, a parent class of Animal is defined, and subclasses of Cat, Bird, Dog, and Wolf are defined. However, if you think about it, “dogs and cats are pets” and may have common methods. #xff0c;Define a pet class,Then the same behavior of unrelated classes can be achieved through the interface.

Benefits:

  1. Conducive to code specification,The purpose is to give developers a clear instruction,Tell them what business needs to be implemented, #xff1b; At the same time, it prevents unclear naming and code confusion caused by developers’ arbitrary naming, which affects development efficiency.
  2. It is helpful to maintain the code – define an interface – put the function menu in the interface – and then implement this interface when defining the class.
  3. It is to ensure the safety and tightness of the code.

5.2 Definition of interface

Definition method:

Public interface Interface name { }

Use interface:

class class name implements interface name { method body };

Code implementation:

class Animal {public String name; public Animal(String name) {this.name = name;}}//Define interface
public interface Eat {public void eat(String food);
}//Use interface
class Dog extends Animal implements Eat{public int age;public Cat(String name) {//Use super to call the constructor of the parent class.super(name);}public void eat(String food) {System.out.println(this.name + "Eating" + food);}
}//Use interface
class Cat extends Animal implements Eat{public int age;public Cat (String name) {//Use super to call the constructor of the parent class.super(name);} public void eat(String food) {System.out.println(this.name + "Eating" &#43 ; food);}
}
public class Test {public static void main(String[] args) {Animal cat = new Cat("小黑");cat.eat();
}
}

The key point of the interface:

  1. If the method body of the interface is empty,, it is an empty implementation.
  2. A class can have multiple interfaces.
  3. All methods in the interface are public and abstract,so public and abstract can be omitted.
  4. The interface cannot be instantiated.
  5. Interfaces can be inherited


Summary

Inheritance is the basis of polymorphism and abstraction – and only by learning abstraction can we understand interfaces. These knowledge are the three most important object-oriented features of Java. Only by carefully studying “can you get one step closer” can you officially start in Java.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/java-language-encapsulation-inheritance-abstraction-polymorphism-interface-4/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索