1. DISCOVER . LEARN . EMPOWER
UNIVERSITY INSTITUTE OF
COMPUTING
Master of Computer Applications
Winning Camp – Content
MCA (TPP) - 2021 Batch
Logic Building
2. *
*
*
3
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#
3. • 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)
May create many objects from a given class
- Each object will have its own attribute, but will have identical behaviour.
• Class:
• prototype or blueprint from which objects are created
• The class construct provides a template (or blueprint) for the creation of objects.
• Classes specify what attributes and behaviour an object may have.
*
*
*
3
1. Objects & Classes
4. • 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
*
*
*
3
Objects & Classes
5. • In old style programming, you had:
• data, which was completely passive
• functions, which could manipulate any data
• An object contains both data and methods that manipulate that data
• An object is active, not passive; it does things
• An object is responsible for its own data
• But: it can expose that data to other objects
*
*
*
3
An object has behaviors
6. *
*
*
3
An object has state
• An object contains both data and methods that manipulate that data
• The data represent the state of the object
• Data can also describe the relationships between this object and other objects
• Example: A CheckingAccount might have
• A balance (the internal state of the account)
• An owner (some object representing a person)
9. • A Class is a set of variables (to represent its attributes) and functions (to describe its behavior)
that act on its variables
• Object is an instance of a class that holds data (values) in its variables. Data can be accessed by its
functions
• Every object belongs to (is an instance of) a class
• An object may have fields, or variables
• The class describes those fields
• An object may have methods
• The class describes those methods
• A class is like a template, or cookie cutter
• You use the class’s constructor to make objects
*
*
*
3
Classes describe objects
10. • Every class object
- Has its own data members
- Has its own member functions (which are the same as other objects of the
same class have)
- When a member function accesses a data member
By default the function accesses the data member of the object to which it belongs!
- No special notation needed
*
*
*
3
Remember
11. • The Class construct
- Actually allows programmers to define new data types for representing information
- Class type objects can have both attribute and behaviour components
- Provides the object-oriented programming in C++
*
*
*
3
Class Data Types
13. #include <iostream>
using namespace std;
class Circle
{
private:
// The radius of this circle
double radius;
public:
// Construct a default circle object
Circle()
{
radius = 1;
} // Construct a circle object
Circle(double newRadius)
{
radius = newRadius;
}
// Return the area of this circle
double getArea()
{
return radius * radius * 3.14159;
}
}; // Must place a semicolon here
int main()
{
Circle circle1(1.0);
Circle circle2(25);
Circle circle3(125);
cout << "The area of the circle of radius "
" 1.0 is " << circle1.getArea() << endl;
cout << "The area of the circle of radius "
“25 is " << circle2.getArea() << endl;
cout << "The area of the circle of radius "
“125 is " << circle3.getArea() << endl;
return 0;
}
*
*
*
3
Example of class
14. class Employee {
// Fields
private String name; //Can get but not change
private double salary; // Cannot get or set
// Constructor
Employee(String n, double s) {
name = n; salary = s;
}
// Methods
void pay () {
System.out.println("Pay to the order of " + name + " $" + salary);
}
public String getName() { return name; } // getter
}
*
*
*
3
Example of class
15. • instance = object
• field = instance variable
• method = function
• sending a message to an object = calling a function
• These are all approximately true
*
*
*
3
Approximate Terminology
16. *
*
*
3
C++ & Java
• In C++ there may be more than one root
• but not in Java!
• In C++ an object may have more than one parent (immediate superclass)
• but not in Java!
• Java has a single, strict hierarchy
17. • Extracting essential properties and behavior of an entity
• Class represents such an abstraction and is commonly referred to as an abstract data type
• Extract only the relevant properties of a real-world of or developing a class while ignoring the
inessentials
• For any problem, extract the relevant real-world object properties for software objects, while
ignoring inessentials
• Defines a view of the software object
*
*
*
3
2. Abstraction
19. *
*
*
3
Abstraction
Example - car
• Car dealer views a car from selling features standpoint
• Price, length of warranty, color, …
• Mechanic views a car from systems maintenance standpoint
• Size of the oil filter, type of spark plugs, …
20. *
*
*
3
3. Encapsulation and
Information Hiding
• Mechanism by which we combine data and the functions that manipulate the data into one unit
• Objects & Classes enforce encapsulation
• Group the attributes and behavior of an object together in a single data structure known as a class
• Data and functions are said to be encapsulated into a single entity – the class
• Data is concealed within a class, so that it cannot be accessed mistakenly by functions outside the class.
21. *
*
*
3
Encapsulation
• Steps
• Decompose an object into parts
• Hide and protect essential information
• Supply an interface that allows an object to be accessed in a controlled and useful manner
• Interface means that the internal representation of a class can be changed without affecting other system
parts
• Example - Radio
• Interface consists of controls and power and antenna connectors
• The details of how it works is hidden
• To install and use a radio
• Do not need to know anything about the radio’s electronics
22. *
*
*
3
Modularity
• Dividing an object into smaller pieces or “modules” so that the object is easier to understand and
• Most complex systems are modular
• Example - Car can be decomposed into subsystems
1. Cooling system
• Radiator Thermostat Water pump
2. Ignition system
• Battery Starter motor Spark plugs
23. *
*
*
3
Hierarchy
• Hierarchy
• Ranking or ordering of objects based on some relationship between them
• Helps us understand complex systems
• Example - a company hierarchy helps employees understand the company and their positions within it
• For complex systems, a useful way of ordering similar abstractions is a taxonomy
from least general to most general
24. • Classes are arranged in a tree like structure called a hierarchy
• The class at the root is named Object
• Every class, except Object, has a superclass
• A class may have several ancestors, up to Object
• When you define a class, you specify its superclass
• If you don’t specify a superclass, Object is assumed
• Every class may have one or more subclasses
*
*
*
3
Classes form a hierarchy
26. *
*
*
3
4. Inheritance
• The process of creating new classes, called derived classes, from existing classes or base
classes creating a hierarchy of parent classes and child classes.
• The child classes inherit the attributes and behaviour of the parent 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
27. *
*
*
3
A variable can hold subclass objects
• Suppose B is a subclass of A
• A objects can be assigned to A variables
• B objects can be assigned to B variables
• B objects can be assigned to A variables, but
• A objects can not be assigned to B variables
• Every B is also an A but not every A is a B
• You can cast: bVariable = (B) aObject;
• In this case, Java does a runtime check
28. *
*
*
3
Objects inherit from superclasses
• A class describes fields and methods
• Objects of that class have those fields and methods
• But an object also inherits:
• the fields described in the class's superclasses
• the methods described in the class's superclasses
• A class is not a complete description of its objects!
29. *
*
*
3
Example of inheritance
class Person {
String name;
int age;
void birthday () {
age = age + 1;
}
}
class Employee
extends Person {
double salary;
void pay () { ...}
}
Every Employee has name and age fields and birthday method as well as a salary field and a pay method.
32. – Generally, the ability to appear in many forms
– More specifically, in OOP, it is the ability to redefine methods for derived classes
– Ability to process objects differently depending on their data type or class
– Giving different meanings to the same thing
*
*
*
3
5. Polymorphism