OOPS Concept in Java
In this post, we learn OOPs Concept in Java. OOPS Stands for Object Oriented Programming System. In this tutorial, I will introduce you to Class, Object, Constructor, Abstraction, Encapsulation, Inheritance, Polymorphism, Interface etc.,
Class:
A class is a blueprint or prototype from which objects are created. A class contains variables (data types) and methods (functions) to describe the behavior of an object.
class Class_Name{ member variables methods }
Object:
Object is a software bundle of related state and behavior. Objects have two characteristics namely state and behavior.
We can also say, Object is an entity that has state and behavior.
State: It represents value (data types/variables) of an object
Behavior: It represents the functionality (methods) of an object
Object is an instance of a class.
Sample:
class Computer{ String Maker; int Model; String Color; void turnOn{ //statement(s) } void turnoff{ //statement(s) } }
Example:
State: Maker, Model, Color etc.,
Behavior: Turn on, Turn off etc.,
To understand what is a class and object in detail, let me give you a basic example related to a computer. Computer with Model and Price.
Assume, you have two computers of Apple and Lenovo. Now say the model of Apple is MacBook Pro and the model of Lenovo is Yoga. The price of Apple is $299 and the price of Lenovo is $99.
Computer is a class which has two attributes namely Model and Price. Apple and Lenovo are the objects of the class Computer.
Let’s see how to create an object:
Compter laptop = new Computer();
Class: Computer
Object: laptop
Keyword: new
Constructor: Computer()
Computer is a class name followed by the name of the object laptop. Then there is a “new” keyword which is used to allocate memory. Finally, there is a call to constructor “Computer()”. This call initializes the new object.
We create an Object by invoking the constructor of a class with the new keyword.
I hope, now you got to know how to create an object
Method:
Earlier we have seen Object is an entity which has both state and behavior. Here we are going to discuss about behavior of an Object. Method describes the behavior of an Object. A method consists of collection of statements which performs an action.
Methods are also known as procedures or functions
Let’s see an example of a method declaration.
public int sum(int a, int b, int c){ // method body } public void sum(int a, int b, int c){ // method body }
Every method declaration must have return type of the method, a pair of parenthesis, and a body between braces
In general, method consists of 6 components.
Modifiers: private, public, and others
Return Type: The data type of the value returned by the method, or void if the method doesn’t return a value.
Method Name: Name of the method.
Built in methods are standard such as System.out.println();
User defined methods accept any names which a developer assigns.
Parameters inside parenthesis: list of parameters preceded by their data types and separated with a comma. If no parameters then you must specify an empty parenthesis.
Exception: Exceptions depends on the operation of the method
Method Body: Method body should be enclosed between braces
The signature of the above declared method is
Int sum(int a, int b, int c)
A method has a unique name within its class. However, a method might have the same name as other methods due to method overloading.
Let’s see how to call methods using an object.
class Computer{ // method void turnOn{ //statement(s) } public static void main (String [] args){ // Created an object Computer laptop = new Computer(); //Method called laptop.turnOn(); } }
Hope you have heard a phrase “Instantiating a class”. The phrase “Instantiating a class” means the same thing as “Creating an Object” which we did in the above program. Whenever you create an Object, it means you are creating an instance of a class, therefore “instantiating a class”.
Methods are of two types
1. Built-in methods or Pre-defined methods: Methods such as String methods, Date & Time methods, etc.,
2. User Defined methods: It contains ‘method with returning value’ and ‘method without returning any value’
Constructor:
Constructor in Java is used in the creation of an Object that is an instance of a Class. Constructor name should be same as class name. It looks like a method but its not a method. It wont return any value. We have seen that methods may return a value. If there is no constructor in a class, then compiler automatically creates a default constructor.
Inheritance:
Inheritance is a process where one class inherits the properties of another class.
Let’s say we have two classes namely Parent Class and Child Class. Child class is also known as Derived Class. As per the above definition, the Child class inherits the properties of the Parent Class. The main purpose of Inheritance is Code Reusability.
Assume we have a Class named Laptop, Apple MacBook Pro, Lenovo Yoga. Apple MacBook Pro and Lenovo Yoga classes extend the Laptop Class to inherit the properties of the Laptop Class.
We will see detailed explanation with some example programs about Inheritance in the post related to Inheritance.
Encapsulation:
Encapsulation is a mechanism of binding code and data together in a single unit. Let’s take an example of Capsule. Different powdered or liquid medicines are encapsulated inside a capsule. Likewise in encapsulation, all the methods and variables are wrapped together in a single class.
We will see detailed explanation with some example programs about Encapsulation in the post related to Encapsulation.
Polymorphism:
Polymorphism allows us to perform a task in multiple ways. Let’s break the word Polymorphism and see it, ‘Poly’ means ‘Many’ and ‘Morphos’ means ‘Shapes’.
Let’s see an example. Assume we have four students and we asked them to draw a shape. All the four may draw different shapes like Circle, Triangle, and Rectangle, .
We will see detailed explanation with some example programs about Polymorphism in the post related to Polymorphism.
Abstraction:
Abstraction is the methodology of hiding the implementation of internal details and showing the functionality to the users.
Example: Mobile Phone.
A layman who is using mobile phone doesn’t know how it works internally but he can make phone calls.
Abstraction in Java is achieved using abstract classes and interfaces.
We will see detailed explanation with some example programs about Abstraction in the post related to Abstraction.
Must Read: Java Tutorial