2. What is Object???
• An object is a noun (or pronoun).
⮚In this universe every thing is noun , so we can conclude that every
thing which we are surrounded by is an object.
⮚Are you an Object??
⮚Each and every object in this universe has some properties.
⮚If u consider yourself as an object, what properties do you have???
⮚Color , height ,weight , Name , Address etc
3. What is oop
Stands for "Object-Oriented Programming." OOP(not Oops!) refers to a programming methodology based on objects, instead of just
functions and procedures.
Most modern programming languages including
Java,
C#, and
PHP,
are object-oriented languages,
and many older programming languages now have object-oriented versions.
▪ Object-oriented programming makes it easier for programmers to structure and organize software programs.
▪ Because individual objects can be modified without affecting other aspects of the program.
▪ it is also easier to update and change programs written in object-oriented languages.
▪ As software programs have grown larger over the years, OOP has made developing these large programs more manageable.
4. Pillars Of OOP
• There are 4 fundamentals ideas of Object Oriented Programming. They have the
wonderful terms:
1. Abstraction
2. Polymorphism
3. Inheritance
4. Encapsulation
• One way to remember these is the acronym APIE. Let’s dive into each of them
one by one.
5. Abstraction (Hiding Something
)
Abstraction is the process of hiding out the working style of an object and showing only the required information of the object
in understandable manner.
Real World Example:
▪ Consider an example if you are driving a car on a road and suddenly another car cross you what would you do??
▪ Most probably you will apply brakes . but if you don’t know where is the paddle of brake , can you apply them???
▪ Exactly not.so information about shape and location of brakes are necessary.
▪
▪ But is it necessary to you to know about the procedure running behind the car when you apply brakes?? Does it really
matter to you.
▪ NO!!!! We are only interested in the end results it is beyond of our interest that what mechanicaloperations are
performing.
▪ Same on this pattern abstraction says “show only those things which are relevant and hide unnecessary complexities”.
6. Polymorphism (Many F orm)
Poly=many morph= form
By name we can come to a conclusion that it's about something with many forms. In OOP it is an ability of an object to
take many forms.
Real World Example:
My name is Owais:
I am a Faculty,
a brother,
a friend,
a son,
a student
depending upon the situation i.e. parameter surrounding me.
Similarlyin OOP we can have a function with different form but with same name.
7. Inheritance (sharing)
• Inheritance is when an object acquires the property of another object.
• Inheritance allows a class (subclass) to acquire the properties and behavior of another class (superclass).
• It helps to reuse, customize and enhance the existing code.
• So it helps to write a code accurately and reduce the development time.
• Real World Example
• In our tradition if a father has any property , it will be further divided into his children or we can say that children can have
advantages of their father’s possessions.
• Same as it is inheritance say that if there is a relation of child and parent between objects.so the properties of parent ca n be
inherited by children.
• Another example, a scientific calculator is an extended form of Calculator, here Calculator is parent and scientific calculator is Child object.
8. Encapsulation (packing)
• Encapsulation means wrapping up data and member function (Method) together into a single unit .
• Encapsulation automatically achieve the concept of data hiding providing security to data by making the variable as
private and expose the property to access the private data which would be public.
• Real World Example
9. Advantages Of OOP
• Improved software-developmentproductivity: Object-oriented programming is modular, as it provides separation of duties in
object-based program development.
• Improved software maintainability: For the reasons mentioned above, object oriented software is also easier to maintain.
Since the design is modular, part of the system can be updated in case of issues without a need to make large-scale changes.
• Faster development: Reuse enables faster development.Object-oriented programming languages come with rich libraries of
objects, and code developed during projects is also reusable in future projects.
• Real World Modeling : as we know that oop is based on object and object are the part of real world. So we can easily
associate all of our task with real world.
10. Disadvantages Of OOP
• Steep learning curve: The thought process involved in object-oriented programming may not be natural for some people, and it can take time
to get used to it. It is complex to create programs based on interaction of objects. Some of the key programming techniques, such as
inheritance and polymorphism, can be challenging to comprehend initially.
• Larger program size: Object-oriented programs typically involve more lines of code than procedural programs.
• Slower programs: Object-oriented programs are typically slower than procedurebased programs, as they typically require more instructions to
be executed.
11. Object Oriented Design
• Object Oriented Design is the concept that forces programmers to plan out their code in order to have a better flowing program.
• Technique that focuses on designing the data and the interfaces to it.
• Process of creating a system where objects coordinate or interact with each other to accomplish a task
12. Classes
Object :
• Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging
the tail, barking, eating. An object is an instance of a class.
Object can also be referred to as an instance of a class.
• An object is one copy of the class.
Class :
• A class can be defined as a template/blueprint that describes the behavior/state that the object of its type
support.
• A class or a category is a group of people or things having common characteristics .
13. Messages And Method
• A method is a set of steps or instructions, or a procedure to accomplish something
• Message is a set of words, a phrase, or statement to convey something
• The figure shows how the objects pass messages to each other to fulfill a task.
• Here (makeCake,DeliverCake,AcceptCake) are methods while each entity passing messages to
another to make the task completed.
14. Modules
• To solve complex problems divide the problem into smaller units or modules.
• The figure shows the approximate time it took to Bob to deliver the cake when he does everything himself.
• This shows that by dividingthe task between multiple people, Bob was able to achieve his goal in almost half the time than he
would have done it himself
15. Generalization
• Generalizationis a bottom-up approach in which two lower level entities combine to forma higher level entity. In generalization, the
higher level entity can also combine with other lower level entity to make further higher level entity.
16. Specializatio
n
• Specialization is opposite toGeneralization. It is a top-downapproach in which one higher level entity can be broken down into two lower level
entity. In specialization, some higher level entities may not have lower-level entity sets at all.
17. Coupling
• Coupling is the principle of "separation of concerns". This means that one object doesn't directly change or modify the state or behavior of
another object.
• Coupling looks at the relationship between objects and how closely connected they are.
• A good example of coupling is HTML and CSS. Before CSS, HTML was used for both markup and presentation. This created bloated code that
was hard to change and difficult to maintain. With the advent of CSS, HTML became used just for markup, and CSS took over for presentation.
This made the code fairly clean and easily changeable. The concerns of presentation and markup were separated.
loosely coupled:
• Objects that are independent from one another and do not directly modify the state of other objects are said to be loosely coupled. Loose
coupling lets the code be more flexible, more changeable, and easier to work with.
tightly coupled:
• Objects that rely on other objects and can modify the states of other objects are said to be tightly coupled. Tight coupling creates situations
where modifying the code of one object also requires changing the code of other objects (also known as a ripple effect). Tightly coupled code
is also harder to reuse because it can't be separated.
Tightly Coupled
Loosely
Coupled
18. Cohesion
• Cohesion is the principle of being or doing one thing well. In other words, cohesion means grouping together code that
contributes to a single task.
• Each object should only have one responsibility.Every behaviorof that object should only do one task. Any more than that
and you'll have a much harder time making changes to the code.
• Code that is organized by functionality and does only one task is said to have high cohesion. Highly cohesive code is reusable,
simple, and easy to understand. It also creates objects that are small and focused.
• Code that is organized arbitrarily and has multiple tasks is said to have low cohesion. Such code is difficult to understand,
maintain, and reuse, and is often complex. It also creates objects that are large and unfocused.
• Having high cohesion is generally good, while having low cohesion is generally bad. When writing code, always strive to write
highly cohesive code.
19. Classes In OOP
• It is similar to structures in C language. Class can also be defined as user defined data type but it also
contains functions in it. So, class is basically a blueprint for object. It declare &defines what data
variables the object will have and what operations can be performed on the class's object.
• In Java, a class is a definition of objects of the same kind. In other words, a class is a blueprint, template, or
prototype that defines and describes the static attributes and dynamic behaviors commonto all objects of the
same kind.
20. Access Modifiers
• There are 4 types of java access modifiers:
1. private
2. default
3. protected
4. Public
private access modifier
• The private access modifier is accessible only within class.
default access modifier
• If you don't use any modifier, it is treated as default bydefault. The default modifier is accessible only within package.
protected access modifier
• The protected access modifier is accessible within package and outside the package but through inheritance only.The protected
access modifier can be applied on the data member, method and constructor.It can't be applied on the class.
public access modifier
• The public access modifier is accessible everywhere. It has the widest scope among all other modifiers.
21. Method
• A Java method is a collection of statements that are grouped together to perform an operation.
• Defines the behavior of a class
• Consists of the actions that an object or instance of a class can perform on the data members
Constructor
• A constructor is a special method of a class or structure in object-oriented programming that initializes an object of that type. A constructor is
an instance method that usually has the same name as the class, and can be used to set the values of the members of an object , either to
default or to user-defined values.
There are basically two rules defined for the constructor.
• Constructor name must be same as its class name
• Constructor must have no explicit return type
22. There are two types of constructors :
• Default constructor (no-arg constructor)
• Parameterized constructor
Default Constructor
• A constructor is called "Default Constructor" when it doesn't have any parameter.
parameterized constructor
A constructor which has a specific number of parameters is called parameterized constructor.
23. Level Of Abstraction
• The amount of complexity by which a system is viewed or programmed. The higher the level, the less detail. The lower the level, the more
detail. The highest level of abstraction is the entire system.
There are three level of Abstraction:
1. Top Most Level
2. Next Level
3. Last Level
• Example: Let’s say we are storing customer information in a customer table. At Last Level these records can be described as blocks of storage
(bytes, gigabytes, terabytes etc.) in memory. These details are often hidden from the programmers.
• At the Next Level these records can be described as fields and attributes along with their data types, their relationship among each other can be
logically implemented. The programmers generally work at this level because they are aware of such things about database systems.
• At Top Most Level, user just interact with system with the help of GUI and enter the details at the screen, they are not aware of how the data is
stored and what data is stored; such details are hidden from them.
24. Types of Inheritance
we have 5 different types of Inheritance. Namely,
1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance (also known as Virtual Inheritance)
1. Single Inheritance
Parents have only one child at a time OR there should only be a derived class from base class.
Parent/Base
Child/Derived
25. 2. Multiple Inheritance
• In this type of inheritance a single derived class may inherit from two or more than two base classes.
3. Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherits from a single base class. means parents have more than one children.
parent1 Parent2
Child
Parent
child1 child2 child3
26. 4. Multilevel Inheritance
In Multilevel Inheritancea derived class will be inheriting a parent class and as well as the derived class act as the parent class
to other class.
5. Hybrid Inheritance in Java
Hybrid Inheritance is the combination of both Single and Multiple Inheritance. Again Hybrid inheritance is also not directly supported in Java only through interface
we can achieve this. Flow diagram of the Hybrid inheritance will look like below. As you can ClassA will be acting as the Parent class for ClassB & ClassC and ClassB &
ClassC will be acting as Parent for ClassD.
parent Child Child Grand Child
27. Problem With Multiple Inheritance
The Diamond Problem - a class inherits from two parent classes each of which in turn inherit from a common
super class.
28. Interface
There are mainly three reasons to use interface. They are given below.
1. Itis used to achieve abstraction.
2. By interface, we can support the functionality of multiple inheritance.
3. Itcan be used to achieve loose coupling.
29. PolyMorphism
• If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.
• If we have to performonly one operation, having same name of the methods increases the readability of the program.
Differentways to overload the method
There are two ways to overload the method in java
1. By changing number of arguments
2. By changing the data type
By Changing Arguments
Int add(int a,int b) int add(int a,int b, int c)
By Changing DataTypes
int add(int a, int b)
{
}
int add(double a ,double b)
{
}
{
{
} }
30. Overriding
If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in
java.
Rules for Java Method Overriding
1. method must have same name as in the parent class
2. method must have same parameter as in the parent class.
3. must be IS-A relationship (inheritance).
32. Replacement & Refinement
Replacement :
When the child class completely overrides the parent class method implementation.
Refinement :
When the child class includes the functionality of the parent class version of the overridden
method within its own version.
33. Abstract Method
• An abstract method is one which is declared inside parent class but not implemented there.
34. pattern
• In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design . A design
pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be
used in many different situations.
Design patterns
• Design patterns in software are strategies which are language-independent and used for solving commonly encountered object-oriented
design problems.
Documenting patterns serves two important purposes:
• It speeds up the process of finding a solution when another problem with similar characteristics is encountered.
• Giving names to patterns allows programmers to use a common vocabulary to discuss design alternatives.
35. Factory Pattern
• creates objects without exposing the instantiation logic to the client.
• refers to the newly created object through a common interface
The implementation is really simple
The client needs a product, but instead of creating it directly using the new operator, it asks the factory object for a new
product, providing the information about the type of object it needs.
• The factory instantiates a new concrete product and then returns to the client the newly created product(casted to
abstract product class).
• The client uses the products as abstract products without being aware about their concrete implementation.
36. Singleton Design Pattern
• Ensure a class has only one instance, and provide a global point of access to it.
• The Singleton pattern ensures that a class has only one instance and provides a
global point of access to that instance.
• Example
• The office of the President of the United States is a Singleton. The United States Constitution specifies the means
by which a president is elected, limits the term of office, and defines the order of succession. As a result, there
can be at most one active president at any given time. Regardless of the personal identity of the active president,
the title, "The President of the United States" is a global point of access that identifies the person in the office.
37. Composite Design Pattern
composite objects:
• objects that contain other objects; for example, a drawing may be composed of graphic primitives, such as lines, circles, rectangles, text, and
so on.
• Composite pattern is used where we need to treat a group of objects in similar way as a single object. Composite pattern composes objects in
term of a tree structure to represent part as well as whole hierarchy.
Example
In an organization, It have general managers and under general managers, there can be managers and under managers there can be developers.
Now you can set a tree structure and ask each node to perform common operation like getSalary().
Composite design pattern treats each node in two ways:
1) Composite – Composite means it can have other objects below it.
2) leaf – leaf means it has no objects below it.
38. Decorator pattern
• Decorator pattern is used to dynamically add or remove responsibilities to an object without affecting
other objects.
• Decorator pattern allows a user to add new functionality to an existing object without altering its
structure.
• assault gun is a deadly weapon on it's own. But you can apply certain "decorations" to make it more
accurate, silent and devastating.
39. Proxy Pattern
• In proxy pattern, a class represents functionality of another class.
• When creating an object is too expensive in terms of time or memory.
• To postpone creation of an object until it is actually needed.
• When loading a large image which can be time consuming.
• For example, you save your money in bank in form of rupees.but when you want
to withdraw money you have to write a check. It means check is not it self money
but having functionality of money.
40. Frameworks
• In OOP, a framework is a set of reusable software components or classes that
form the basis of an application.
• it makes it easier to work with complex technologies
• it ties together a bunch of discrete objects/components into something more
useful
• it forces the team (or just me) to implement code in a way that promotes
consistent coding, fewer bugs, and more flexible applications
• everyone can easily test and debug the code, even code that they didn't write
• A software framework is an abstraction in which software providing generic
functionality can be selectively changed by additional user-written code, thus
providing application-specific software.