Object, Class, Abstract class and Interface in Java

Tram Ho

1. Object in Java

Object is a physical entity, can be an animal, an object … For example, a house, an airplane, a motorbike, a person …

Each object will have its own characteristics:

  • Status of the object: shown in the value of the variables in the class (the field of the object), for example what color car, how many cubes, price …
  • Behavior: methods of a class, or actions of an object. For example, the car can run, play the whistle, light …
  • Identifier: object identification is implemented via a unique ID. The value of the ID is not visible to external users. But it is internally used by the JVM to uniquely identify each object.

For example: There is a pen object named Thien Long, black, … is considered as its state. It is used for writing, so writing is its behavior.

2. Class in Java

In Java in particular and in object-oriented programming in general, Class (class) is understood as a group of objects with common characteristics.

  • For example, a vehicle class is a group of objects that have wheels and serve as a vehicle for transportation on roads.

Class is a detailed model for you to use to create Objects. Class defines all the properties and methods needed of an Object.

  • For example, XeOto class includes attributes (color, volume, speed …), we can understand this is the design of a XeOto, and when creating a car (object) from that design, we there will be many different options: color (white, yellow, blue, black), cubic (5.2L, 6.3L), speed (300km / h, 400km / h) …

Creating objects must follow the predefined class design:

  • For example, if Class XeOto is designed without a propeller, then the object (for example, a black car, 5.2L displacement and 300km / h speed) will be created without a propeller.

3. Abstract class in Java

Abstract class, also known as simple abstract class, is considered as a parent class for all classes with the same nature. Therefore each derived class (subclass) can only inherit from an abstract class. Besides, it does not allow instance creation, meaning it will not be possible to create objects of that class.

A class declared with abstract keyword is abstract class.

An abstract class can have methods: Abstract method or Non-abtract method.

An abstract method is a blank method that has no implementation.

Non-abtract method is an executable method.

An abstract class can declare 0, 1 or more abstract methods inside.

Cannot initialize an object directly from an abstract class.

4. Interface in Java

Interface is considered as a mask for all classes of the same way but can vary in nature. Since then the derived class can inherit from many Interface classes to fully complement the way it operates (Multiple inheritance).

Interface is not class.

The interface only contains empty methods / properties that have no implementation.

The interface is like a template, a framework for classes to implement and follow.

Classes can inherit multiple interfaces.

Interface is a contract, implement classes must implement methods according to the interface defined.

5. Summary

Class is a detailed model for you to use to create Objects. Class defines all the properties and methods needed of an Object.

Each Object must belong to a certain Class. And an Object is an instance of Class. All Objects belonging to the same Class have the same properties and methods.

You should not be confused when talking about whether a class is implemented or extend.

You can only extend from a class and implement only the interface functions for your class. In a nutshell, the relationship between a class when inheriting an abstract class is called is-a, and a class when implementing an interface is called can-do (or –able).

Share the news now

Source : Viblo