0% found this document useful (0 votes)
21 views41 pages

OOPS

Java

Uploaded by

baminiaishwarya
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)
21 views41 pages

OOPS

Java

Uploaded by

baminiaishwarya
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/ 41

Introduction to

Object Oriented Programming

Introduction to OOP
Topics
●Why use OOP?
●Building blocks of OOP

● Classes
● Objects
●What is OOP?
●OOP concepts

● Abstraction
● Encapsulation
● Inheritance
● Polymorphism
●Advantages vs Disadvantages
●Conclusion

Introduction to OOP
Topics
●Why use OOP?
●Building blocks of OOP
● Classes
● Objects
●What is OOP?
●OOP concepts
● Abstraction
● Encapsulation
● Inheritance
● Polymorphism
●Advantages vs Disadvantages
●Conclusion

Introduction to OOP
Why use OOP?

Object Oriented Programming (OOP) is one of the most


widely used programming paradigm

Why is it extensively used?



● Well suited for building trivial and complex applications
● Allows re-use of code thereby increasing productivity
● New features can be easily built into the existing code
● Reduced production cost and maintenance cost

Common programming languages used for OOP include


C++, Java, and C#

Introduction to OOP
Topics
●Why use OOP?
●Building blocks of OOP

● Classes
● Objects
●What is OOP?
●OOP concepts

● Abstraction
● Encapsulation
● Inheritance
● Polymorphism
●Advantages vs Disadvantages
●Conclusion

Introduction to OOP
Building Blocks of OOP: Objects &
Classes
● Object: models a
● Real world object (ex. computer, book, box)
● Concept (ex. meeting, interview)
● Process (ex. sorting a stack of papers or comparing two
computers to measure their performance)

●Class: prototype or blueprint from which objects are


created

Introduction to OOP
Building Blocks of OOP: Objects &
Classes

Class has
● Set of attributes or properties that describes every object
● Set of behavior or actions that every object can perform

Object has

● Set of data (value for each of its attribute)
● Set of actions that it can perform
● An identity

Every object belongs to a class


Introduction to OOP
Real World Example of Objects &
Classes
Object: FordCar1

Attributes Behavior
Color: Yellow Start, Accelerate,
Reverse, Stop
Class: FordCar Type: Coupe
Model: Mustang
Attributes Cylinder: 6
Color, Type, Model, Cylinder
Behavior
Start, Accelerate, Reverse, Stop
Object: FordCar2

Attributes Behavior
Color: Orange Start, Accelerate,
Type: Coupe Reverse, Stop

Model: Focus
Cylinder: 4
Introduction to OOP
Another Real World Example..

Object: Person1

Class: Person Attributes Behavior


Name:Amulya Speak,
Attributes Listen, Eat,
Height: 5’ 4”
Run, Walk
Name, Height, Age Age: 21

Behavior
Speak, Listen, Eat, Run, Walk Object: Person2

Attributes Behavior
Name:Chetan Speak,
Listen, Eat,
Height: 5’ 9”
Run, Walk
Age: 24

Introduction to OOP
Class

●A class is a set of variables (to represent its attributes) and


functions (to describe its behavior) that act on its variables

Introduction to OOP
Class ShippingBox

sender_name : string
receiver_name : string
cost_per_pound : int int shipping_cost() {
weight : int return cost_per_pound*weight;
}

shipping_cost() : int

Introduction to OOP
Object

●Object is an instance of a class that holds data (values) in


its variables. Data can be accessed by its functions

Introduction to OOP
Objects of ShippingBox class

Object BoxA

sender_name = Julie
receiver_name = Jill
cost_per_pound = 2
weight = 5
shipping_cost()

Class
ShippingBox
Object BoxB

sender_name = Jim
receiver_name = John
cost_per_pound = 5
weight = 10
shipping_cost()

Introduction to OOP
Topics
●Why use OOP?
●Building blocks of OOP

● Classes
● Objects
●What is OOP?
●OOP concepts

● Abstraction
● Encapsulation
● Inheritance
● Polymorphism
●Advantages vs Disadvantages
●Conclusion

Introduction to OOP
What is OOP?

●Paradigm for problem solving by interaction among objects


●It follows a natural way of solving problems

● Ex. Ann wants to start her car


(1) Ann walks to her car
(2) Ann sends a message to the car to start by turning on the ignition
(3)The car starts

Introduction to OOP
Problem Solving in OOP

Problem: Ann wants to start her car

Object Ann Object Ann’s car


Name = Ann Color = Yellow
Age = 21 mes Type = Coupe
sag
Speak() e Model = Mustang
Run() Cylinder = 6
Walk() Start()
Accelerate()
Stop()

Introduction to OOP
Topics
●Why use OOP?
●Building blocks of OOP

● Classes
● Objects
●What is OOP?
●OOP concepts

● Abstraction
● Encapsulation
● Inheritance
● Polymorphism
●Advantages vs Disadvantages
●Conclusion

Introduction to OOP
Abstraction

●Extracting essential properties and behavior of an entity


●Class represents such an abstraction and is commonly

referred to as an abstract data type

Introduction to OOP
Topics
●Why use OOP?
●Building blocks of OOP

● Classes
● Objects
●What is OOP?
●OOP concepts

● Abstraction
● Encapsulation
● Inheritance
● Polymorphism
●Advantages vs Disadvantages
●Conclusion

Introduction to OOP
Encapsulation

●Mechanism by which we combine data and the functions


that manipulate the data into one unit
●Objects & Classes enforce encapsulation

