[go: up one dir, main page]

0% found this document useful (0 votes)
80 views55 pages

Final - SEIT - UNIT 1 - OOP

OOP- UNIT NO. 1 PPT(ENGINEERING 3rd SEM)

Uploaded by

Vinay Bansode
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)
80 views55 pages

Final - SEIT - UNIT 1 - OOP

OOP- UNIT NO. 1 PPT(ENGINEERING 3rd SEM)

Uploaded by

Vinay Bansode
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/ 55

Object Oriented Programming[214444]

Second Year Information Technology (2019 Course)


Unit No 01: Foundation of Object Oriented Programming

Syllabus:-
Introduction OOP : Software Evolution, Introduction to Procedural, Modular, Object-Oriented
and Generic Programming Techniques, Limitations of Procedural Programming, Need of
Object-Oriented Programming, Fundamentals of Object-Oriented Programming: Objects,
Classes, Data Members, Methods, Messages, Data Encapsulation, Data Abstraction and
Information Hiding, Inheritance, Polymorphism, Static and Dynamic Binding, Message
Passing.
Unit Objectives
On completion of the unit students will be able to:
1. To understand different programming technique
2.To understand limitation of procedure programming and need of oop
3.To understand fundamentals of object oriented programming
Unit Outcomes:
1. To learn need of object oriented programming and its foundation .

Texts Books:
T1) An Introduction to Object Oriented Programming (3rd Ed), by Timothy A. Budd, published by Addison- Wesley,2002
T2) E. Balaguruswamy, “Object Oriented Programming Using C++ and Java”, Tata McGrawHill

Outcome Satisfied: PEOs: 1 PSOs:2 POs: a,c,e COs:1


Outline
Sr. Topic Name Reference Book
No.
1 Software Evaluation, Introduction to T2(page no:3-9)

Procedural, Modular, Object- Oriented and


Generic Programming Techniques,
2 Limitations of Procedural Programming, Need OOP in C++ by E-
Balaguruswamy (page no:11-
of Object-Oriented Programming 18)

3 Fundamentals of Object-Oriented Programming: T1(page no:74-96)

What is an Objects? What is Class? Examples

4 Data Encapsulation, Data Abstraction and T1(page no:25-30)

Information Hiding,
5 Inheritance, Polymorphism, T2(page no:188-216)

6 Static and Dynamic Binding, Message Passing. T1(page no:101-112)


Software Evolution
Software Evolution

Software Evolution is a term which refers to the process of developing software


initially, then timely updating it for various reasons, i.e., to add new features or
to remove obsolete functionalities etc.

The evolution process includes fundamental activities of change analysis,


release planning, system update and releasing a system to customers.

The cost an impact of these changes are accessed to see how much system is
affected by the change and how much it might cost to implement the change.

If the proposed changes are accepted, a new release of the software system is
planned.

During release planning, all the proposed changes (fault repair, adaptation, and
new functionality) are considered.
Software Evolution
What is Programming?

• Like any human language, a programming language provides a way to


express concepts.

• Computer programming is the process of writing instructions that get


executed by computers

• The instructions, also known as code, are written in a programming


language which the computer can understand and use to perform a task or
solve a problem.

• Programming Language Family


– Machine Language➔ 0 & 1 form
– Assembly Language➔ Mnemonic
– High Level Language ➔Procedural ,OOP, Modular, Functional etc..
Real World Example
Let's say that you are working for a vehicle parts manufacturer that needs to update
it's online inventory system.

Your boss tells you to program two similar but separate forms for a website, one form
that processes information about cars and one that does the same for trucks.

Cars: we will need to record the following information: Color, Engine Size,
Transmission Type, Number of doors

Truck: Color, Engine Size, Transmission Type, Cab Size, Towing Capacity.
Scenario 01

Suppose that we suddenly need to add a bus form, that records the following
information:

Bus: Color, Engine Size, Transmission Type, Number of passengers

Procedural: We need to recreate the entire form, repeating the code for Color,
Engine Size, and Transmission Type.

OOP: We simply extend the vehicle class with a bus class and add the method,
numberOfPassengers.
Scenario 02

Instead of storing color in a database like we previously did, for some strange
reason our client wants the color emailed to him.

Procedural: We change three different forms: cars, trucks, and buses to email the
color to the client rather than storing it in the database.

OOP: We change the color method in the vehicle class and because the car, truck,
and bus classes all extend (or inherit from, to put it another way) the vehicle class,
they are automatically updated.
Procedure Programming

• Procedural programs: consist of a series of steps or procedures that take


place one after the other

• Procedural Languages:
– COBOL
– BASIC
– FORTRAN
– C

Fig: Structure of the procedure oriented program


Procedure Programming

• Problem is viewed into sequence of things to be done

• Consist of writing list of instructions for computer to follow, and


organizing these instructions into group known as functions.

• Emphasis is on doing things i.e. procedures.

• Most of the function share global data

• Function transform data from one form to another.

• Employs top-down approach in program design.


Modular Programming

• Emphasizes separating the functionality of program into independents

• Concerns are separated such that modules perform logically discrete


