In this chapter, we will learn:
- Class and Objects
- Constructor
- Object-Orientation Programming: Encapsulation, Polymorphism, and Inheritance
1. What is an object?
The concept object maps the objects in the real world. There are many cars on the street. Each car is an object. Then you can name a tree is an object, a person is an object, etc, An object can be anything in the real world. Even a bank account, a purchase transaction can be an object.
Once an object is created, it is different to the next created object based on the same blueprint. Just like two Honda Civic cars are 2 individual objects. They share the same set of properties, like manufacturer, brand, number of seats, vin (the unique id associated with each car once manufactured), mileage, etc. All cars have a shared set of common functions. For example: start, drive, shutOff. Some cars may extra functions, like powered window, powered mirror, Bluetooth connections.
Java is an Object-Oriented language. The concept in Java is very similar to that in the real world. With Java, the blueprint is the class. We can use “new” command to create objects as many as we need. All properties of an object are declared as member variables in the class. The functions are declared as methods in the class.
package xyz.tutorial.lesson5.model;
public class Car {
String manufacturer;
String brand;
int vin;
float mileage;
short numberOfSeats;
public void start() {
System.out.println("Start");
}
public void shutOff() {
System.out.println("Shut off");
}
public void drive() {
System.out.println("Drive");
}
}
package xyz.tutorial.lesson5;
import xyz.tutorial.lesson5.model.Car;
public class Lesson5 {
public static void main(String[] args) {
Car car1 = new Car();
Car car2 = new Car();
}
}
The 2 classes of the example are from 2 packages. The package reflects the location where the class is placed. Two classes from 2 packages mean the 2 classes are placed in different locations. Accessing from one class to another class from a different package require importing. When we define a Java class, the first line always is the package declaration, then followed all necessary imports. An imported class or a class from the same package (without import) can be access from the current class.
Car class is a blueprint. Every car object is created by “new Car()”.
2. Constructor
The actual process of creating objects upon class requires a special method, which is called constructor. But why we didn’t see that special method (constructor) in the Car class.
Java constructor rule: If a default constructor is not explicitly declared, Java will automatically apply it with certain conditions.
A class may have multiple constructors. The default one is the contractor without any arguments. That is why even if we didn’t declare any constrictors in the Car class, we can stiill create the object through new Car(); Of course, we can add a default constrictor explicitly in the source code.
package xyz.tutorial.lesson5.model;
public class Car {
public Car() {
}
String manufacturer;
String brand;
int vin;
float mileage;
short numberOfSeats;
public void start() {
System.out.println("Start");
}
public void shutOff() {
System.out.println("Shut off");
}
public void drive() {
System.out.println("Drive");
}
}
A constructor has an obvious different appearance to a regular method. (1) It does not have a return type. A regular method must declare what data type the method is going to return. If no data will be retuned, a method must have “void” t0 clearly state that. (2) The Java coding convention recommend camel case with the first letter in lower case for identifiers except the class names. a class name starts with upper case. A constructor must exactly match the name of the class, so it starts with upper case as well.
A default constructor is used for creating a “default” object. If necessary, extra constructors can be defined for creating an object and do necessary initialization. In every constructor.
package xyz.tutorial.lesson5.model;
public class Car {
public Car() {
super();
}
public Car(String manufacturer, String brand) {
this.manufacturer = manufacturer;
this.brand = brand;
}
public Car(String manufacturer, String brand, int vin) {
this.manufacturer = manufacturer;
this.brand = brand;
this.vin = vin;
}
String manufacturer;
String brand;
int vin;
float mileage;
short numberOfSeats;
public void start() {
System.out.println("Start");
}
public void shutOff() {
System.out.println("Shut off");
}
public void drive() {
System.out.println("Drive");
}
}
We have added 2 extra constructors, now there are 3 constructors in the Car class. The first one is the default constructor. The second one takes 2 arguments for initialization. The third one takes 3 arguments for initialization. Accordingly, we can create more car objects in the following ways:
Car car3 = new Car(“Honda”, “Civic”);
Car car4 = new Car(“Honda”, “Accord”, “14357853677”);
The parameters for calling a class’ constructor must exactly match the number of the arguments, and data type.
Java constructor rule: If a class declares one or more constructors other than the default constructor, Java will not apply the default constructor. If the default constructor is still needed, it must be declared explicitly. (In that case, the default constructor cannot be omitted)
3. Inheritance
In Java, every class has its parent class. Classes could form a hierarchy tree that every class is placed at a fixed place with a clear path to trace back in its ancestors and show its children classes if they exist. All classes have a common root: the Object class in the java.lang package.
java.lang is a system reserved package for Java language specification. All class in the java.lang are by default imported. We don’t need to import a class from that package. Also, we cannot define any additional classes in that package. (that is why it is called reserved)
A child class can derive from its parent by using “extends”. However, since the Object class is the root in the tree, if a class directly extends Object, “extends Object” can be omitted.
Our Car class example can be illustrated as:

This is a class diagram using UML (Unified Modeling language). The Car class is a direct child class of the Object class, so it is not necessary to explicitly declare a class like public class Car extends Object. In a UNL class diagram, we usually don’t need to add the Object class either.
Java only allows single inheritance. That means if a class needs to extend another class, then there is only one super class (single parent class) is allowed.

In the Animal class diagram, the Animal class is on the top. The root ancestor class Object is omitted.
package xyz.tutorial.lesson5.model;
public class Animal {
}
package xyz.tutorial.lesson5.model;
public class Mammal extends Animal {
public Mammal() {
super();
}
}
package xyz.tutorial.lesson5.model;
public class Mammal extends Animal {
public Mammal() {
super();
System.out.println("Mammal constructor is called");
}
}
package xyz.tutorial.lesson5.model;
public class Dog extends Mammal {
public Dog() {
super();
System.out.println("Dog constructor is called");
}
}
Above 3 class reflect the inheritance (through “extends”) in the class diagram. Create a new Dog object:
Dog dog1 = new Dog();
The output is:
Mammal constructor is called
Dog constructor is called
The first line is printed from the Dog class’s constructor. The second line is printed from Dog’s super class Mammal’s constructor. When a constructor is called during instantiation, all ancestor class in the chain through the root class Object will be initialized too. So the constructor in the class inheritance chain will be called one by one.
The first line of a constructor should be super() with or without parameters, which means it calls the super class’ constructor before completion of calling the constructor of the current class. Since super() (without parameter) is the default line, it is usually omitted.
4. Encapsulation
Let’s revisit the Car class. It includes constructors, member variables, and member methods. Once Car objects are created, all the member variables and member methods all belong to each individual Car object. Each object has its own set of variables and methods.
The member variables are the properties of an object. The member methods are all the possible actions we can perform on that particular object.
A class defined all needed properties and actions in one place. It is an important Object-Orientation Programming (OOP) concept, encapsulation.
5. Overriding and Polymorphism
A class extends it super class, the variables and methods defined in the super class are inherited.
package xyz.tutorial.lesson5.model;
public class Mammal extends Animal {
public Mammal() {
super();
System.out.println("Mammal constructor is called");
}
public void sound() {
System.out.println("Animal sound.");
}
}
package xyz.tutorial.lesson5.model;
public class Cat extends Mammal {
public Cat() {
super();
System.out.println("Cat constructor is called");
}
}
Cat cat = new Cat();
cat.sound();
The Cat class doesn’t define sound() method. The method is inherited from its super class Mammal. Once call the sound() method on the cat object, the sound() method from its super class is called.
If the Cat and Dog classes override the sound() method. Calling the sound() method on the cat object, the overrode method will be called instead of the inherited one.
In OOP, Polymorphism means “many forms”. Polymorphism makes it possible to have the method from its super class perform different tasks.
Exercise
Modify the Cat and Dog classes, and execute the following line of codes,
Dog dog = new Dog();
dog.sound();
Cat cat = new Cat();
cat.sound();
to achieve the result
Mammal constructor is called
Dog constructor is called
bow-wow
Mammal constructor is called
Cat constructor is called
Meow