Introduction to OOP
Topics
●Why use OOP?
●Building blocks of OOP

● Classes
● Objects
●What is OOP?
●OOP concepts

● Abstraction
● Encapsulation
● Inheritance
● Polymorphism
●Advantages vs Disadvantages
●Conclusion

Introduction to OOP
Inheritance

●Create new classes (derived classes) from existing


classes (base classes)
●The derived class inherits the variables and functions of

the base class and adds additional ones!


●Provides the ability to re-use existing code

Introduction to OOP
Inheritance Example

BankAccount CheckingAccount SavingsAccount

customer_name : string customer_name : string customer_name : string


account_type : string account_type : string account_type : string
balance : int balance : int balance : int
insufficient_funds_fee : int interest_rate : int

deposit() : int deposit() : int deposit() : int


withdrawal() : int withdrawal() : int withdrawal() : int
process_deposit() : int calculate_interest() : int

Introduction to OOP
Inheritance Example

BankAccount
customer_name : string
account_type : string
balance : int

deposit() : int
withdrawal() : int

CheckingAccount SavingsAccount

insufficient_funds_fee : int interest_rate : int


process_deposit() : int calculate_interest() : int

Introduction to OOP
Topics
●Why use OOP?
●Building blocks of OOP

● Classes
● Objects
●What is OOP?
●OOP concepts

● Abstraction
● Encapsulation
● Inheritance
● Polymorphism*
●Advantages vs Disadvantages
●Conclusion

Introduction to OOP
Disadvantages of OOP

●Initial extra effort needed in accurately modeling the


classes and sub-classes for a problem

●Objects in OOP consume more memory. Each object


carries additional overhead for its methods, especially
when dealing with a large number of objects

Not suited for high frequency trading for example where


performance is critical and every microsecond counts

Introduction to OOP
Conclusion

Introduction to OOP
INHERITANCE

●: Which of the following statements about inheritance in Java is true?

● a) Inheritance allows a class to inherit properties and behaviors from multiple classes
simultaneously.
b) Inheritance in Java is denoted by the keyword extends.
●c) Private members of a superclass are accessible to subclasses.
d) Inheritance does not support code reuse and extensibility.

Introduction to OOP
INHERITANCE

●: Which of the following statements about inheritance in Java is true?

● a) Inheritance allows a class to inherit properties and behaviors from multiple classes
simultaneously.
b) Inheritance in Java is denoted by the keyword extends.
●c) Private members of a superclass are accessible to subclasses.
d) Inheritance does not support code reuse and extensibility.

Introduction to OOP
ABSTRACTION
●Which statement about abstract classes in Java is correct?

a) Abstract classes can be instantiated using the new keyword.


b) Abstract classes may contain both abstract and non-abstract methods.
c) Abstract classes must implement all methods defined in their interfaces.
d) Abstract classes cannot have constructors.

Introduction to OOP
ABSTRACTION
●Which statement about abstract classes in Java is correct?

a) Abstract classes can be instantiated using the new keyword.


b) Abstract classes may contain both abstract and non-abstract methods.
c) Abstract classes must implement all methods defined in their interfaces.
d) Abstract classes cannot have constructors.

Introduction to OOP
POLYMORPHISM

●: What is the key difference between method overloading and method


overriding?

●a) Method overloading allows a subclass to redefine a method from its


superclass.
●b) Method overriding occurs when a class has multiple methods with the same
name but different signatures.
●c) Method overloading occurs when a subclass provides a specific
implementation of a method from its superclass.
●d) Method overriding allows a subclass to provide a different implementation of

a method defined in its superclass.

Introduction to OOP
POLYMORPHISM

●: What is the key difference between method overloading and method


overriding?

●a) Method overloading allows a subclass to redefine a method from its


superclass.
●b) Method overriding occurs when a class has multiple methods with the same
name but different signatures.
●c) Method overloading occurs when a subclass provides a specific
implementation of a method from its superclass.
●d) Method overriding allows a subclass to provide a different implementation of

a method defined in its superclass.

Introduction to OOP
INHERITANCE

●In Java, a subclass ____________.

●a) Inherits all properties and methods from its superclass


● b) Can override private methods from its superclass
●c) Can access private members of its superclass directly
● d) Cannot have methods with the same signature as its superclass

Introduction to OOP
INHERITANCE

●In Java, a subclass ____________.

●a) Inherits all properties and methods from its superclass


● b) Can override private methods from its superclass
●c) Can access private members of its superclass directly
● d) Cannot have methods with the same signature as its superclass

Introduction to OOP
POLYMORPHISM

●Which type of polymorphism is resolved at compile-time?

● a) Static polymorphism
● b) Dynamic polymorphism
● c) Compile-time polymorphism
● d) Run-time polymorphism

Introduction to OOP
POLYMORPHISM

●Which type of polymorphism is resolved at compile-time?

● a) Static polymorphism
● b) Dynamic polymorphism
● c) Compile-time polymorphism
● d) Run-time polymorphism

Introduction to OOP
INHERITANCE

●In Java, a subclass ____________.

●a) Inherits all properties and methods from its superclass


● b) Can override private methods from its superclass
●c) Can access private members of its superclass directly
● d) Cannot have methods with the same signature as its superclass

Introduction to OOP
INHERITANCE

●In Java, a subclass ____________.

●a) Inherits all properties and methods from its superclass


● b) Can override private methods from its superclass
●c) Can access private members of its superclass directly
● d) Cannot have methods with the same signature as its superclass

Introduction to OOP
Thank you!

Introduction to OOP

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy