3. Q. What is OOPs?
• Object-oriented programming (OOP) is a way to organize and
conceptualize a program as a set of interacting objects.
• The key idea :- The real world can be accurately described as a
collection of objects that interact with each other.
4. Object Oriented vs. Structured Language
Object Oriented
• Based on Objects
• It includes approach in the form of
Encapsulation, Inheritance,
Polymorphism and Abstraction
• Vast Reusability
Ex. Java, C++, Python, and Ruby
Structured Language
• No Objects
• Procedural approach in the form of
functions
• Top-Down design
• Limited Reusability
Ex. C, Pascal, and Fortran
5. Why
OOPs ?
• Increases Productivity
Code Reusability
• Easier Integration
Adaptability
• Affordable and easy to Maintain
Cost Effective
• Objects and Classes can be tested Independently
Easy Debugging
• Allows for the organization and management of
large codebases
Scalability
• Access to Objects can be
restricted(Encapsulation)
Modularity
6. Pillars of OOPs Inheritance Polymorphism
Abstraction Encapsulation
7. Inheritance
Q. What is Inheritance ?
Literally it means Inheriting something from one person to other i.e.
Property from grandfather to father and father to son.
Grandfather’s
property
(G)
Father’s
property
(F+G)
Son’s property
(S+F+G)
8. Inheritance
How ?
• Derives a new class from existing one.
• Existing class is called a Parent-class or
Super class.
• Derived class is called is Child-class or
Sub-class.
• Achieved through “extends” keyword.
• Java supports single and Multi-level
Inheritance.
Exception:
Java does not support Multiple
Inheritance To prevent Ambiguity
but can be achieved through Interface.
9. public class Country
{
public void country()
{
System.out.println(“
Country – India”);
}
}
public class State extends
Country
{
public void state()
{
System.out.println(“
State-maharashtra ”);
}
}
public class City extends
State
{
public void city()
{
System.out.println(“
City-Pune”);
}
}
public class Test
{
public static void main(String [] args)
{
City c=new City();
c.country();
c.state();
c.city();}
}
output
Country-India
State-Maharashtra
City-Pune
10. Polymorphism
Q. What is Polymorphism?
• Literally it means One name many forms.
• Allows objects of different classes to be treated as objects of a common superclass
• It is closely related to inheritance and method overriding.
• Achieved through 1) Overriding (Dynamic binding)
2) Overloading (Static Binding)
11. • Polymorphism allows you to write more flexible
and extensible code
Flexibility and
Extensibility:
• Subclasses can override or extend the behavior
of the parent class, reducing code duplication
Code Reusability
• more readable and self-explanatory
• expresses the intent more clearly.
Enhances
Readability
• allows you to have a single method name with
different implementations
Simplifies Method
Overloading
WHY ?
Polymorphism
12. Polymorphism How ?
Overloading
• Multiple methods in the same class
with the same name but different
parameter lists
• Determined at compile-time (static
binding)
• Only within the same class.
• Can have the same or different
return types
Override
• Used in inheritance to provide a specific
implementation of a method in a
subclass that is already defined in its
superclass
• Determined at runtime (dynamic
binding)
• A method in a subclass with the same
name, return type, and parameter
must be same to Superclass
• Requires the same return type
13. Compile-Time Polymorphism
(Static Binding)
Run-Time Polymorphism
(Dynamic Binding)
public class Addition{
public int add(int a, int b) {
return a + b; }
public double add(double a, double b) {
return a + b; }
public static void main(String[] args)
{
Addition a=new Addition();
a.add(11111111,44444444);
} }
Output:
555555555
public class Animal {
public void bark() {
System.out.println("Animal barks"); } }
public class Dog extends Animal {
@Override
public void bark() {
System.out.println("Dog barks"); } }
public class Test{
public static void main(String [] args){
Dog d=new Dog();
d.bark();}}
Output:
Dog barks