In order to understand the ideas of OOP, we have to clarify the terms related to OOP used in Java.
Class (explanation 1)
A class is used to model an entity in the real-world. An entity in the real-world has properties and behaviours. E.g., a student has its name, height, gender, date of birth (properties). A student can change its name. A student can enrol to a course (behaviours).
Class (explanation 2)
A class is a definition of an object, a blueprint of an object (how an object looks like), a contract of an object (how an object's data and methods are accessed).
Class (explanation 3)
A class is a type of an object. E.g., a Student object is from the Student class and is of the Student type.
Object (explanation 1)
A class is a definition or imagination of an object. An object is a concrete, detailed existence of a class in your program.
Object (explanation 2)
An object is of a class. An object belongs to a class. An object is created by the class's constructor. A class has no object before one object is created by the constructor. An object of a class exists only after the object has been created.
Instance (explanation 1)
An existing example of a class. A class is a definition of an instance. An instance is created based on a class (definition). An instance of a class exists only after the instance has been created.
Instance (explanation 2)
An instance is almost the same as an object. The terms are used interchangeably.
Relationships among class, type, object, and instance:
-
A Student object belongs to the Student class.
-
A Student instance belongs to the Student class.
-
A Student object is of Student type.
-
A Student instance is of Student type.
-
A Student object is an instance of the Student class.
-
(X) A Student instance is an object of the Student class (somewhat wrong).
-
The Student class defines the Student type.
What does a Class look like?
A class contains data fields (properties) of the class's objects.
A class contains methods (behaviours / actions) that the class's objects can perform.
Members of a class are its data fields and methods.
To create a class, write like this:
To generalise, the syntax looks like this:
Why superclass / subclass / inheritance?
Sometimes two kinds of entities are related. And if we change some properties of one entity, the other entity's properties may need to be changed accordingly.
Consider an example of cars and motorcycles. Suppose all vehicles all over the world have a speed limit of 70km/h. No vehicles in the world can exceed the 70-km-per-hour speed limit. In this case, we can give a property to cars and motorcycles like this:
Defining like this is totally fine. But what if the god of our world changes the speed limit again? Or what if there are 300 kinds of vehicles in the world? We have to change that property's value in every class and make sure that they are in correct values. For dealing with such a case, we better use class inheritance.
In class inheritance, we have a superclass (or a supertype) whose data fields and methods are inherited by its subclasses. The subclasses have its own data fields and methods and those inherited from the superclass. If the inherited data fields are changed in the superclass, those data fields will also be changed in the subclasses. Class inheritance can be used when there is an "is-a" relationship between two classes. For example, a Car is a Vehicle, and a Motorcycle is a Vehicle. We can design a Vehicle class and make it a superclass by letting the Car and Motorcycle classes inherit (extend) the Vehicle class.
The UML diagram below denotes the relationships:
For more about object-oriented, you can refer to the book "The Object-Oriented Thought Process (2019, Addison-Wesley)" and "Software Engineering (2016, Pearson)".
Drawing UML diagrams: draw.io
Concept of abstract class
What does "abstract" mean? The word "abstract" of abstract class means that a class has very little specific details. Sometimes a class has so little details that we can't even imagine what the objects of the class really is. So, it is called an abstract class.
Concept of concrete class
In contrast to an abstract class, a concrete class is a class that has enough details to represent an entity. A class that is detailed enough to create an object of that class is considered as a concrete class.
Why do we need abstract classes?
We need abstract classes basically because of design needs or programming needs. We don't necessarily need to use abstract classes to describe the world. But when we do programming, we want to make the program easy to be maintained, and easy for other people to extend and use it. That's why we would create some classes that are very abstract.
What is an abstract superclass?
Sometimes a superclass is so abstract that it cannot be used to create any specific instances. Such a class is referred to as an abstract class.
When you don't know how to implement a method in a superclass, the method should be an abstract method; or when you want to deliberately leave the implementation to the subclasses, the method should be abstract.
An abstract method in a superclass doesn't have implementation. Its implementation is provided by the subclasses.
A class that contains abstract methods must be defined as abstract. If you don't want a subclass to be abstract, you must implement all abstract methods inherited from the superclass.