Let’s get to know about Object-Oriented Programming

Object-Oriented Programming concepts with Java

Prathibha Perera
Tech x Talent
7 min readAug 9, 2021

--

GIF by Author

What is Object-Oriented Programming?

If the programing using an object concept, we can call it object-oriented programming. Without writing procedures or methods that perform on data, like in procedural programming, creating objects that contain both data and methods can be used in object-oriented programming. The importance of the using OOP concept in programming is we can follow the “DRY” principle.

DRY — Don’t Repeat Yourself

When we are coding, the DRY principle is a major concern to consider and it helps to increase the coding effectiveness by reducing repetition of code.

What is a Class?

Class is a blueprint that creates objects and it is common to all objects.

What is an Object?

An object is defined as an instance of a class and it inherits the things that are related to the class.

GIF by Author

Few object-oriented languages: Java, C++, C#, Python, JavaScript, Dart

When we are studying object-oriented programming there are some major concepts we should focus on. As you have heard before, they are called “4 Pillars of Object-Oriented Programming”. The rest of the article will describe the 4 Pillars of Object-Oriented Programming.

Pillar 1 — Inheritance

I am starting with explaining Inheritance because it will help you to understand other concepts in an easier manner after you know what is exactly “Inheritance” in OOP. And for the code implementation purpose, I will use Java and examples will bring through Java code examples.

Through Inheritance, it will allow us to use one class’s features inside another class. Therefore we should know what is a superclass and what is a subclass?

  • Superclass — Parent class is another term for superclass and it is the class being inherited from.
  • Subclass — Child class is another term for the subclass and it is the class that inherits from another class and it is used extends keyword.

By using Inheritance, it provides“Re-usability” for our code implementation hence if we need to create a new class with the features that are having an existing class, we can simply derive our new class from the existing class. The final keyword can be stoped for making any inherited classes.

Following Java-Inheritance example will clarify the concept behind the Inheritance.

class Sampleclass1{// the Sampleclass1 with 2 fields
public int var1;
public int var2;
// the Sampleclass1 class constructor
public Sampleclass1(int var1, int var2){
this.var1= var1;
this.var2= var2;
}
// the Sampleclass1 class method 1;public void method_1(){System.out.println("Method 1 from Parent class");
}
// the Sampleclass1 class method 2;
public void method_2(){System.out.println("Method 2 from Parent class");
}
}
// derived class
class Sampleclass2 extends Sampleclass1{
// the MountainBike subclass adds one more field
public int var3;
// the Sampleclass2 subclass constructorpublic Sampleclass2(int var1, int var2, int va3){// invoking Sampleclass1 constructor
super(var1, var2);
var3= var3;
}
// the Sampleclass2 class method 1;public void method_3(int var1, int var2, int var3){System.out.println("Method 1 from Child class");
System.out.println("****** " + "Value 1: " + var1 + " Value 2: " + var2 + " Value 1: " + var3 + " *****");
}
}
public class Test {public static void main(String args[]){Sampleclass2 sc = new Sampleclass2(3, 100, 25);sc.method_1();
sc.method_2();
sc.method_3(3,100,25);
}
}
Image by Author - sample output of the Java-Inheritance code

Pillar 2 — Polymorphism

Poly (many) + morphs (forms) = Polymorphism

As I mentioned above Polymorphism is related to having many forms that are allowed to have single action in different ways. As in a real-world scenario, a person is playing different roles in the home as a Parent for their children, Child to her parents, Grandchild to her GrandParents and etc.

Polymorphism is subdivided into 2 categories,

  1. Compile-time Polymorphism => Static Polymorphism => Overloading

Overloading can be achieved through multiple functions with the same name but passing the different numbers of arguments or different types of arguments.

The following examples will clarify the concept behind Overloading.

class Sumofinputs {
//2 methods has been declared with same name "Sum" > Overloading
//Different types of arguments example

// Sum Method with int type 2 parameters
static int Sum(int x, int y)
{
return x + y;
}

// Sum Method with double type 2 parameters
static double Sum(double x, double y)
{
return x + y;
}
}