functions.

• The desired module goal is to have no interaction between modules.

• Each module works independently to another module


Object Oriented Programming
• It allows decomposition of problem into a number of entities called objects, and
then builds data and functions around these objects

• Emphasis is on data rather than procedure.

• Objects have attributes and can take actions

• You can pass messages to objects, so that they take action. The same message
works differently when applied to the various objects

• A method can work appropriately with different types of data

• Objects can inherit traits of previously created objects

• Information hiding is more complete than in procedural programs

• New data and functions can be added easily whenever necessary. Follows
bottom-up approach
Object Oriented Programming
Generic Programming
• Generic programming is about generalizing software components.

• Algorithms are written in terms of to-be-specified-later.

• This is programming paradigm for developing efficient and reusable


software libraries.

• Tasks of generic programming


– Categorize abstraction in a domain into concepts
– Implement generic algorithm based on concepts
– Build concrete model for concepts

• In C++ class and function templates are particularly effective mechanisms.


Limitation of Procedure Oriented Programming
Need of Object Oriented Programming
1) OOP provides a clear modular structure for programs

2) OOP makes it easy to maintain and modify existing code

3) Code Reusability

4) It is suitable for real word problems and real world works

5) OOP provides a good framework for code libraries where supplied


software components can be easily adapted and modified by the
programmer.
Fundamentals of OOP
• Objects

• Classes

• Data abstraction and Encapsulation

• Inheritance

• Polymorphism

• Dynamic Binding

• Message Passing
Objects

• What is an Object?

• Many real world objects have both state (Characteristics that can
change) and abilities(Things that they can do).

• Real-world objects=state(properties)+abilities(Behavior)

• Programming objects=Data+Functions

• The match between programming objects and real world objects


is the result of combining data and member functions.
Objects
• What is an Object?

– Objects are basic run time entities. They may represent a


person, a place, a bank account, or any item that the
program has to handle.

– Each object take up space in the memory to store data and


code.

– Objects Interact with each other by sending messages to each


other.
– OOP encapsulates data packages (attributes) and functions
(behavior) into Objects have the property of objects called
Information hiding.
Representing Object

• Program objects should be chosen such that they match closely


with the real world objects.

OBJECTS:STUDENT

DATA
Name
Date-of-birth
Marks

FUNCTIONS
Total
Average
Display
..............
Classes & Objects

• Class is a new data type which is used to define objects.

• A class is a template or blue print.

• It specifies what data and what functions will be included in


objects of that class.

• A class is description of similar objects

• Objects are instances of classes.

• Classes are user defined data type

• Example: Fruit Mango;


• Will create an object Mango belonging to the class Fruit
Classes & Objects
• What is a Class?
– A class is a blueprint or prototype that defines the variables
and the methods (functions) common to all objects of a
certain kind.

• Objects (data and functions) are members of user-defined types


called classes.

• A class definition is an extension of a C structure definition. It is


made up of declarations of variables and of function prototypes
to manipulate those variables. The variables are typically
declared to be private or protected and the functions are
typically declared to be public.

• Structures and classes actually are the same in C++ (both can
have member functions or “methods”), but structures members
are public by default and class members are private by default.
Example: Classes & Objects

object class

class Student
char name
int rollNo
setName()
setRollNo()
Rani Rima Rita Bina calcMarks()
R001 R002 R003 R004
Example: Classes & Objects
Example
Example

class Book
{
string title;
string author;
int pubDate;
double price;

void displayInfo()
{....}
}
Data Members & Methods

class Book
{
//data members
string title;
string author;
int pubDate;
double price;

//Methods or member function


void displayInfo()
{....}
}
Messages

• Objects of the program interact by sending messages to each other


Example in c++
using namespace std;

#include<iostream>

class addition
{
int a,b,c; //Private Data Members
public:
void read(); //Public member functions
void add();
};

void addition :: read()


{
cout<<"\nEnter the two numbers\n";
cin>>a>>b;
}
void addition :: add()
{
c=a+b;
cout<<"\nThe addition is="<<c<<"\n";
}
int main()
{
addition p,s;
p.read();
p.add();
s.read();
s.add();
}
Example in java
Object and Class Example: main within the class

//Java Program to illustrate how to define a class and fields


//Defining a Student class.
class Student{
//defining fields
int id;//field or data member or instance variable
String name;

//creating main method inside the Student class


public static void main(String args[]){
//Creating an object or instance
Student s1=new Student();//creating an object of Student
//Printing values of the object
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name);
}
} Output:
0
null
Example in java
Object and Class Example: main outside the class
//Java Program to demonstrate having the main method in another class
//Creating Student class.
class Student{
int id;
String name;
}
//Creating another class TestStudent1 which contains the main method
class TestStudent1{
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}

