You are on page 1of 32

Object Oriented Programming(OOPs)

What is OOP?

Object-Oriented Programming is a methodology or
paradigm to design a program using classes and
objects.The following are some concepts involving
OOPs.

Class

Object

Inheritence

Polymorphism
 Abstraction

Encapsulation
Object
 Any entity that has state and behavior
is known as an object. For example a
chair, pen, table, keyboard, bike, etc.
It can be physical or logical.
 It is defined as an instance of a class.
 e.g. : A dog is an object because it
has states like color, name, breed,
etc.
Class
 Collection of objects is called class. It is a
logical entity.
 A class can also be defined as a blueprint
from which you can create an individual
object. Class doesn't consume any space.
Access Modifiers In JAVA

As the name suggests access modifiers in Java helps to restrict the


scope of a class, constructor , variable , method or data
member. There are four types of access modifiers available in
java:
*default(no keyword required )
*public
*private
*protected
1.Default

When no access modifier is specified for a class , method or data


member – It is said to be having the default access modifier by
default.
The data members, class or methods which are not declared using any
access modifiers i.e. having default access modifier are accessible
only within the same package.
In the following example, we will create two packages and the classes
in the packages will be having the default access modifiers and we
will try to access a class from one package from a class of second
package.
Default(cont)

package p2;
package p1;
import p1.*;

//Class Geeks is having Default access modifier //This class is having default access modifier
class Geek class GeekNew

{ {

void display() public static void main(String args[])


{
{
//accessing class Geek from package p1
System.out.println("Hello World!");
Geeks obj = new Geek();
}

}
obj.display();
}
}
2.Private

Private: The private access modifier is specified using the keyword private.
The methods or data members declared as private are accessible only within the
class in which they are declared.
Any other class of same package will not be able to access these members.
It is illustrated in the following example:
Private(cont)

package p1; class B

class A {

{ public static void main(String args[])

private void display() {

{ A obj = new A();

System.out.println("GeeksforGeeks"); //trying to access private method of


another class
}
obj.display();
}
}

}
3.Protected


Private: The protected access modifier is specified using the keyword protected.

It is similar to private members but the only difference is that the protected
members are accessible within all classes of the same package whereas private
members are not.

It is illustrated in the following example:
4.Public


Private: The protected access modifier is specified using the keyword protected.

The member which has been declared public is visible to the world.
Access Modifiers In JAVA(cont)
Inheritance


Inheritance is a mechanism wherein a new
class is derived from an existing class.
 A class that is derived from another class is
called subclass or derived class.
 A class from which other classes are
derived is known as Base class.
The real life example of inheritance is child
and parents, all the properties of father are
inherited by his son.
Inheritance: Car ← Vehicle
Inheritance: Bike ← Vehicle
Inheritance:Main Class
Output
Polymorphism
 If one task is performed by different ways,
it is known as polymorphism.
 Suppose if you are in class room that time
you behave like a student, when you are
in market at that time you behave like a
customer, when you at your home at that
time you behave like a son or daughter,
Here one person present in different-
different behaviors.
Abstraction
 Abstraction is the process of abstraction in
Java which is used to hide certain details
and only show the essential features of
the object. In other words, it deals with the
outside view of an object (interface).
 when we ride a bike, we only know
about how to ride bikes but can not
know about how it work? And also we
do not know the internal functionality
of a bike.
Advantages of
Abstraction

 It reduces the complexity of viewing the things.


 Avoids code duplication and increases
reusability.
 Helps to increase security of an application or
program as only important details are provided
to the user.
Encapsulation
 Encapsulation in Java is a process of
wrapping code and data together into a
single unit, for example, a capsule which
is mixed of several medicines. We can
create a fully encapsulated class in Java
by making all the data members of the
class private .
Overriding
 The main advantage of method overriding is that
the class can give its own specific
implementation to a inherited method without
even modifying the parent class code.

This is helpful when a class has several child
classes, so if a child class needs to use the
parent class method, it can use it and the other
classes that want to have different
implementation can use overriding feature to
make changes without touching the parent class
code.

You might also like