Final - SEIT - UNIT 1 - OOP
Final - SEIT - UNIT 1 - OOP
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
Information Hiding,
5 Inheritance, Polymorphism, T2(page no:188-216)
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?
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:
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 Languages:
– COBOL
– BASIC
– FORTRAN
– C
• You can pass messages to objects, so that they take action. The same message
works differently when applied to the various objects
• 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.
3) Code Reusability
• Classes
• 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
OBJECTS:STUDENT
DATA
Name
Date-of-birth
Marks
FUNCTIONS
Total
Average
Display
..............
Classes & Objects
• 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;
#include<iostream>
class addition
{
int a,b,c; //Private Data Members
public:
void read(); //Public member functions
void add();
};
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.
• 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
• 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.
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.
• 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)