Java

Object-Oriented Programming Concepts

Object Oriented Programming Concepts

Object-Oriented Programming Concepts, as you know we are going to learn Java programming language, the Java is an object-oriented programing language. Don’t worry about it if you do not understand what it is because I will explain it to you.

Object-Oriented programming has two main things

  • Object
  • Class

Note – An object is anything that you can see or touch and a class is a way to decide how that object would like and behave.

To understand the Object-Oriented Programming concept we will discuss an example here

Let’s see you are a car manufacturer. Before you start building the car you know a car is an object that you can touch. Everything that you can see or touch is an object and has a couple of properties.

  • The size of that object.
  • Color of that object.
  • What does that object do?

In the programming world, you know you are going to create software. The software can not be touched but you can feel it with its functionality. Just like you use your smartphone and you feel the operating system that can be Android or iOS etcetera. You can not touch the operating system but feel it because you are using its functionality by using touch functionality.

In the car example first, you decide the same things.

  • The size of this car.
  • Color of this car.
  • The functionality of this car.

The size and color of the car are self-explanatory but the functionality consists of a couple of things. The functionality means how fast this car will run. How smooth the steering will be and several other things.

In the programming world, if you want to create a car you need to create a Class named ‘car‘. Once the Class named ‘car‘ is created means a car is made. Now you need to decide how this car will look and behave. You can declare everything inside this class. I hope you understand what it means when we say an object and a class. Let’s go to the next level.

The Object-Oriented Programming has a couple of Points

  1. Object
  2. Class
  3. Method
  4. Encapsulation
  5. Inheritance
  6. Abstraction
  7. Polymorphism

If you do not understand other points yet, do not worry we will discuss each of them. I believe you have already understood Object and Class so we will start with Method.

Method

In simple language we can say, a method is a function. Just like the human body has several functions. Our throat performs a function when we speak. Our lungs and heart have been performing some functions since the time when we were in our mother’s womb. In-car example, breaking mechanism is a function. Engine starting mechanism is a function.

In the programming world, a method is a piece of code that adds some functionality to the class. We always create a method inside a class. A method can not be written outside a class.

You must be wondering why we create a method? We create a method to use it multiple times. If we need to use the same piece of code multiple times we put it inside a method and call it multiple times. A method can be called from other classes as well. Once a method is created it can be called anywhere.

Encapsulation

To understand Encapsulation, we need to take the example of Capsule. In the medical world when we go to a doctor for some sickness he gives us some medicine. In those medicines, he sometimes gives us some capsules. That capsule has the medicine inside it to keep it safe from the environment. Sometimes the chemical that is used to make that medicine inside that capsule is hyperactive and can react if it comes in contact with the moisture in the air. Once the medicine reacts with the moisture it changes its effect. To keep it separate from moisture, we put it inside that capsule so that medicine can be delivered to the human body in its purest form.

In the programming world when we create a Class, Method, Property, Variable we do not want them to be accessed by all other classes but some specific classes. To achieve this we use Access Specifiers. There are several access specifiers such as public, private, protected and others. Do not worry about Access specifiers at this point because we will discuss them later. At this point you can think public, private, protected are the words who stop the access of class’s internal components and keep it safe.

Inheritance

Inheritance is a very straightforward concept. We all are aware of it. You and I, we all have parents and we are the product of their properties. You may hear of this sentence numerous times by your relatives or neighbors that you look exactly like your father or mother. When they say that they mean you have inherited properties from your parents. When they say your behavior is exactly like your father or mother they mean you have actions inherited parents.

Inheritance in Programming World

  • When somebody says that any part of your body is like your parents. They mean you have inherited a property from your parents.
  • When they say your actions are like your parents they mean you have inherited methods from your parents.

In programming, we use this inheritance concept and this is called extending a class. When Class A is extended by Class B, Class B will get all public properties and methods from Class A.

We will understand Inheritance by an example in programming

Let us say you want to create a Bike, A Bike and a Truck in Java Programming. You need to first write down the properties and methods (Functions) of all of these three vehicles. Let us do this.

Bike

  • Engine
  • Wheels
  • Seats
  • Handle
  • Fuel Tank
  • Lights

Car

  • Engine
  • Wheels
  • Seats
  • Steering
  • Fuel Tank
  • Lights
  • Music System
  • Seat Belt
  • Air Conditioner
  • Fridge
  • Entertainment System

Truck

  • Engine
  • Wheels
  • Seats
  • Steering
  • Fuel Tank
  • Lights
  • Music System
  • Seat Belt
  • Air Conditioner
  • Container

As you can see there are several properties common among all these three vehicles. We can easily understand that we need to create a common class that will have common properties for all of these classes. Let us create a Vehicle class that will have common properties and methods of all classes.

Vehicle

  • Engine
  • Wheels
  • Seats
  • Fuel Tank
  • Lights
package LAU.parent;

public class Vehicle {
	public String Engine;
	public String Wheels;
	public String Seats;
	public String Fuel_Tank;
	public String Lights;

}

