search
HomeJavajavaTutorialIntroduction to object-oriented programming design in Java language
Introduction to object-oriented programming design in Java languageJun 10, 2023 am 08:30 AM
javaDesign PatternsObject-Oriented Programming

As a high-level programming language, Java language adopts Object Oriented Programming (OOP for short) as its basic programming paradigm. As an idea and method in programming, object-oriented programming has become the most important programming model in the field of computer science. In this article, we will introduce object-oriented programming design in Java language.

1. What is object-oriented programming?

Object-oriented programming is an object-based programming method. Its core idea is to decompose the program into several modules, each module has its own data and behavior. Each module is an object, and the objects collaborate to complete a certain task by calling each other's methods.

The core concepts in object-oriented programming are "classes" and "objects". A class defines the properties and behavior of an object, and an object is an instance of a class. Classes allow you to create multiple objects that all have the same properties and behavior. This programming method can effectively achieve code reuse, encapsulation and maintenance, making the program more readable and maintainable.

Object-oriented programming in the Java language is based on the Java Virtual Machine (JVM). JVM is a virtual machine that can run on different platforms. Therefore, Java programs can run on different operating systems, such as Windows, Mac, Linux, etc.

2. Object-oriented programming in Java

Object-oriented programming in Java mainly includes the following aspects:

1. Classes and objects

"Class" in Java is a template used to describe the status and behavior of a certain type of object. An "object" is an instance of a class, which has the properties and methods described by the class. In Java, classes are defined through the class keyword.

For example, a Person class is defined below:

public class Person{
    private String name; // 姓名
    private int age; // 年龄
    
    // 构造方法
    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }
    
    //获取姓名
    public String getName(){
        return name;
    }
    
    //获取年龄
    public int getAge(){
        return age;
    }
    
    //输出信息
    public void printInfo(){
        System.out.println("姓名:" + name + ",年龄:" + age);
    }
}

In the above program, we define a Person class, which contains name and age attributes and methods for obtaining name, age, and output information. method. When using it, you can create an object through the following code:

Person p = new Person("张三", 20);

Among them, the new keyword is used to create an instance of the Person class, that is, a Person object.

2. Encapsulation and inheritance

Encapsulation (Encapsulation) in Java refers to the process of packaging the data and behavior of an object together and hiding the internal implementation details from the outside world. In Java, encapsulation is achieved through access control keywords private, public, protected, etc.

Inheritance means that one class inherits the properties and methods of another class. In Java, inheritance is implemented through the keyword extends. Subclasses can inherit the properties and methods of the parent class, and can also override (Override) the methods of the parent class.

The following is an example of inheritance:

public class Student extends Person{
    private String school; // 学校

    // 构造方法
    public Student(String name, int age, String school){
        super(name, age);
        this.school = school;
    }
    //获取学校
    public String getSchool(){
        return school;
    }
    //输出信息
    public void printInfo(){
        super.printInfo(); // 调用父类的printInfo方法
        System.out.println("所在学校:" + school);
    }
}

In the above program, we define a Student class, which inherits the attributes and methods of the Person class, and adds the school attribute and getSchool, printInfo method.

3. Polymorphism

Polymorphism in Java refers to different manifestations of the same behavior. In Java, polymorphism is achieved through inheritance and override.

The following is a polymorphic example:

public class Test{
    public static void main(String[] args){
        Person p = new Person("张三", 20);
        p.printInfo();
        Student s = new Student("李四", 18, "北大");
        s.printInfo();
        p = s; // 多态
        p.printInfo();
    }
}

In the above program, we define a Person class and a Student class, each of which has its own printInfo method. In the main method, we first create a Person object p and output its information; then create a Student object s and output its information; then we implement polymorphism through "p = s", and when we call p's printInfo method again, What is actually called is the printInfo method of the Student class.

4. Interface

The interface in Java is a set of abstract methods. All methods in the interface are abstract and have no specific implementation. Interfaces can be implemented by classes, and a class can implement multiple interfaces. In Java, interfaces are defined through the keyword interface.

The following is an example of an interface:

interface IShape{
    public abstract double area();
}

class Circle implements IShape{
    private double radius;
    public Circle(double radius){
        this.radius = radius;
    }
    public double area(){
        return Math.PI * radius * radius;
    }
}

class Rectangle implements IShape{
    private double width, height;
    public Rectangle(double width, double height){
        this.width = width;
        this.height = height;
    }
    public double area(){
        return width * height;
    }
}

public class Test{
    public static void main(String[] args){
        IShape c = new Circle(2.0);
        IShape r = new Rectangle(3.0, 4.0);
        System.out.println("圆面积:" + c.area());
        System.out.println("矩形面积:" + r.area());
    }
}

In the above program, we define an IShape interface and two classes, Circle and Rectangle, both of which implement the IShape interface. In the main method, we create Circle and Rectangle objects and call their area methods respectively.

Summary

The Java language uses object-oriented programming (OOP) as its basic programming paradigm, and implements program design through mechanisms such as classes and objects, encapsulation and inheritance, polymorphism and interfaces. Programs written using object-oriented programming methods have good maintainability, scalability and better code reusability.

The above is the detailed content of Introduction to object-oriented programming design in Java language. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java数据结构之AVL树详解Java数据结构之AVL树详解Jun 01, 2022 am 11:39 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),