You are on page 1of 3

concept of Object-Oriented Programming language in Java.

What is Object Oriented Programming?


Object Oriented Programming is a programming paradigm that uses "objects" to design
applications and computer programs. It utilizes several techniques from previously established
paradigms, including inheritance, modularity, polymorphism, and encapsulation.

A software design method that models the characteristics of real or abstract objects using
software classes and objects.

Characteristics of “Object”:
State(what the object have), Behavior(what the objects do), Identity(what makes them unique)

For example, a car is an object.


Its states includes: Speed, RPM, Gear, Direction, Fuel level.
Its behavior includes: Change Gear, Go faster/slower, Go in reverse, stop.
Its identity includes: Number plate.

The objects and classes.


Classes: A class can be defined as a template/blue print that describes the behaviors/states that
object of its type support.

Objects: Objects have states and behaviors. Example: A dog has states - color, name, breed as
well as behaviors -wagging, barking, eating. An object is an instance of a class

Class in Java:
A class is a blue print from which individual objects are created.
Everything in Java is defined in a class.
In its simplest form, a class just defines a collection of data for example:
class Employee {
String name;
String emailId;
Date joiningDate;
}

The order of data fields and methods in a class is not significant.

A class can contain any of the following variable types.


Local variable
Instance variable
Class variable

Objects in Java:
Let us now look deep into what are objects. If we consider the real-world we can find many
objects
around us, Cars, Dogs, Humans, etc. All these objects have a state and behavior.
To create an object (instance) of a particular class, use the new operator, followed by an
invocation of a constructor for that class, such as:
new MyFirstClass();

The constructor method initializes the state of the new object.


The new operator returns a reference to the newly created object.
As with primitives, the variable type must be compatible with the value type when using object
references, as in:
MyFirstClass myClass = new MyFirstClass();

To access member data or methods of an object, use the dot (.) notation: variable.field or
variable.method()

Example:
public class EmployeeDemo {
public static void main(String[] args) {
Employee e1 = new Employee();
e1.name = "abc";
e1.emailId = "test@yourcompany.com";

System.out.println("Name: " + e1.name);


System.out.println("Email Address: " + e1.emailId); }
}

Constructor:
When we discussing about classes than one of the most important thing is constructors. Every
class has one constructor, more than one is also possible. If we do not explicitly write a
constructor for a class the Java compiler builds a default constructor for that class
Each time a new object is created, at least one constructor will be invoked, it is parameter
constructors or default constructors. The main rule of constructors is that they should have the
same name as the class and constructors has not return type.

Example:
public class Main{
int val;
public Main(){
val = 10;
}
public Main(int val){
this.val = val;
}
}
Creating an Object:
There are three steps for creating object from class.
Declaration: A variable declaration with a variable name with an object type.
Instantiation: The 'new' key word is used to create the object.
Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the
new object.

Example:
public class Main{
public static void main(String args[]){
String str = new String(“String”);
}
}

You might also like