0% found this document useful (0 votes)
11 views29 pages

Oops Concept

The document explains Object-Oriented Programming (OOP) concepts such as classes, objects, inheritance, encapsulation, abstraction, and polymorphism, highlighting their importance in programming languages like Java. It details the benefits of OOP, including improved productivity and software quality, and describes the four main principles of OOP along with various types of inheritance. Additionally, it covers encapsulation and abstraction methods in Java, as well as the concept of polymorphism and its types.

Uploaded by

Rohit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views29 pages

Oops Concept

The document explains Object-Oriented Programming (OOP) concepts such as classes, objects, inheritance, encapsulation, abstraction, and polymorphism, highlighting their importance in programming languages like Java. It details the benefits of OOP, including improved productivity and software quality, and describes the four main principles of OOP along with various types of inheritance. Additionally, it covers encapsulation and abstraction methods in Java, as well as the concept of polymorphism and its types.

Uploaded by

Rohit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Java OOPs Concepts With Examples

• Object Oriented programming is a


programming style which is associated with
the concepts like class, object, Inheritance,
Encapsulation, Abstraction, Polymorphism.
Most popular programming languages like
Java, C++, C#, Ruby, etc. follow an object-
oriented programming paradigm.
What are the benefits of Object Oriented
Programming?
• Improved productivity during software
development
• Improved software maintainability
• Faster development sprints
• Lower cost of development
• Higher quality software
What are the four basic principles/ building blocks of
OOP (object oriented programming)?
• The building blocks of object-oriented
programming are Inheritance, Encapsulation,
Abstraction, and Polymorphism.
• Let’s understand more about each of them in
the following sequence:
• Inheritance
• Encapsulation
• Abstraction
• Polymorphism
Object Oriented Programming : Inheritance

• In OOP, computer programs are designed in


such a way where everything is an object that
interact with one another.
• Inheritance is one such concept where the
properties of one class can be inherited by the
other.
• It helps to reuse the code and establish a
relationship between different classes.
• As we can see in the image, a child inherits the
properties from his father. Similarly, in Java, there are
two classes:
• 1. Parent class ( Super or Base class)
• 2. Child class (Subclass or Derived class )
A class which inherits the properties
is known as Child Class
whereas a class whose
properties are inherited
is known as Parent class.
• Inheritance is further classified into 4 types:
Single Inheritance
• In single inheritance, one class inherits the
properties of another.
• It enables a derived class to inherit the properties
and behavior from a single parent class.
• This will in turn enable code reusability as well as
add new features to the existing code.
• Here, Class A is your parent class and Class B is
your child class which inherits the properties and
behavior of the parent class.
syntax for single inheritance:
Class A
{
---
}
Class B extends A
{
---
}
Multilevel Inheritance

• When a class is derived from a class which is also derived


from another class, i.e. a class having more than one
parent class but at different levels, such type of
inheritance is called Multilevel Inheritance.
• If we talk about the flowchart, class B inherits the
properties and behavior of class A and class C inherits
the properties of class B.
• Here A is the parent class for B and class B is the parent
class for C. So in this case class C implicitly inherits the
properties and methods of class A along with Class B.
That’s what is multilevel inheritance.
syntax for multilevel inheritance in Java:

Class A{
---
}
Class B extends A{
---
}
Class C extends B{
---
}
Hierarchical Inheritance

• When a class has more than one child classes


(sub classes) or in other words, more than one
child classes have the same parent class, then
such kind of inheritance is known
as hierarchical.
• If we talk about the flowchart, Class B and C are
the child classes which are inheriting from the
parent class i.e Class A.
syntax for hierarchical inheritance in Java

Class A{
---
}
Class B extends A{
---
}
Class C extends A{
---
}
Hybrid Inheritance
• Hybrid inheritance is a combination
of multiple inheritance and multilevel inheritance.
Since multiple inheritance is not supported in Java
as it leads to ambiguity, so this type of inheritance
can only be achieved through the use of the
interfaces.
• If we talk about the flowchart, class A is a parent
class for class B and C, whereas Class B and C are
the parent class of D which is the only child class of
B and C.
Object Oriented Programming : Encapsulation

• Encapsulation is a mechanism where you bind


your data and code together as a single unit.
• It also means to hide your data in order to
make it safe from any modification.
• The best way to understand encapsulation is to look at
the example of a medical capsule, where the drug is
always safe inside the capsule. Similarly, through
encapsulation the methods and variables of a class are
well hidden and safe.
We can achieve encapsulation in Java by:
• Declaring the variables of a class as private.
• Providing public setter and getter methods to
modify and view the variables values.
look at the code below to get a better understanding of
encapsulation

public class Employee {


private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
}
}
Object Oriented Programming : Abstraction

• Abstraction basically deals with hiding the


details and showing the essential things to
the user.
• If you look at the image here, whenever we
get a call, we get an option to either pick it
up or just reject it. But in reality, there is a
lot of code that runs in the background.
• So you don’t know the internal processing
of how a call is generated, that’s the beauty
of abstraction. Therefore, abstraction helps
to reduce complexity.
We can achieve abstraction in two ways:
a) Abstract Class
b) Interface
Abstract class
• Abstract class in Java contains the ‘abstract’ keyword. Now
what does the abstract keyword mean? If a class is declared
abstract, it cannot be instantiated, which means you cannot
create an object of an abstract class.
• Also, an abstract class can contain abstract as well as
concrete methods.

Note: You can achieve 0-100% abstraction using abstract class.

• To use an abstract class, you have to inherit it from another


class where you have to provide implementations for the
abstract methods there itself, else it will also become an
abstract class.
syntax of an abstract class
Abstract class Mobile // abstract class mobile
{
Abstract void run(); // abstract method
}
Interface
• Interface in Java is a blueprint of a class or you can say it is
a collection of abstract methods and static constants.
• In an interface, each method is public and abstract but it
does not contain any constructor.
• Along with abstraction, interface also helps to achieve
multiple inheritance in Java.

Note: You can achieve 100% abstraction using interfaces.

• So an interface basically is a group of related methods with


empty bodies.
Object Oriented Programming : Polymorphism

• Polymorphism means taking many forms,


where ‘poly’ means many and ‘morph’ means
forms. It is the ability of a variable, function or
object to take on multiple forms.
• In other words, polymorphism allows you
define one interface or method and have
multiple implementations.
• As you can see in the figure, there is a parent
class- BowlerClass and it has three child
classes: FastPacer, MediumPacer and Spinner.
• Bowler class has bowlingMethod() where all the child classes
are inheriting this method.
• As we all know that a fast bowler will going to bowl
differently as compared to medium pacer and spinner in
terms of bowling speed, long run up and way of bowling, etc.
• Similarly a medium pacer’s implementation
of bowlingMethod() is also going to be different as compared
to other bowlers. And same happens with spinner class.
• All the three classes above inherited
the bowlingMethod() but their implementation is totally
different from one another.
• Polymorphism in Java is of two types:
1. Run time polymorphism
2. Compile time polymorphism

You might also like