public class Test {
public static void main(String[] args)
{

System.out.println(Sumofinputs.Sum(2, 4));

System.out.println(Sumofinputs.Sum(5.5, 6.3));
}
}
Image by Author — sample output of the Java-overloading code
class Sumofinputs {
//2 methods has been declared with same name "Sum" > Overloading
//Different number of arguments example

// Sum Method with 2 parameters
static int Sum(int x, int y)
{
return x + y;
}

// Sum Method with 3 parameters
static int Sum(int x, int y, int z)
{
return x + y + z;
}
}

public class Test {
public static void main(String[] args)
{

System.out.println(Sumofinputs.Sum(2, 4));

System.out.println(Sumofinputs.Sum(2, 4, 6));
}
}
Image by Author — sample output of the above Java-overloading code

2. Run time Polymorphism => Dynamic Method Dispatch => Overriding

Overridden methods will be resolved at the run time and overriding is a way that the methods are declaring at the child classes such, declared same as in the parent class.

The following example will clarify the concept behind Overriding.

class Sampleclass1 {
//Parent class method that is overriding
void method_1()
{
System.out.println("Method from Parent class");
}
}
//Child class 1 with overriden method
class Sampleclass2 extends Sampleclass1 {

void method_1()
{
System.out.println("Method from Sampleclass2 class");
}
}
//Child class 2 with overriden method
class Sampleclass3 extends Sampleclass1 {

void method_1()
{
System.out.println("Method from Sampleclass3 class");
}
}

public class Test {
public static void main(String[] args)
{

Sampleclass1 sc;

sc = new Sampleclass2();
sc.method_1();

sc = new Sampleclass3();
sc.method_1();
}
}
Image by Author — sample output of the Java-overriding code

Pillar 3— Abstraction

The abstraction concept is used to hide unnecessary details from the user and display only necessary details.

A simple real-world example is, consider bank ATM or CDM machine that we can withdraw and deposit money. We only do pressing necessary buttons, take money through the machine, put money into the machine, and observe the machine display. But we exactly do not know what is the implementation of the machine. The same kind of thing is happening in the Abstraction concept.

I will use the Java interface and abstract class to make understand the scenario. Before going to the implementations, we should have a basic idea about the Abstract class and the Abstract method.

  • An abstract class is declared with the “abstract” keyword
  • An abstract class has one or more abstract methods and it can have concrete methods also. But it is necessary to mention that non-abstract classes may not have abstract methods.
  • An abstract method is a method without implementation(method body) and just contains only method definition.

The following example will clarify the concept behind Abstraction.

abstract class Sampleclass1{  
abstract void method();
}
class Sampleclass2 extends Sampleclass1{
void method(){
System.out.println("Sampleclass2 class method");
}
}
public class Test {
public static void main(String args[]){
Sampleclass1 sc = new Sampleclass2();
sc.method();
}
}
Image by Author — sample output of the Java-Abstraction code

Pillar 4 — Encapsulation

The major purpose of the Encapsulation principle is to hide class data and variables from one class to another class or wrapping up data and functions into a single unit. This can be done through declaring variables as private in the class and implement public methods.

A simple real-world example is an organization that may contain several departments that are using a common website but each other departments can not access data from them. This concept ensures accuracy and reduces unexpected distractions on data.

The following example will clarify the concept behind Encapsulation.

class Sampleclass1 {
// private variables var1 and var2 declared & these can only be accessed by public methods of class
;
private int var1;
private int var2;

// get method for var1 to access private variable var1
public int getVar1() { return var1; }

// get method for var1 to access private variable var1
public int getVar2() { return var2; }

// set method for var1 to access private variable var1
public void setVar1(int newVar1) { var1 = newVar1; }


// set method for var1 to access private variable var1
public void setVar2(int newVar2) { var2 = newVar2; }
}

public class Test {
public static void main(String[] args)
{
Sampleclass1 sc = new Sampleclass1();

// setting values of the variables
sc.setVar1(10);
sc.setVar2(20);

// Displaying values of the variables
System.out.println("1: " + sc.getVar1());
System.out.println("2: " + sc.getVar2());
}
}
Image by Author — sample output of the Java-Encapsulation code

Cool… 😊 We did it...

Conclusion

This article provides you the things that you should basically know about Object-Oriented Programming and example will interpret the coding implementations.

Hope you enjoyed the article and if so hit a clap.

Thank you for reading!!!🤗

--

--

Prathibha Perera
Tech x Talent

Software Engineer at Corzent. Tech enthusiast and Dedicated.