0% found this document useful (0 votes)
27 views74 pages

OOPS Concepts

The document provides information on various object-oriented programming (OOP) concepts like encapsulation, association, aggregation, composition, abstraction, inheritance, polymorphism, interfaces, and abstract classes. It includes definitions and examples to explain each concept. Key differences between association, aggregation, and composition relationships are also discussed.

Uploaded by

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

OOPS Concepts

The document provides information on various object-oriented programming (OOP) concepts like encapsulation, association, aggregation, composition, abstraction, inheritance, polymorphism, interfaces, and abstract classes. It includes definitions and examples to explain each concept. Key differences between association, aggregation, and composition relationships are also discussed.

Uploaded by

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

Amphisoft Technologies

OOPS Concepts
OOPS Concepts
➢ Encapsulation
➢ Association
➢ Aggregation
➢ Composition

Association, aggregation and composition (difference)
➢ Abstraction
➢ Abstract class

Generalization
➢ Inheritence
➢ Polymorphism - Method overloading and overriding, operator overloading

Interface
➢ Interfaces and classes

Abstract Class vs Interface
OOPS Concepts
➢ Encapsulation
➢ Association
➢ Aggregation
➢ Composition

Association, aggregation and composition (difference)
➢ Abstraction
➢ Abstract class

Generalization
➢ Inheritence
➢ Polymorphism

Interface
➢ Interface and classes

Abstract Class vs Interface
Encapsulation
Encapsulation

Encapsulation - or Information Hiding

Encapsulation - process of combining data and functions into a single
unit called class

Encapsulation - Data is only accessible through the functions present
inside the class

Encapsulation - most often achieved through information hiding

Information Hiding - structure of an object is hidden, as well as
implementation of its methods
Encapsulation

Each class has two parts

An Interface

An Implementation

Interface - implementation code for
the external Interface

Implementation - code that does all
the operations, makes the interface
look as if it is doing something
Encapsulation -Example (1/6)
Encapsulation -Example (2/6)
Encapsulation -Example (3/6)
Encapsulation -Example (4/6)
Encapsulation -Example (5/6)
Encapsulation -Example (6/6)
Benefits of Encapsulation

Improves maintainability and flexibility and re-usability

The fields can be made read-only or write-only

User would not be knowing what is going on behind the scene,
They would only be knowing that to update a field call set method
and to read a field call get method but what these set and get
methods are doing is purely hidden from them
OOPS Concepts
➢ Encapsulation
➢ Association
➢ Aggregation
➢ Composition

Association, aggregation and composition (difference)
➢ Abstraction
➢ Abstract class

Generalization
➢ Inheritence
➢ Polymorphism

Interface
➢ Interface and classes

Abstract Class vs Interface
Association
Association

Association - Relationship between two Objects

Association - Objects have independent lifecycles

Association - There is no owner

Association - Objects have independent lifecycles
Types of Association

One to One association


One to Many association

PRODUCT VENDOR

Many to One association

INSTRUCTOR SKILL

Many to Many association
Association

SwipeCard class uses the Manager class and the Manager class uses the
SwipeCard class

we can create objects of the Manager class and SwipeCard class
independently and they can have their own object life time
OOPS Concepts
➢ Encapsulation
➢ Association
➢ Aggregation
➢ Composition

Association, aggregation and composition (difference)
➢ Abstraction
➢ Abstract class

Generalization
➢ Inheritence
➢ Polymorphism

Interface
➢ Interface and classes

Abstract Class vs Interface
Aggregation

Aggregation - Special case of association

Aggregation - also called a “Has-a” relationship

Aggregation - directional association between objects

Aggregation - Object have independent life-cycles

Aggregation - Parent-Child relationship
Aggregation

The Manager object will own Worker objects

The child Worker objects can not belong to any other object
Aggregation

A Worker object cannot belong to a SwipeCard object

But the Worker object can have its own life time which is completely
disconnected from the Manager object

Looking from a different perspective, it means that if the Manager
object is deleted, the Worker object does not die
OOPS Concepts
➢ Encapsulation
➢ Association
➢ Aggregation
➢ Composition

Association, aggregation and composition (difference)
➢ Abstraction
➢ Abstract class

Generalization
➢ Inheritence
➢ Polymorphism

Interface
➢ Interface and classes

Abstract Class vs Interface
Composition

Composition - Special case of aggregation

Composition - Strong Type of Aggregation

Composition - When an object contains the other object, if the
contained object cannot exist without the existence of container
object
Composition

Manager and the project objects are dependent on each other

The lifetimes of both the objects are the same. In other words, the
project will not be successful if the manager is not good, and the manager
will not get good increments if the project has issues
Composition

