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 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.
Example:-
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
Behavior: It represents the functionality (methods) of an object
Object is an instance of a class.
Example:-
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.,
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()
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.
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
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.
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.
No comments:
Post a Comment