Classes and Objects in Java Part I

Classes and Objects are basic concepts of Object Oriented Programming which revolve around the real life entities.

Class

A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order:

类是用户定义的蓝图或原型,从中可以创建对象。它表示一种类型的所有对象共有的一组属性或方法。

  1. Modifiers : A class can be public or has default access (Refer this for details).

    修饰符: 一个类可以是公共的或具有默认访问权限

  2. Class name: The name should begin with a initial letter (capitalized by convention).

  3. Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.

  4. Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.

  5. Body: The class body surrounded by braces, { }.

Constructors are used for initializing new objects. Fields are variables that provides the state of the class and its objects, and methods are used to implement the behavior of the class and its objects.

构造函数用于初始化新对象。字段是提供类及其对象状态的变量,方法用于实现类及其对象的行为。

There are various types of classes that are used in real time applications such as nested classes, anonymous classes, lambda expressions.

Object

It is a basic unit of Object Oriented Programming and represents the real life entities. A typical Java program creates many objects, which as you know, interact by invoking methods. An object consists of :

它是面向对象编程的基本单元,代表了现实生活中的实体。一个典型的Java程序会创建许多对象,如您所知,这些对象通过调用方法进行交互。一个对象包括:

  1. State : It is represented by attributes of an object. It also reflects the properties of an object.

    状态:它由对象的属性表示。它还反映了对象的属性。

  2. Behavior : It is represented by methods of an object. It also reflects the response of an object with other objects.

    行为:它由对象的方法表示。它还反映了一个对象对其它对象的响应。

  3. Identity : It gives a unique name to an object and enables one object to interact with other objects.

    身份: 它为一个对象赋予唯一的名称,并使一个对象能够与其它对象进行交互

Example of an object : dog

在这里插入图片描述

Objects correspond to things found in the real world. For example, a graphics program may have objects such as “circle”, “square”, “menu”. An online shopping system might have objects such as “shopping cart”, “customer”, and “product”.

对象对应于现实世界中发现的事物。例如,图形程序可以具有诸如“圆形”,“正方形”, “菜单”之类的对象。在线购物系统可能具有“购物车,”客户“和”产品“之类的对象

Declaring Objects (Also called instantiating a class)

When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.

当创建一个类的对象时,该类被称为实例化。所有实例都共享类的属性和行为。但是这些属性的值,即状态对于每个对象都是唯一的。单个类可以具有任意数量的实例。

Example :

在这里插入图片描述

As we declare variables like (type name). This notifies the compiler that we will use name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. So for reference variable, type must be strictly a concrete class name. In general,we can’t create objects of an abstract class or an interface.

Dog tuffy;

If we declare reference variable(tuffy) like this, its value will be undetermined(null) until an object is actually created and assigned to it. Simply declaring a reference variable does not create an object.

Initializing an object

The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the class constructor.

new运算符为新对象分配内存并返回对该内存的引用来实例化一个类。new运算符还会调用类构造函数。

package org.geeksforgeeks;


// Class Declaration
public class Dog {
    // Instance Variables
    String name;
    String breed;
    int age;
    String color;

    // Constructor Declaration of Class
    public Dog(String name, String breed,
               int age, String color) {
        this.name = name;
        this.breed = breed;
        this.age = age;
        this.color = color;
    }

    // method 1
    public String getName() {
        return name;
    }

    // method 2
    public String getBreed() {
        return breed;
    }

    // method 3
    public int getAge() {
        return age;
    }

    // method 4
    public String getColor() {
        return color;
    }

    @Override
    public String toString() {
        return ("Hi my name is " + this.getName() +
                ".\nMy breed,age and color are " +
                this.getBreed() + "," + this.getAge() +
                "," + this.getColor());
    }

    public static void main(String[] args) {
        Dog tuffy = new Dog("tuffy", "papillon", 5, "white");
        System.out.println(tuffy.toString());
    }
}

Output

Hi my name is tuffy.
My breed,age and color are papillon,5,white

This class contains a single constructor. We can recognize a constructor because its declaration uses the same name as the class and it has no return type. The Java compiler differentiates the constructors based on the number and the type of the arguments. The constructor in the Dog class takes four arguments. The following statement provides “tuffy”,”papillon”,5,”white” as values for those arguments:

Dog tuffy = new Dog("tuffy", "papillon", 5, "white");

The result of executing this statement can be illustrated as :
在这里插入图片描述

Note:

All classes have at least one constructor. If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, also called the default constructor. This default constructor calls the class parent’s no-argument constructor (as it contain only one statement i.e super()😉, or the Object class constructor if the class has no other parent (as Object class is parent of all classes either directly or indirectly).

super()😉, or the Object class constructor if the class has no other parent (as Object class is parent of all classes either directly or indirectly).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值