Home > Java > javaTutorial > What is the difference between object and class in java

What is the difference between object and class in java

青灯夜游
Release: 2018-11-30 16:56:36
Original
13757 people have browsed it

This article will introduce to you the difference between objects and classes in Java, so that everyone can have a simple understanding of classes and objects and know the differences between them. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What is the difference between object and class in java

#Many people are confused about the difference between classes and objects and are not very able to distinguish them. In fact, there are many differences between objects and classes. In this article, we will take a look at the differences between classes and objects.

Classes and objects are the basis of object-oriented programming. A class can be regarded as a construct that encapsulates a set of variables and methods; and an object acts as a member or instance of the class.

Let’s take a closer look at the difference between classes and objects:

What is the difference between object and class in java

1. An object is an instance (result) of a class, which helps programmers use variables and methods in the class. A class is a blueprint or template from which instances, i.e. objects, can be created.

2. Objects can be regarded as: class variables.

Classes are used to bind data and methods together as a single unit.

3. The object is a real-world entity, such as a pen, mobile phone, bed, keyboard, mouse, chair, etc.; the object has status and behavior, for example: a dog has status: color, name, breed And behavior: tail wagging, barking, eating.

A class is a group of objects with common properties.

4. Objects can be physical entities (tangible) or logical entities (intangible). A class is a logical entity.

5. Objects are created through the new keyword, mainly for example: Studen s1 =new Student(); classes are declared using the class keyword, for example: class Student {}

6. According to requirements, objects can be declared multiple times; classes only need to be declared once.

7. When an object is created, it will occupy memory. When a class is created, it does not occupy any memory space.

8. There are many ways to create objects in java, such as the new keyword, newInstance() method, clone() method, deserialization, etc.; the only way to define a class in java is to use the class keyword method.

Let's take a look at some real examples of classes and objects in Java in order to better understand their differences:

1. Class: Human; Object : Men, women, children. . . .

2. Category: Fruit; Object: apple, banana, mango, pomegranate, orange. . . .

3. Category: mobile phone; Object: iPhone, Huawei, vivo. . . .

4. Category: Food; Object: hamburger, rice, noodles. . .

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study. For more related video tutorials, it is recommended to visit: JavaTutorial!


The above is the detailed content of What is the difference between object and class in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template