This write-up is about OOPs concepts in Java and its implementation in Java. We would go through an overview of the major OOPs concepts in Java with examples to get a decent theoretical understanding of the same. OOP which stands for Object Oriented Programming is a set of ideas which help in implementing real world entities through software programs. There are many programming languages which support OOP concepts and some of them are C++, C#, Java, Smalltalk, Python.
In this blog, we would discuss the OOP concepts in general and how much of it we could implement in Java as per the below structure:-
- OOP concepts
- Classes
- Objects
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
- Advantages of Java OOP Concepts
OOP concepts
Object-oriented programming is a programming methodology where everything is represented as an object which contains data and methods (also called as functions by some). Objects pass messages/data to each other via methods. The object decides what to do with a received message/data and does further processing as per the way it is coded. There are 4 tenets of OOP systems namely Encapsulation, Abstraction, Inheritance, Polymorphism which would be discussed in more detail down the line.
Click here – Importance Of Cloud Computing In 2021
Classes
A “class” in Java defines the blueprint of objects by way of defining the state (data) and behavior (operation/methods). Cookie cutter is an example that is very apt and often used to explain the concept of “class” and cookies as the concept of “objects”. Classes can be used to create any number of objects of its kind. A class in general could have one or more of the following as part of its declaration “Class name”, “Modifiers”, “Constructors”, “Fields”, “Methods”, “class body” (within { } brackets), “Nested class and interface”.
Let us consider a class named Dog, whose state can be represented by variables like name, age, breed. And the behavior can be represented by methods like sleep, feed, play.
public class Dog {
private String name;
private int age;
private String breed;
/*
Instance methods: which represent behaviors of Dog
*/
public void sleep(){
System.out.println(“The dog – “ + name +“is sleeping”);
}
public void play(){
System.out.println(“The dog – “ + name +“is playing”);
}
public void feed(){
System.out.println(“The dog – “ + name +“is feeding”);
}
public String getName() {
return name;
}
}
Instance variables like “name, age, breed” which represent states of “Dog” and declared using access modifiers as “private” are only accessible to methods of class “Dog”. The variables and methods defined within a class are called as instance variables and instance methods respectively. The program logic is always coded in the methods. As a thumb rule we make instance variables private and instance methods public in Java.
Objects
As mentioned earlier objects can be considered as cookies created out of the cookie cutter class. And it represents a real-world entity which has an identity, state, and behavior. Below is an example
public class ObjectExample {
public static void main(String[] args) {
Dog rusty = new Dog();
String dog_name = rusty.getName();
rusty.sleep();
}
}
In the above example the variable “rusty” is an object of class “Dog” which has been created via the class constructor. A constructor is a class method which is of the same name as the class. An operation is performed on the object “rusty” by calling the method “sleep”.
Encapsulation
The concept of enclosing data and the methods (program code that would work on it) is called encapsulation. And like in most OOP languages it’s implemented via classes and access modifiers in Java too. It’s achieved in Java by making all the data members of the class private and accessing them via methods. In the example given above the method “getName” is used to get and set the name of the dog into the variable “dog_name”, thus not allowing direct access to the object variable “name”.
Abstraction
Similar to the concept of encapsulation is “Abstraction” where only the functionality is shown to the user. And in Java, this is achieved in two ways: abstract class and interface. The keyword “abstract” could be used with classes as well as methods.
An abstract class is declared by preceding the class keyword with the keyword “abstract”. We can’t create objects of abstract classes but they can have constructors, static methods, and final methods. One important thing to remember about it is even if one abstract method exists inside a class then the whole class becomes an abstract class.
An interface is a class like structure which doesn’t have any concrete definitions.
Inheritance
Inheritance is a concept in which one object acquires all the states and behaviors of its parent object. The following are the types of inheritance in Java: “single, multilevel, hierarchical, multiple and hybrid”. A class allows single, multilevel and hierarchical inheritances whereas an Interface allows multiple and hybrid inheritances. A class can inherit only one class but it can inherit any number of interfaces. An interface can inherit more than one interfaces.
The keyword “extends” is used to implement inheritance in Java for classes and “implements” when interfaces are to be inherited by classes.
Single level – In this only one class derives from the parent class. Syntax:
class Parent {
….
}
class Child extends Parent {
….
}
Multilevel- In this only one class derived from another class which is further inherited by another class. Syntax:
class GrandParent {
….
}
class Parent extends GrandParent {
….
}
class Child extends Parent {
….
}
Hierarchical level – This one states that 2 or more derived classes use a single parent. Syntax:
class Parent {
….
}
class Child1 extends Parent {
….
}
class Child2 extends Parent {
….
}
Multiple inheritance – This is implemented using interfaces where multiple interfaces are inherited by a class using the keyword “implements”.
class Child1 implements Interface1, Interface2 {
….
}
Hybrid inheritance – This one is a combination of multiple and multilevel inheritance
Polymorphism
It is an OOP tenet which allows the coder to perform a single action in multiple ways.
There are 2 types of polymorphism, namely “Static polymorphism” which gets resolved during compilation and “Dynamic polymorphism” in which a call to an overridden method is resolved at runtime. Static polymorphism is mostly implemented using method overloading wherein one of multiple methods with the same name but different signs are identified and invoked based on the arguments passed to it. Dynamic polymorphism is used when 2 methods with the same name and signs are implemented in 2 different classes and their invocation is decided at runtime when the code is executed.
Advantages of Java OOP Concepts
There are many advantages of implementing OOP concepts in Java and below are some:
- Reduced complexity in coding by dividing the software into meaningful and manageable pieces
- We could add or modify new features by introducing a few new objects and objectively modifying the existing code.
- Code reusability since objects could be reused across applications thus reducing development time
This brings us to the end of this article on OOPS concepts in Java. If you wish to learn more such concepts, you can check out the free online courses with certificate on Great Learning Academy.
Click here – A Weekend in Peoria, Illinois: What to Do