Output:
0
null
Example in java
Object and Class Example: Initialization through reference variable
class Student{
int id;
String name;
}
class TestStudent3{
public static void main(String args[]){
//Creating objects
Student s1=new Student();
Student s2=new Student();
//Initializing objects
s1.id=101;
s1.name="Sonoo";
s2.id=102;
s2.name="Amit";
//Printing data
System.out.println(s1.id+" "+s1.name);
System.out.println(s2.id+" "+s2.name);
} Output:
} 101 Sonoo
102 Amit
Example in java
Object and Class Example: Initialization through method
class Student{
int rollno;
String name;
void insertRecord(int r, String n){
rollno=r;
name=n;
}
void displayInformation(){System.out.println(rollno+" "+name);}
}
class TestStudent4{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
} Output:
} 111 Karan
222 Aryan
Data Abstraction & Encapsulation
• 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.

• You 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.You can
achieve 0-100% abstraction using abstract class.
Data Abstraction & Encapsulation
• 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.
• You can achieve 100% abstraction using interfaces.
• Features of Interface:-
• 1. data member/variable declared inside by default Static and Final.
• 2. methods declared inside interface by default public and abstract.
• 3. constructor concept not present in interface.
Data Abstraction & Encapsulation
• 4. object of interface can’t be creaed.
• 5. interface support multiple inheritance.
• 6. to create object of interface programmer need to make use of
“Implement” class.

• Let us understand interfaces better by taking an example of a ‘ParentCar’


interface with its related methods.
public interface ParentCar {
public void changeGear( int newValue);
public void speedUp(int increment);
public void applyBrakes(int decrement);
}
• These methods need be present for every car, right? But their working is
going to be different.
Data Abstraction & Encapsulation
• Encapsulation:
– The process of binding code and data together in the form of a capsule
– Data is not accessible to the outside world, only those function which are
wrapped in the class can access it.
– Since the classes use the concept of data abstraction, they are known as
Abstract Data Type(ADT).
• 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.
Data Abstraction & Encapsulation
• 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.
•Let us 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) {
}
}
Inheritance

• Inheritance:
– The feature by which one class acquires the properties and
functionalities of another class
• Classes are extensible
• You can create new classes that extend or are descendents of existing
classes
• The descendent classes can inherit all the attributes of the parent class,
Inheritance

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.

• Types of Inheritance
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
Class A {
---
}
Class B extends A {
---
}
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 Class
A{
---
}
Class B extends A {
---
}
Class C extends B {
---
}
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
Class A {
---
}
Class B extends A {
---
}
Class C extends A {
---
}
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
Polymorphism
• Polymorphism is a Greek term , means ability to take more than one form.
• 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.
• Object-oriented programs use polymorphism to carry out the same operation
in a manner customized to the object.
Polymorphism
• Polymorphism in Java is of two types:
– Run time polymorphism
– Compile time polymorphism
The runtime polymorphism refers to a process in which a call to an
overridden method is resolved at runtime rather than at compile-time.
• Method overriding is an example of run time polymorphism.
public Class Bowler{
void bowlingMethod(){ System.out.println("
bowler ");
}
}
public Class FastPacer extends Bowler{ void
bowlingMethod(){
System.out.println(" fast bowler ");
}
Public static void main(String[] args){ FastPacer
obj= new FastPacer();
obj.bowlingMethod();
}
}
Polymorphism
• The compile time polymorphism refers to a process in which a call to an
overloaded method is resolved at compile time rather than at run time.

• Method overloading is an example of compile time polymorphism.

• Method Overloading is a feature that allows a class to have two or more


methods having the same name but the arguments passed to the methods are
different.

• Unlike method overriding, arguments can differ in:


– Number of parameters passed to a method
– Datatype of parameters
– Sequence of datatypes when passed to a method.
Polymorphism

class Adder {
int add(int a, int b){
return a+b;
}
double add( double a, double b){
return a+b;
}
public static void main(String args[]){
Adder obj=new Adder();
System.out.println(obj.add(11,11));
System.out.println(obj.add(12.3,12.6));
}
}
Static & Dynamic Binding
• Binding refers to linking of procedure call to the code to be executed in response to
the call.
• Static Binding
– Also known as Early Binding, it is a binding that is resolved by the compiler
at compile time.
– The code is executed in response to function call is decided at compile time.
– It is the code associated with a given procedure call is known at the compile
time.
– E.g. Method overloading
• Dynamic Binding
– It is the code associated with a given procedure call is not known until the
time of the call at the run time.
– The code is executed in response to function call is decided at run time.
– E.g. Method overriding
Static Vs Dynamic Binding
• The Static binding is also known as Early binding while Dynamic binding is
also known as late binding.

• In static binding call is resolved at compile time while in dynamic binding call
is resolved at run time.

• While static binding uses Type information, dynamic binding makes use
of Objects for binding.

• The static binding is used by private, static and final members. For the
virtual methods in Java, the binding is done at the run time in accordance
with the run time object.

• Static binding supports Overloading, while Dynamic Binding supports


Overriding.
Message Passing
• The Objects use the message passing to interact with each other.

• Objects communicate with one another by sending and receiving information to


each other.

• A message for an object is a request for execution of a procedure and therefore


will invoke a function in the receiving object that generates the desired results.

• Message passing involves specifying the name of the object, the name of the
function and the information to be sent.

• E.g.
• x.get(10)

You might also like