Both objects are heavily dependent on each other

In other words, if one goes for garbage collection the other also has to be
garbage collected, or putting from a different perspective, the lifetime of
the objects are the same

That’s why the composition is called “Death” relationship
OOPS Concepts
➢ Encapsulation
➢ Association
➢ Aggregation
➢ Composition

Association, aggregation and composition (difference)
➢ Abstraction
➢ Abstract class

Generalization
➢ Inheritence
➢ Polymorphism

Interface
➢ Interface and classes

Abstract Class vs Interface
Association->Aggregation-
>Composition(Difference)
Association->Aggregation-
>Composition(Difference)

Aggregation is a special kind of an association

Composition is a special kind of an aggregation
OOPS Concepts
➢ Encapsulation
➢ Association
➢ Aggregation
➢ Composition

Association, aggregation and composition (difference)
➢ Abstraction
➢ Abstract class

Generalization
➢ Inheritence
➢ Polymorphism

Interface
➢ Interface and classes

Abstract Class vs Interface
Abstraction
Abstraction

Abstraction - Abstraction means ignoring irrelevant features, properties,
or functions and emphasizing the relevant ones

Abstraction = managing complexity

"Relevant" to what?
Abstraction

Abstraction is something we do every day

Looking at an object, we see those things about it that have meaning to
us

We abstract the properties of the object, and keep only what we need

E.g. students get "name" but not "color of eyes"

Allows us to represent a complex reality in terms of a simplified model

Abstraction highlights the properties of an entity that we need and hides
the others
OOPS Concepts
➢ Encapsulation
➢ Association
➢ Aggregation
➢ Composition

Association, aggregation and composition (difference)
➢ Abstraction
➢ Abstract class

Generalization
➢ Inheritence
➢ Polymorphism

Interface
➢ Interface and classes

Abstract Class vs Interface
Abstract Class

A class which can not be instantiated is known
as abstract class

Not allowed to create object of Abstract class

An abstract class has no use until unless it is
extended by some other class

If you declare an abstract method in a class then
you must declare the class abstract as well. you
can’t have abstract method in a non-abstract
class. It’s vice versa is not always true

If a class is not having any abstract method then
also it can be marked as abstract

Abstract class can have non-abstract method as
well
Abstract class declaration


Abstract Method


Abstract method has no body


Always end the declaration with a semicolon(;)


It must be overridden, An abstract class must be extended and in a same
way abstract method must be overridden


Abstract method must be in a abstract class
Abstract class and Method - Example

Output
Benefits of Abstract Class

Group several related classes together as siblings

Grouping classes together is important in keeping a program
organized and understandable

An Abstract class is a way to organize inheritance, sometimes it
makes no since to implement every method in a class

Abstract classes are templates for future specific classes
OOPS Concepts
➢ Encapsulation
➢ Association
➢ Aggregation
➢ Composition

Association, aggregation and composition (difference)
➢ Abstraction
➢ Abstract class

Generalization
➢ Inheritence
➢ Polymorphism

Interface
➢ Interface and classes

Abstract Class vs Interface
Generalization

Generalization - a bottom approach in which two lower level
entites combine to form a higher level entity

The higher level entity can also combine with lower level entity
to make further higher level entity
Generalization

Bird generalizes from Parrot and Sparrow

Generalization is the process of extracting common features from
two or more classes, and combining them into a generalized super class

Common features can be attributes, associations, or methods

Generalization can relate to the “extends” keyword
Generalization - Example
Bird.java Parrot.java

Sparrow.java
OOPS Concepts
➢ Encapsulation
➢ Association
➢ Aggregation
➢ Composition

Association, aggregation and composition (difference)
➢ Abstraction
➢ Abstract class

Generalization
➢ Inheritence
➢ Polymorphism

Interface
➢ Interface and classes

Abstract Class vs Interface
Inheritance
Inheritance

Inheritance - allows reuse of code without modifying the
original code

Inheritance - allows flexibility to programmer to make
modifications to the program without altering the original code

Base class - create a new class that inherits the properties
from another class

Derived class - class derives or inherits, from the base class
Inheritance Example (1/6)
Inheritance Example (2/6)
Inheritance Example (3/6)
Inheritance Example (4/6)
Inheritance Example (5/6)
Inheritance Example (6/6)
Types of Inheritance (1/2)
Types of Inheritance (2/2)

Based on Class

Single Inheritance - one derived class inherits from one base class

Multilevel Inheritance - hierarchy wherein subclass acts as a base
class for other classes

Hierarchical Inheritance - hierarchy wherein multiple subclasses
inherit from one base class

Based on Interface

