CHAPTER 1
Introduction to Object Oriented Programming
• Overview of user defined data type(union and struct
• What is meant by object(class object)
• What is object oriented programming
• Comparing procedural and object oriented programming
• Class and object
• What is encapsulation
• Data hiding
• (Inheritance and polymorphism) in the next chapter
• Java program structure and key words used in the program
Prepared by Eng A4K 1
Over view of user defined data type in c++
union and structure
Union:
• ‘Union’ is a key word used to define a unified (single)name for a set of different
data type. Where member data variables can be accessed by the dot operator.
syntax:
union Name{ //list of variables}
• The advantage is to ease program writing
Struct data:
• Struct key word is used to define the blue print of the data structure
• It allows many things hence it is powerful than union.
• It allows pointer as data member so we call struct data as struct object.
• Similar to union elements struct object accessed by the dot operator.
syntax:
struct Name{//list of varaibles}
• The advantage is create a form of data structure
Prepared by Eng A4K 2
<cont’d>
• The major difference between a structure and a union is storage. In a structure, each
member has its distinct storage location while the members of a union utilize a
shared memory location that is equal to the size of its largest data member
struct employee{ union employee{
int id; int id;
string name; string name;
string department; string department;
string email; string email;
}; };
Here each data
members will consume This will consume
distinctly allocated shared memory of the
memory size string size
Prepared by Eng A4K 3
Example: union
#include<iostream>
using namespace std;
union Ddata{
int x;
double y;
};
int main(){
Ddata R; // declaration of user defined data type
R.x=9;
R.y=1.0002;
cout<<R.x; // accessing member elements dot perator is used
cout<<endl<<R.y;
return 0;
}
Prepared by Eng A4K 4
Example: Struct
#include<iostream>
using namespace std;
struct Ddata{
int x=9;
double y=100;
};
int main(){
Ddata R; // declaration of user defined data type
cout<<R.x; // accessing member elements dot operator is used
cout<<endl<<R.y;
return 0;
}
Prepared by Eng A4K 5
What is an object?
• Any entity that has state and behavior is known as an object. For example, a chair,
pen, table, keyboard, bike, etc. It can be physical or logical.
• Everything in Java is associated with classes and objects, along with its attributes
and methods. For example: in real life, a car is an object.
• The car has attributes, such as weight and color, and methods, such as drive and
brake.
• A Class is like an object constructor, or a "blueprint" for creating objects
• An Object can be defined as an instance of a class. An object contains an address
and takes up some space in memory.
• Objects can communicate without knowing the details of each other's data or
code.
• The only necessary thing is the type of message accepted and the type of response
returned by the objects.
Example: A dog is an object because it has states like color, name, breed, etc. as well
as behaviors like wagging the tail, barking, eating, etc.
Prepared by Eng A4K 6
Create an object
First the blue print is created by using the class key word
class employee {
private
int id;
String name;
String department;
String email;
public
void displayinfo(){ System.out.println(“ID:”+id)
System.out.println(“Name:”+name)
System.out.println(“Dep’t:”+department)
System.out.println(“email:”+email)
}
};
Prepared by Eng A4K 7
<cont’d>
Types of access modifier
Modifier Description
declarations are visible only within the package
Default
(package private)
Private declarations are visible within the class only
declarations are visible within the package or all
Protected
subclasses
Public declarations are visible everywhere
Prepared by Eng A4K 8
<cont’d>
A typical Java program creates many objects, which as you know, interact by invoking
methods. An object consists of:
State : It is represented by attributes of an object. It also reflects the properties of an
object.
Behavior : It is represented by methods of an object. It also reflects the response of an
object with other objects.
Identity : It gives a unique name to an object and enables one object to interact with
other objects.
Method: A method is a collection of statements that perform some specific task and
return result to the caller. A method can perform some specific task without returning
anything. Methods allow us to reuse the code without retyping the code. In Java,
every method must be part of some class which is different from languages like C, C++
and Python.
Methods are time savers and help us to reuse the code without retyping the code.
Prepared by Eng A4K 9
Concept of OOP
There arc two common programming approach:
• Procedural programming and
• Object-oriented programming(or OOP)
• Procedural programming is a programming practice centered on the procedures or
actions that take place in a program.
• Object-oriented programming is centered around the object.
• Objects are created from abstract data types that encapsulate data and functions
together.
• In procedural programming the data and the functions are separate entities.
• When programs become larger and more complex, the
• separation of a program's data and the code that operates on the data can lead to
problems.
• When the structure of the data changes, the code that operates on the data must
also change to accept the new format.
• This results in additional work for programmers and a greater opportunity for bugs
to appear in the code.
• This problem has helped influence the shift from procedural programming to object-
oriented programming. Prepared by Eng A4K 10
<cont’d>
• Simula is considered the first object-oriented programming language. The
programming paradigm where everything is represented as an object is known as a
truly object-oriented programming language.
• Smalltalk is considered the first truly object-oriented programming language.
• The popular object-oriented languages are Java, C#, PHP, Python, C++, etc.
• The main aim of object-oriented programming is to implement real-world entities,
for example, object, classes, abstraction, inheritance, polymorphism, etc.
• OOP addresses the problems that can result from the separation of code and
data through encapsulation and data hiding.
• Encapsulation refers to the combining of data and code into a single object.
• Data hiding refers to an object's ability to hide its data from code that is outside the
object.
Prepared by Eng A4K 11
Basic Concept
• Abstraction
• Encapsulation or information hiding
• Inheritance and
• Polymorphism
Abstraction
Abstraction refers to the act of representing essential features without including the
background details or explanations.
Example:
• We can driving a car with out knowing how each system work
• We can use object without knowing its internal implementation.
Encapsulation
Refers to wrapping up of data and methods into a single unit (called class)
Data is not accessible to the outside world and only those methods in the class can access it.
Insulation of the data from direct access by a program is called data hiding.
Prepared by Eng A4K 12
<cont’d>
• Another way to think about encapsulation is, it is a protective shield that prevents
the data from being accessed by the code outside this shield.
• Technically in encapsulation, the variables or data of a class is hidden from any
other class and can be accessed only through any member function of own class in
which they are declared.
• As in encapsulation, the data in a class is hidden from other classes, so it is also
known as data-hiding.
• Encapsulation can be achieved by Declaring all the variables in the class as private
and writing public methods in the class to set and get the values of variables.
Prepared by Eng A4K 13
Inheritance
Inheritance is an important pillar of OOP(Object Oriented Programming). It is the
mechanism in java by which one class is allow to inherit the features(fields and methods)
of another class.
Let us discuss some of frequent used important terminologies:
Super Class:
The class whose features are inherited is known as superclass(or a base class or a parent
class).
Sub Class:
The class that inherits the other class is known as subclass(or a derived class, extended
class, or child class). The subclass can add its own fields and methods in addition to the
superclass fields and methods.
Reusability:
Inheritance supports the concept of “reusability”, i.e. when we want to create a new class
and there is already a class that includes some of the code that we want, we can derive
our new class from the existing class. By doing this, we are reusing the fields and
methods of the existing class.
Prepared by Eng A4K 14
<cont’d>
Prepared by Eng A4K 15
Polymorphism(next chapter)
• It refers to the ability of OOPs programming languages to differentiate between
entities with the same name efficiently. This is done by Java with the help of the
signature and declaration of these entities.
Note: Polymorphism in Java are mainly of 2 types:
• Overloading
• Overriding
Prepared by Eng A4K 16
Other OOP concepts
Coupling
Coupling refers to the knowledge or information or dependency of another class. It
arises when classes are aware of each other. If a class has the details information of
another class, there is strong coupling. In Java, we use private, protected, and public
modifiers to display the visibility level of a class, method, and field. You can use
interfaces for the weaker coupling because there is no concrete implementation.
Cohesion
Cohesion refers to the level of a component which performs a single well-defined task.
A single well-defined task is done by a highly cohesive method. The weakly cohesive
method will split the task into separate parts. The java.io package is a highly cohesive
package because it has I/O related classes and interface. However, the java.util package
is a weakly cohesive package because it has unrelated classes and interfaces.
Prepared by Eng A4K 17
Other OOP concepts Cont’d
Association
Association represents the relationship between the objects. Here, one object can be
associated with one object or many objects. There can be four types of association
between the objects:
One to One
One to Many
Many to One, and
Many to Many
Let's understand the relationship with real-time examples. For example, One country
can have one prime minister (one to one), and a prime minister can have many
ministers (one to many). Also, many MP's can have one prime minister (many to
one), and many ministers can have many departments (many to many).
Association can be undirectional or bidirectional.
Prepared by Eng A4K 18
Other OOP concepts (cont’d)
Aggregation
Aggregation is a way to achieve Association. Aggregation represents the relationship
where one object contains other objects as a part of its state. It represents the weak
relationship between objects. It is also termed as a has-a relationship in Java. Like,
inheritance represents the is-a relationship. It is another way to reuse objects.
Composition
The composition is also a way to achieve Association. The composition represents the
relationship where one object contains other objects as a part of its state. There is a
strong relationship between the containing object and the dependent object. It is the
state where containing objects do not have an independent existence. If you delete the
parent object, all the child objects will be deleted automatically.
Prepared by Eng A4K 19
END OF THE
CHAPTER
Prepared by Eng A4K 20