After creating Vehicle class we will have special properties remaining in bike, car and truck class. Here are the remaining properties in the classes.

Bike

  • Handle
package LAU.Vehicles;

import LAU.parent.Vehicle;

public class Bike extends Vehicle{
	public String Handle;

}

Car

  • Steering
  • Music System
  • Seats Belt
  • Air Conditioner
  • Fridge
  • Entertainment System
package LAU.Vehicles;

import LAU.parent.Vehicle;

public class Car extends Vehicle {
	public String Steering;
	public String Music_System;
	public String Seat_Belt;
	public String Air_Conditioner;
	public String Fridge;
	public String Entertainment_System;

}

Truck

  • Steering
  • Music System
  • Seats
  • Air Conditioner
  • Container
package LAU.Vehicles;

import LAU.parent.Vehicle;

public class Truck extends Vehicle{
	public String Steering;
	public String Music_System;
	public String Seat_Belt;
	public String Air_Conditioner;
	public String Container;

}

Execution of the Program

import LAU.Vehicles.Bike;
import LAU.Vehicles.Car;
import LAU.Vehicles.Truck;

public class Hello {

	public static void main(String[] args) {
		Bike bike = new Bike();
		System.out.println(bike.Handle = "Small");
		System.out.println(bike.Engine = "Petrol");
		System.out.println(bike.Wheels = "Tubeless");
		System.out.println(bike.Seats = "Dual Seater");
		System.out.println(bike.Fuel_Tank = "Medium Size");
		System.out.println(bike.Lights = "Dual Front Lights");
		
		System.out.println("************************************");
		
		Car car = new Car();
		
		System.out.println(car.Steering = "Small");
		System.out.println(car.Music_System = "Sony");
		System.out.println(car.Seat_Belt = "Yes");
		System.out.println(car.Air_Conditioner = "Samsung");
		System.out.println(car.Fridge = "Samsung");
		System.out.println(car.Entertainment_System = "Sony");
		System.out.println(car.Engine = "Deasel");
		System.out.println(car.Wheels = "Tubeless");
		System.out.println(car.Seats = "Four Seater");
		System.out.println(car.Fuel_Tank = "Big Size");
		System.out.println(car.Lights = "Dual Lights on both sides");
		
		System.out.println("************************************");
		
		Truck truck = new Truck();
		System.out.println(truck.Steering = "Big");
		System.out.println(truck.Music_System = "Samsung");
		System.out.println(truck.Seat_Belt = "Yes");
		System.out.println(truck.Air_Conditioner = "Samsung");
		System.out.println(truck.Container = "Big");
		System.out.println(truck.Engine = "Deasel");
		System.out.println(truck.Wheels = "Big");
		System.out.println(truck.Seats = "Dual Seater");
		System.out.println(truck.Fuel_Tank = "Big");
		System.out.println(truck.Lights = "Four Front Lights");


	}

}

The output of the Program

Output
Output

As you can see in the above execution. Bike class has only one property Handle in the class and the rest of the extra properties are Inherited from Vehicle class. The same goes for Car and Truck classes. Both Car and Truck Classes have inherited several properties from Vehicle Class.

Abstraction

Abstraction in programming world means, hide complexity from the user and show only what is important to the user. At this point, we are in the very initial stage of understanding programming concepts so I will give you a very straightforward example to understand Abstraction.

When you use your cellphone and click on the call button to call your friend, you see a screen that shows the call is in progress. In a few moments, the call gets connected to your friend and you start talking. In this example, you do not worry about how this call was connected.

In programming, the Abstract class is used so that some of the methods of the class can be described by the person who is using the class.

  • An abstract class will have an abstract keyword before the class name.
  • An abstract class is very close to the Interface class with a tiny difference.
  • An abstract class may contain variables, properties, and Abstract and Non-Abstract classes and Abstract and Non-Abstract Methods.
  • A non-abstract class would have implementation whereas Abstract class will not have implementation but the only declaration.
  • In a class, if you declare one method as abstract, the whole class becomes an abstract class.

Example – Let us say we create a person class. In-person class there can be several methods such as he eats, he breathes, speaks but in easting habit, he can be Vegetarian or Non-Vegetarian so eat method will not have an implementation. The class that will extend the person class will provide the implementation of the eat method. In this case, we will have to create two more classes that will provide an implementation for the eat class, one class will be Vegetarian and the other class will be Non_Vegetarian. Let’s see this example in the form of code below.

This is the Person Class

package Test.Project.Person;

public abstract class Person {
	public String SkinColor;
	public void speak() {
		System.out.println("Shares his Thoughts");
	}
	
	public abstract void eat();

}

This is the Vegetarian Class

package Test.Project.Person;

public class Vegetarian extends Person{

	@Override
	public void eat() {
		System.out.println("Eats Vegetarian Food Only");
		
	}

}

This is the Non_Vegetarian Class

package Test.Project.Person;

public class Non_Vegetarian extends Person{

	@Override
	public void eat() {
		System.out.println("Eats Non-Vegetarian and Vegetarian");
		
	}

}

This is the Main Class Where We Run Our Project