Multiple Inheritance - Multiple inheritance is not supported in
java in case of class because of to remove ambiguity, provide more
maintainable and clear design

Hybrid Inheritance - hierarchy that reflects any legal combination
of other four types of inheritance
Benefits of Inheritance

Reusability - facility to use public methods of base class without
rewriting the same

Extensibility - extending the base class logic as per business logic of
the derived class

Data hiding - base class can decide to keep some data private so that it
cannot be altered by the derived class

Overriding - With inheritance, we will be able to override the methods
of the base class so that meaningful implementation of the base class
method can be designed in the derived class.
OOPS Concepts
➢ Encapsulation
➢ Association
➢ Aggregation
➢ Composition

Association, aggregation and composition (difference)
➢ Abstraction
➢ Abstract class

Generalization
➢ Inheritence
➢ Polymorphism

Interface
➢ Interface and classes

Abstract Class vs Interface
Polymorphism
Polymorphism

Polymorphism - "Poly" means "many" and "morph" means "form"

Polymorphism - means that some code or operations or objects behave
differently in different contexts

Polymorphism – reusable code by programmers

Polymorphism enables us to "program in the general" rather than
"program in the specific"

Two Types of Polymorphism

Polymorphism

CompileCompile
Time Polymorphism
Time Polymorphism
(Static) RunTime Polymorphism (Dynamic)
Compile Time Polymorphism


Compile time polymorphism is nothing but the Method Overloading
in java

Method Overloading - can say that a class can have more than one
methods with same name but with different number of arguments or
different types of arguments or both
Compile Time Polymorphism- Example

OUTPUT
No parameters
a: 10
a and b: 10 20
double a:303.23
Result of ob.Method(123.2):
91948.43290000001
RunTime Polymorphism

Runtime polymorphism or Method Overriding or Dynamic Method
Dispatch is a process in which a call to an overridden method is
resolved at runtime rather than compile-time

Example - Consider a scenario, Bank is a class that provides method to
get the rate of interest. But, rate of interest may differ according to
banks. For example, SBI, ICICI and AXIS banks could provide 8%,
7% and 9% rate of interest
RunTime Polymorphism - Example
Benefits of Polymorphism

Simplicity

If you need to write code that deals with a family of types, the code
can ignore type-specific details and just interact with the base type
of the family

Even though the code thinks it is using an object of the base class,
the object's class could actually be the base class or any one of its
subclasses

This makes your code easier for you to write and easier for others to
understand

Extensibility

Other subclasses could be added later to the family of types, and
objects of those new subclasses would also work with the existing
code
OOPS Concepts
➢ Encapsulation
➢ Association
➢ Aggregation
➢ Composition

Association, aggregation and composition (difference)
➢ Abstraction
➢ Abstract class

Generalization
➢ Inheritence
➢ Polymorphism

Interface
➢ Interface and classes

Abstract Class vs Interface
Interface
Why Interface?
Class A
Class A Class B Class C

Class ABC


Class ABC inherits all variables and methods from Class A, Class B, and
Class C

Java does NOT support multiple inheritances

So the interface to implement the functionality of multiple inheritance
Interface

Interface defines a standard and public way of specifying the behavior
of classes

Defines a contract

All methods of an interface are abstract methods

Defines the signatures of a set of methods, without the body
(implementation of the methods)

A concrete class must implement the interface (all the abstract methods
of the Interface)

Interface allows classes, regardless of their locations in the class
hierarchy, to implement common behaviors

Interface also represents IS-A relationship

Interface cannot be instantiated just like abstract class
Interface
Defining Interface
public interface [InterfaceName]
{
//some methods without the body
}

Implementing Interface

class ClassName implements InterfaceName


[, InterfaceName2, …]
{
// Body of Class
}
OOPS Concepts
➢ Encapsulation
➢ Association
➢ Aggregation
➢ Composition

Association, aggregation and composition (difference)
➢ Abstraction
➢ Abstract class

Generalization
➢ Inheritence
➢ Polymorphism

Interface
➢ Interface and classes

Abstract Class vs Interface
Interface and classes

A class extends another class, an interface extends another interface
but a class implements an interface
Simple Interface

Printable interface have only one method, its implementation is
provided in the A class
Interface - Example

Printable

extends
Showable

implements
Class A
OOPS Concepts
➢ Encapsulation
➢ Association
➢ Aggregation
➢ Composition

Association, aggregation and composition (difference)
➢ Abstraction
➢ Abstract class

Generalization
➢ Inheritence
➢ Polymorphism

Interface
➢ Interface and classes

Abstract Class vs Interface
Abstract class vs Interfaces

You might also like