OOPS Concept in Java (Object Oriented Programming in Java)

Introduction to OOP Concepts in Java:

Java DataTypes and other basic Java Concepts were explained in detail in our previous tutorial.

Object-oriented programming is a way of designing and programming a software by manipulating objects and the objects are the main part of the programming.

If you want to build a house, you need to design the house first. House design should have rooms, doors, windows etc. Here room is a class.

Watch out our entire series of Java for beginner tutorials here.

Check out the below Hands-On Video Tutorial on:

OOPS Concept in Java – Part 1:

In-depth look at OOPS Concept in Java – Part 2:

Classes & Objects in Java:

Encapsulation and Polymorphism in Java:


The class is a blueprint/prototype of a real-world object and it models the state and behavior of that real-world object.

Class Declaration

 class <em>MyClass</em> {
// field, constructor, and
// method declarations
 Class room(){

//room should have dimension.

Dimension is attribute.
//in the room, we are going keep things. Keeping is the function we are going
to do.
Keepthings (); this is a method in java.

Class declaration


Software objects are like real life objects with state and behavior. The state will be stored as a field and expose its behavior as methods.

Here room is like general design. Within this room, you should design a living room, cooking room, bedroom.

 Class CookingRoom(){
Dimension is attribute.

//here we should have method to keep cooking things.
This method is same like room class keep things method. 
but it should   have   some more features to hold cooking items.

 Class LivingRoom(){

Dimension is attribute.

//here we should have method to keep Living Room things.
This method is same like room class keep things method. 
but it should   have   some more features to hold living room items(like tv etc).

watching tv();

Here “Keepthings()” method should do the basic functionalities required for room and it should have additional specification according to “living room” class and “cooking room” class requirement. So the two classes should inherit the methods in the “room” class.



Behavior (methods) and state of one class inherited to the other class are called inheritance. The methods and state are inherited from the parent class to the child class.


Class  CookingRoom extends Room{
Class  LivingRoom extends Room{


In biology, polymorphism refers to the spices which can have many different forms.

With the same concept in object-oriented programming, the child class can inherit its parent class methods, also it can add unique features to that behavior. Polymorphism can be achieved by method overloading or overriding.


 public class OverloadDemo {
public int add( int a,int b) {
int rs=a+b;
return rs;

public int add( int a,int b,int c) {

int rs=a+b+c;
return rs;
public static void main(String[] args) {

OverloadDemo ov=new OverloadDemo();




 public class Bicycle {
int wheels=2;
String seat;
public void riding() {
System.out.println("bicycle is used for riding");


 public class RoadBicycle extends Bicycle{
public void riding() {
System.out.println("RoadBicycle is used for road riding");


 public class TestBicycle {

public static void main(String[] args) {
Bicycle b=new RoadBicycle();


class testbicycle

OUTPUT: Here the child class method overrides the parent class method.

output- overriding

Super Keyword

Super keyword is a reference variable which is used to refer to the immediate parent class object. With super keyword, you can refer the parent class method or variables or constructor.

This Keyword

This keyword is used to refer to the current class object. Using this keyword, you can access the current class instance variable or current class method. This can be passed as an argument in the method call.

public class DemoThisKeyword {

private int accno;
private int  balance;

public void setvalues(int accno,int balance) {



public int showdata()

return balance;

public static void main(String[] args) {
// TODO Auto-generated method stub

DemoThisKeyword obj =new DemoThisKeyword();
obj.setvalues(11, 100);


this keyword


Java constructors are like methods which will be called when the object is created for the class. The constructor should have the same name as the class.

Class Myclass{
Method1() {


While Creating an object for the class, Myclass myobj =new Myclass ();

The constructor method will be called. The constructor is automatically created by Java compiler for all the classes by default.

public class ConstructorExampleProgram {

int employee_age;
String employee_name;
int employee_salary;

//Default constructor

//Parameterized constructor
ConstructorExampleProgram(String n,int a,int b){
public static void main(String args[]){
ConstructorExampleProgram obj1 = new ConstructorExampleProgram();
ConstructorExampleProgram obj2 =
new ConstructorExampleProgram("clare", 56,7500);
System.out.println(obj1.employee_name+" "+obj1.employee_age+"

System.out.println(obj2.employee_name+" "+obj2.employee_age+"

Constructor sample programme

Rules to be followed for the constructor:

  • The constructor should have the same name as the class name.
  • The constructor should not have a return statement.

Points to be noted:

  • In Object-oriented programming, objects are the main part of programming.
  • OOP concepts are object, class, inheritance, polymorphism, constructor.
  • Super keyword used to refer to the parent class members, and this keyword is used to refer to a current class object.
  • Constructors are like methods which will be called when the object is created for the class.


In this tutorial, we have explored the object-oriented concepts like class, objects, inheritance, polymorphism in detail.

Our upcoming tutorial will explain more about Interfaces and Abstract classes in detail!!

PREV Tutorial | NEXT Tutorial