Introduction to OOPS, Characteristics of OOPS, Object oriented languages, comparison between
procedural and object oriented programming, basic principles of Object Orientation-class, object,
abstraction, encapsulation, inheritance, polymorphism, modularity, and message passing. Features
of object orientation - attributes, state, identity, operation, behaviour.
2. Programming paradigms?
Procedural programming:- is a paradigm where list or set of instructions
telling a computer what to do step by step and how to perform from the first
code to the second code. (A systematic order of statements, functions and
commands)
Object Oriented Programming:- is programming paradigm that uses classes
and objects to create models based on the real world environment. In OOPs it
makes it easy to maintain and modify existing code as new objects are
created inheriting characteristics from existing ones
3. OOP?
“Object-oriented programming is a method of implementation in which
programs are organized as cooperative collections of objects, each of
which represents an instance of some class, and whose classes are all
members of a hierarchy of classes united via inheritance relationships.”
- Grady Booch
5. Characteristics of OOP
● At the heart of OOP are four main characteristics. These are the features from
which the advantages of using OOP are born.
○ Data Abstraction
○ Data Encapsulation
○ Inheritance
○ Polymorphism
● These characteristics differentiate object oriented programming from the
traditional procedural programming model.
7. OOP?
● Object means a real word entity such as pen, chair, table etc.
● Object-Oriented Programming is a methodology or paradigm to design a
program using classes and objects.
● It simplifies the software development and maintenance by providing some
concepts:
○ Object
○ Class
○ Inheritance
○ Polymorphism
○ Abstraction
○ Encapsulation
8. Object?
❏ A thing in a real world that can be either physical or conceptual.
❏ An object in object oriented programming can be physical or conceptual.
❏ Conceptual objects are entities that are not tangible in the way real-world
physical objects are.
❏ Bulb is a physical object.
❏ While college is a conceptual object.
❏ Conceptual objects may not have a real world equivalent.
❏ For instance, a Stack object in a program.
9. Object?
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.
10. Class?
❏ A class is a construct created in object-oriented programming languages that
enables creation of objects.
❏ Also sometimes called blueprint or template or prototype from which objects
are created.
❏ It defines members (variables and methods).
❏ A class is an abstraction,
Syntax:
Class ClassName{
-------
}
11. Class?
❏ In general, class declarations can include these components, in order:
❏ Modifiers: A class can be public or has default access (Refer this for details).
❏ Class name: The name should begin with a initial letter (capitalized by
convention).
❏ Superclass(if any): The name of the class’s parent (superclass), if any,
preceded by the keyword extends. A class can only extend (subclass) one
parent.
❏ Interfaces(if any): A comma-separated list of interfaces implemented by the
class, if any, preceded by the keyword implements. A class can implement
more than one interface.
❏ Body: The class body surrounded by braces, { }.
12.
13. Data Abstraction
❏ Hiding internal implementation & highlight the set of services
❏ which only the essential details are displayed to the user.
❏ Allows for simple things to represent complexity.
❏ Such as objects, classes, and variables representing more complex underlying code and
data.
For eg:- Consider the scanner import – we don’t worry about how it works we just
create a scanner object and use it when we want user input.
❏ Data Abstraction may also be defined as the process of identifying only the required
characteristics of an object ignoring the irrelevant details. The properties and
behaviours of an object differentiate it from other objects of similar type and also help
in classifying/grouping the objects.
14. Data Abstraction
❏ In java, abstraction is achieved by interfaces and abstract classes.
❏ This is important because it lets avoid repeating the same work multiple times.
❏ abstraction helps to reduce complexity. You can achieve abstraction in two ways:
❏ Abstract Class
❏ Interface
❏ Advantages
❏ Security
❏ Enhancement will become very easily
❏ Maintainability
❏ Modularity
15. Encapsulation
❏ Encapsulation is defined as the wrapping up of data under a single unit. It is the
mechanism that binds together code and the data it manipulates.
❏ 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.
16. Encapsulation
❏ 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.
17. Inheritance
❏ Inheritance can be defined as the process where one class acquires the properties,
methods and fields of another.
❏ With the use of inheritance the information is made manageable in a hierarchical order.
❏ The class which inherits the properties of the other is known as subclass, derived class,
child class and
❏ the class whose properties are inherited are known as super class, base class, parent
class
❏ In object-oriented programming, inheritance is the concept that when a class of objects
is define, any subclass that is defined can inherit the definitions of one or more general
classes.
18. Inheritance
❏ This means for the programmer that an object in a subclass need not carry its own
definition of data and methods that are generic to the class (or classes) of which it is a
part.
❏ This not only speeds up program development;
❏ it also ensures an inherent validity to the defined subclass object
19.
20. Single 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.
Class A
{
---
}
Class B extends A {
---
}
syntax:
-
21. Multilevel 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.
❏ In this 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{
---
}
22. Hierarchical 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.
In 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{
---
}
23. Hybrid 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.
In this 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 of B and C.
24. Important terminology:
❏ 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.
❏ The keyword used for inheritance is extends.
25. Polymorphism
❏ Polymorphism means taking many forms, where ‘poly’ means many and ‘morph’ means
forms.
❏ 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.
❏ Polymorphism refers to the ability of OOPs programming languages to differentiate
between entities with the same name efficiently.
❏ Polymorphism in Java is of two types:
❏ Run time polymorphism
❏ Compile time polymorphism
26. Compile time polymorphism:-Polymorphism that is resolved during compiler time is known as static
polymorphism. Method overloading can be considered as static polymorphism example.
Method Overloading: This allows us to have more than one methods with same name in a class that
differs in signature.
public class ExampleOverloading
{
public static void main(String
args[])
{
DisplayOverloading obj = new
DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}
class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
Polymorphism
27. Run time polymorphism:-Dynamic polymorphism is a process in which a call to an overridden
method is resolved at runtime, that’s why it is called runtime polymorphism. Method Overriding is
one of the ways to achieve Dynamic Polymorphism. In any object-oriented programming language,
Overriding is a feature that allows a subclass or child class to provide a specific implementation of a
method that is already provided by one of its super-classes or parent classes.
Polymorphism
28. Advantages of Dynamic Polymorphism
➔ Dynamic Polymorphism allows Java to support overriding of methods which is central for run-
time polymorphism.
➔ It allows a class to specify methods that will be common to all of its derivatives while allowing
subclasses to define the specific implementation of some or all of those methods.
➔ It also allows subclasses to add its specific methods subclasses to define the specific
implementation of same
Operator Overloading
An operator or method overloading refers to
a polymorphic characteristic of same symbol
or operator having different meanings (forms)
depending on the context. For example, the
plus symbol (+) is used for mathematical
addition as well as String concatenation. In
either case, only context (i.e. argument types)
determines the interpretation of the symbol.
Output:
29. Modularity
❏ Modularity is the process of decomposing a problem (program) into a set of modules so
as to reduce the overall complexity of the problem.
❏ “Modularity is the property of a system that has been decomposed into a set of
cohesive and loosely coupled modules.” -Booch
❏ Modularity is intrinsically linked with encapsulation. Modularity can be visualized as a
way of mapping encapsulated abstractions into real, physical modules having high
cohesion within the modules and their inter–module interaction or coupling is low
30. 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.
31. Method
A In general, method declarations has six components:
❏ Access Modifier: Defines access type of the method i.e. from where it can be accessed
in your application. In Java, there 4 type of the access specifiers.
❏ public
❏ Protected
❏ private
❏ default (declared/defined without using any modifier):
❏ The return type: The data type of the value returned by the method or void if does not
return a value.
❏ Method Name: the rules for field names apply to method names as well, but the
convention is a little different.
32. Method
❏ Parameter list: Comma separated list of the input parameters are defined, preceded
with their data type, within the enclosed parenthesis. If there are no parameters, you
must use empty parentheses ().
❏ Exception list: The exceptions you expect by the method can throw, you can specify
these exception(s).
❏ Method body: it is enclosed between braces. The code you need to be executed to
perform your intended operations.
34. Modifiers
❏ Access Modifier: controls the access level
Public: The code is accessible for all classes
Default (don't specify a modifier): The code is only accessible in the same package.
Protected: The code is accessible in the same package and subclasses.
Private: code is only accessible within the declared class
❏ Non Access Modifier: do not control access level, but provides other functionality
final(class): The class cannot be inherited by other classes
abstract(class): The class cannot be used to create objects
final(attributes,methods): Attributes and methods cannot be overridden/modified
static(attributes,methods): Attributes and methods belongs to the class, rather than an object
abstract(methods): Can only be used in an abstract class, and can only be used on methods.
The method does not have a body
transient(attributes,methods): Attributes and methods are skipped when serializing the object containing them
synchronized(attributes,methods): Methods can only be accessed by one thread at a time
volatile(attributes,methods): The value of an attribute is not cached thread-locally, and is always read
from the "main memory"
35. Message Passing
❏ 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
method and the information to be sent.
For eg:-
Employee.salary(name);
object
message
information
36. Attribute
❏ Attributes are the individual things that differentiate one object from another and
determine the appearance, state, or other qualities of that object.
❏ Let's create a theoretical class called Motorcycle. A motorcycle class might include
the following attributes and have these typical values:
Color: red, green, silver, brown
Style: cruiser, sport bike, standard
Make: Honda, BMW, Bultaco
❏ Attributes of an object can also include information about its state;
❏ for example, you could have features for engine condition (off or on) or current
gear selected.
❏ Object attributes is the data bundled in an instance of a class. The object
attributes are called instance variables or member fields.
37. State
❏ Set of data fields with current values
❏ The state represent cumulative results of an objective behavior
❏ States are mutable. The state of an object changes several times throughout it's
lifetime. The state gets changes either by some function applied on the object or
through an event outside of the object.
❏ Attributes are immutable. When you create an object you set attributes. And
those attribute values most likely do not change throughout the lifetime of
object.
For example color is an attribute. That doesn't mean color has to be an attribute in all cases. It
will change based on the context. For example consider consider traffic light as an
object, where color is not an attribute but a state.
38. Identity
❏ The identity is a characteristic used to uniquely identify that object – for example,
the cat’s name.
❏ It gives a unique name to an object and enables one object to interact with other
objects.
❏ “Identity is that property of an object which distinguishes it from all others.”
❏ Every instance of a class has its own memory to hold its state
❏ Object identity is typically implemented via a unique ID. The value of the ID is not
visible to the external user. But, it is used internally by the JVM to identify each
object uniquely
39. Behavior
❏ A class's behavior determines how an instance of that class operates;
❏ for example, how it will "react" if asked to do something by another class or object or if
its internal state changes.
❏ Behavior is the only way objects can do anything to themselves or have anything done
to them.
❏ For example, to go back to the theoretical Motorcycle class, here are some behaviors
that the Motorcycle class might have:
❏ Start the engine
❏ Stop the engine
❏ Speed up
❏ Change gear
❏ Stall
❏ To define an object's behavior, you create methods, a set of Java statements that
accomplish some task.
❏ Methods look and behave just like functions in other languages but are defined and
accessible solely inside a class. Java does not have functions defined outside classes
Simplicity:Software objects model real world objects, so the complexity is reduced and the program structure is very clear.
Modularity:Each object forms a separate entity whose internal workings are decoupled from other parts of the system.
Modifiability:It is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods.
Extensibility:Adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones.
Maintainability:Objects can be maintained separately, making locating and fixing problems easier.
Reusability:Objects can be reused in different programs.
Message passing
A single object by itself may not be very useful. An application contains many objects. One object interacts with another object by invoking methods on that object. It is also referred to as Method Invocation.
Object: It is a basic unit of Object Oriented Programming and represents the real life entities. A typical Java program creates many objects, which as you know, interact by invoking methods.
Class: A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type.
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.
Polymorphism: Polymorphism 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.
Abstraction: Data Abstraction is the property by virtue of which only the essential details are displayed to the user.The trivial or the non-essentials units are not displayed to the user. Ex: A car is viewed as a car rather than its individual components.
Encapsulation: Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. 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.
In the below example, you have two classes MacBook and iPad. MacBook is a parent class and iPadis a child class. The child class is overriding the method myMethod() of the parent class. Here, I have assigned child class object to the parent class reference to determine which method would be called at run-time. It is the type of object that determines which version of the method would be called (not the type of reference).
In the below example, you have two classes MacBook and iPad. MacBook is a parent class and iPadis a child class. The child class is overriding the method myMethod() of the parent class. Here, I have assigned child class object to the parent class reference to determine which method would be called at run-time. It is the type of object that determines which version of the method would be called (not the type of reference).