Object-Oriented Programming
Java is an OO language as it is modeled and organized around objects rather than actions and data rather than logic. OOP in java aims to implement real-world entities such as objects, classes, abstractions, inheritance, and so on.
Java classes
A class in Java is a blueprint that includes all your data. A class contains variables and methods that describe the behavior of an object.
1class Vehicle {
2
3 //variables
4 private int numWheels;
5 private String name;
6
7 //methods
8 public String getName() {
9 return name;
10 }
11
12 public void setNumWheels(int numWheels) {
13 this.numWheels = numWheels;
14 }
15
16
17}
Objects
An object is an instance of a class that can access your data. The keyword new
is used to create the object.
1//Declaring and initializing an object
2Vehicle v = new Vehicle();
Java Constructors
A constructor is a block of code that initializes a newly created object. It is basically a method but doesn't have any return type and has the same name as it's class. There are two types of constructors in Java:
1) Default Constructor
The default constructor is created by default by the java compiler at class creation if no other constructor is declared in the class. It doesn't contain any parameters which is why it is sometimes referred to as the no-argument constructor.
1public class Vehicle {
2
3 //default constructor
4 public Vehicle() {}
5
6}
7
8public class Driver {
9
10 public static void main(String[] args) {
11
12 //creating vehicle object using default constructor
13 Vehicle vehicle = new Vehicle();
14 }
15
16}
2) Overloaded Constructor
The overloaded constructor contains one or more parameters. It is used to provide different values to the distinct objects at the time of their creation.
1public class Vehicle {
2 int numWheels;
3 String color;
4 int numDoors;
5
6 //overloaded constructor
7 public Vehicle(int numWheels, String color, int numDoors) {
8
9 //keyword this points to current object
10
11 this.numWheels = numWheels;
12 this.color = color;
13 this.numDoors = numDoors;
14 }
15
16 //toString() method used get value of instance variables
17 public String toString() {
18
19 String s = "number of wheels" + numWheels +
20 "color:" + color + "number of doors:" + numDoors;
21
22 return s;
23 }
24}
25public class Driver {
26
27 public static void main(String[] args) {
28
29 Vehicle car = new Vehicle(4, "red", 4);
30 Vehicle truck = new Vehicle(12, "black", 2);
31
32 System.out.println(car.toString());
33 System.out.println(truck.toString());
34 }
35
36}
The 4 basic principles of Object-Oriented Programming

1) Polymorphism is the ability of a variable, function, or object to take multiple forms. It allows you to define one interface or method and have multiple implementations. Polymorphism is characterized by the method overloading and method overriding.
Method Overloading
Method Overloading is the act of having the same function name but differs in number and type of arguments within the same class.
1public class Example {
2
3 public int sum(int a, int b) {
4 return a + b;
5
6 }
7
8 public int add(int a, int b, int c) {
9 return a + b + c;
10 }
11
12
13}
Method Overriding
Method Overriding is the specific implementation of a method in a child class which is already defined in the parent class.
1public class Vehicle {
2
3 //Overriden method
4
5 public void drive() {
6
7 System.out.println("Vehicle is driving");
8
9 }
10}
11
12public class Car extends Vehicle {
13
14 //Overriden Method
15
16 public void drive() {
17
18 System.out.println("Car is driving");
19
20 }
21
22}
As you can see, the child class Car
inherited the drive
method from its parent class Vehicle
. However, we can change the functionality of the method while keeping the same method signature.
2) Abstraction is a concept of hiding the details and showing only the necessary things to the user. We use an abstract class/Interface to express the intent of the class rather than the actual implementation.
An Abstract class is a class which is declared with an abstract keyword and cannot be instantiated.
1public abstract class Example{
2
3 public void display();
4
5 public int sum(int a, int b);
6
7}
An interface is a blueprint that contains static constants and abstract methods.
1public Interface VehicleInterface {
2
3 //methods in an interface does not have a body
4 public void displayVehicle();
5 public boolean isAutomatic();
6
7}
8
9public class Vehicle implements VehicleInterface {
10
11 public void displayVehicle() {
12
13 //The body of displayVehicle goes here
14
15 System.out.println("This is a car");
16
17
18 }
19
20 public boolean isAutomatic() {
21
22 //The body of isAutomatic goes here
23
24 return true;
25 }
26}
3) Inheritance
Inheritance is a concept where the properties of one class can be inherited by the other. It helps with reusability and establishes a relationship between different classes. The relationship is established in code using the keyword extends
.
1public class Person {
2
3 private int pID;
4 private String name;
5
6 Person() {
7 pID = 4;
8 name = "Brian";
9 }
10
11 Person(int pID, String name) {
12
13 this.pID = pID;
14 this.name = name;
15 }
16
17}
18
19public class Student extends Person {
20
21 public void displayName() {
22
23 System.out.println(super.name);
24 }
25
26
27}
In this example, the child class Student
inherits attributes from it's parent class, Person
. As stated above the relationship is established using the keyword extends
. The super
keyword in java acts as a reference variable that is used to refer to parent class objects.
The displayName()
method would print the value of name that is inherited from the Person class.
4) Encapsulation
Encapsulation is a process of hiding data implementation by restricting access to public methods. The instance variables are kept private and the accessor methods(getters and setters) are made public.