package Test.Project;

import Test.Project.Person.Non_Vegetarian;
import Test.Project.Person.Person;
import Test.Project.Person.Vegetarian;

public class Hello {

	public static void main(String[] args) {
		Person Nady = new Vegetarian();
		Nady.eat();
		Nady.speak();
		
		Person John = new Non_Vegetarian();
		John.eat();
		John.speak();
		String SkinColor = John.SkinColor = "White";
		System.out.println(SkinColor);

	}

}

The Output of This Program

Output
Output

As you can see in the above program Vegetarian and Non_Vegetarian are two separate classes. Both classes are extending Person Class. When a class is extended by another class, all the methods and Variables of the class get shared to that class. As you can see Skin Color Variable is also being shared in this program.

Polymorphism

As you can see polymorphism is made with a combination of two words. Poly and Morphism. Poly means Multiple and morphism comes from morph. Morph means form. Polymorphism means multiple forms of something.

In programming when a single piece of code or object behaves differently in a different when bonded with other objects. It is also called late binding.

We have two concepts in Polymorphism.

  • Method Overloading
  • Method Overriding

Method Overloading – Method overloading happens when we have two methods with the same name and different signature in the same class.

Method Overloading is also possible if we have two classes in the child-parent relationship. Class A has a child Class B. Class A and Class B has a method with the same name but different signatures. Based on the signature Class chooses the right method to invoke and this is called method overloading.

ClassA Code

package test.ClassA;

public class ClassA {
	public String Message;
	
	public ClassA(String Message) {
		
		this.Message = Message;
		
	}
	
	public ClassA() {
		
	}
	
	public void show() {
		System.out.println("No Message");
	}
	
	public void show(String message) {
		System.out.println(message);
		
	}
}

ClassB Code

package test.ClassB;

import test.ClassA.ClassA;

public class ClassB extends ClassA {

	public ClassB(String Message) {
		super(Message);
		
	}
	public ClassB() {
		
	}

}

Execution of the Program

import test.ClassA.ClassA;
import test.ClassB.*;

public class Hello {

	public static void main(String[] args) {
		
		ClassA classA = new ClassA();
		classA.show();
		System.out.println("************************");
		classA.show("This is a Custom Message from ClassA");
		System.out.println("************************");
		ClassB classB = new ClassB();
		classB.show("This is a Custom Message from ClassB");
		System.out.println("************************");
		classB.show();
		
		}

}

The output of Method Overloading Example

method overloading output
Method Overloading Output

As you can see in our ClassA, we have two methods with the same names. One show method is without parameter and another show method is with a parameter that takes a string. The class chooses the right method based on the input. If the input is received without parameter the class invokes the parameterless method. If the input is with a string, the class invokes the parameter method.

You can also see the ClassB does not even have a single method inside it but ClassB extends ClassA which means whatever public properties and methods are available in ClassA will automatically be available for ClassB. This is why when ClassB is asked to invoke a show method it invokes the parent’s show method.

Method Overriding – Method Overriding happens when Child and Parent classes have methods with the same name and Signature. The child class gives preference to its own method rather than invoking the parent class method. If the child class did not have a method with the same name, it was supposed to invoke the parent class method, because it has its own method with the same name, child class overrides parent class method.

Let us see method overriding with the same example as Method overloading. We did not have any method is ClassB now let’s create a show method in ClassB and see how it works.

ClassA Code

package test.ClassA;

public class ClassA {
	public String Message;
	
	public ClassA(String Message) {
		
		this.Message = Message;
		
	}
	
	public ClassA() {
		
	}
	
	public void show() {
		System.out.println("No Message");
	}
	
	public void show(String message) {
		System.out.println(message);
		
	}
}

ClassB Code

package test.ClassB;

import test.ClassA.ClassA;

public class ClassB extends ClassA {

	public ClassB(String Message) {
		super(Message);
		
	}
	public ClassB() {
		
	}
	public void show() {
		System.out.println("This is a Hard Coded Message from ClassB Show Method");
	}

}

Execution of Method Overriding Program

import test.ClassA.ClassA;
import test.ClassB.*;

public class Hello {

	public static void main(String[] args) {
		
		ClassA classA = new ClassA();
		classA.show();
		System.out.println("************************");
		classA.show("This is a Custom Message from ClassA");
		System.out.println("************************");
		ClassB classB = new ClassB();
		classB.show("This is a Custom Message from ClassB");
		System.out.println("************************");
		classB.show();
		
		}

}

The output of Method Overriding Program Example

Method Overriding
Method Overriding

As you can see in the above example, we have created a show method in ClassB and now when ClassB is asked to invoke show method it does not go to the parent class. It has its own show method and it overrides parent class show method and shows the message on the screen as “This is a Hard-Coded Message from ClassB Show Method”.

With this example, we have successfully learned all the Object-Oriented Programming Concepts. I hope you have understood them all. In case of any confusion please go through the examples again or comment below. Have a good one guys!

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This website uses cookies to improve your experience. We'll assume you're ok with this. Thank you! Accept Read More