2. Object-oriented programming
• Object-oriented programming System(OOPs) is a programming
paradigm based on the concept of “objects” that contain data and
methods.
• The primary purpose of object-oriented programming is to increase
the flexibility and maintainability of programs.
• Object oriented programming brings together data and its
behavior(methods) in a single location(object) makes it easier to
understand how a program works.
3. What is an Object?
• Object: is a bundle of data and its behavior (often known as
methods).
• Objects have two characteristics: They have states and behaviors.
• Examples of states and behaviors
Example 1: Example 2:
Object: House Object: Car
State: Address, Color, Area State: Color, Brand, Weight, Model
Behavior: Open door, close door Behavior: Break, Accelerate, Slow Down,
Gear change.
4. What is an Object? Con…
class House {
String address;
String color;
double area;
void openDoor() {
//Write code here
}
void closeDoor() {
//Write code here
}
... ...
}
6. Abstraction
• One of the most fundamental concept of OOPs is Abstraction.
• Abstraction is a process where you show only “relevant” data and
“hide” unnecessary details of an object from the user.
• For example:
• when you login to your Amazon account online, you enter your
user_id and password and press login, what happens when you
press login, how the input data sent to amazon server, how it gets
verified is all abstracted away from the you.
7. Abstraction Con…
• Another example of abstraction:
• A car in itself is a well-defined object, which is composed of several other
smaller objects like a gearing system, steering mechanism, engine, which are
again have their own subsystems. But for humans car is a one single object,
which can be managed by the help of its subsystems, even if their inner
details are unknown.
9. Encapsulation
• Encapsulation simply means binding object state(fields) and
behavior(methods) together. If you are creating class, you are doing
encapsulation.
• Encapsulation is:
• Binding the data with the code that manipulates it.
• It keeps the data and the code safe from external interference
10. Encapsulation Con…
• The whole idea behind encapsulation is to hide the implementation
details from users. If a data member is private it means it can only be
accessed within the same class. No outside class can access private
data member (variable) of other class.
• However if we setup public getter and setter methods to update (for
example void setSSN(int ssn))and read (for example int getSSN()) the private
data fields then the outside class can access those private data fields
via public methods.
11. Encapsulation Con…
• This way data can only be accessed by public methods thus making
the private fields and their implementation hidden for outside
classes. That’s why encapsulation is known as data hiding.
12. Encapsulation Con…
• Example of Encapsulation in Java
• How to implement encapsulation in java:
1. Make the instance variables private so that they cannot be
accessed directly from outside the class. You can only set and
get values of these variables through the methods of the class.
2. Have getter and setter methods in the class to set and get the
values of the fields.
13. Encapsulation Con…
• A example of encapsulation is the class of java.util.Hashtable. User
only knows that he can store data in the form of key/value pair in a
Hashtable and that he can retrieve that data in the various ways. But
the actual implementation like, how and where this data is actually
stored, is hidden from the user. User can simply use Hashtable
wherever he wants to store Key/Value pairs without bothering about
its implementation.
15. Advantages of encapsulation
• It improves maintainability and flexibility.
• The fields can be made read-only.
• User would not be knowing what is going on behind the scene. They
would only be knowing that to update a field call set method and to
read a field call get method but what these set and get methods are
doing is purely hidden from them.
17. Inheritance
• Inheritance is the mechanism by which an object acquires the
some/all properties of another object.
• The process by which one class acquires the properties(data
members) and functionalities(methods) of another class is called
inheritance.
18. Inheritance
• The aim of inheritance is to provide the reusability of code so that a
class has to write only the unique features and rest of the common
properties and functionalities can be extended from the another
class.
19. Inheritance
• Inheritance is a process of defining a new class based on an existing
class by extending its common data members and methods.
• Inheritance allows us to reuse of code, it improves reusability in your
java application.
• Note: The biggest advantage of Inheritance is that the code that is
already present in base class need not be rewritten in the child class.
20. Child and Base Class
• Child Class:
• The class that extends the features of another class is known as child class,
sub class or derived class.
• Parent Class:
• The class whose properties and functionalities are used(inherited) by
another class is known as parent class, super class or Base class.
• Note: The derived class inherits all the members and methods that
are declared as public or protected.
22. Types of inheritance
• Single Inheritance: refers to a child and parent class relationship where a
class extends the another class.
• Multilevel inheritance: refers to a child and parent class relationship where
a class extends the child class. For example class C extends class B and class
B extends class A.
• Hierarchical inheritance: refers to a child and parent class relationship
where more than one classes extends the same class. For example, classes
B, C & D extends the same class A.
• Multiple Inheritance: refers to the concept of one class extending more
than one classes, which means a child class has two parent classes. For
example class C extends both classes A and B. Java doesn’t support
multiple inheritance
23. Constructors and Inheritance
• Constructor of sub class is invoked when we create the object of
subclass, it by default invokes the default constructor of super class.
Hence, in inheritance the objects are constructed top-down.
• The superclass constructor can be called explicitly using the super
keyword, but it should be first statement in a constructor.
• Example in java (Demo)
25. Polymorphism
• Polymorphism is one of the OOPs feature that allows us to perform a
single action in different ways.
• Polymorphism is the capability of a method to do different things
based on the object that it is acting upon. In other words,
polymorphism allows you define one interface and have multiple
implementations.
26. Polymorphism
• In other words it means, one method with multiple implementation,
for a certain class of action. And which implementation to be used is
decided at runtime depending upon the situation (i.e., data type of
the object)
27. Static and Dynamic Polymorphism
• Polymorphism could be static and dynamic both.
• Method Overloading is static polymorphism while, Method
overriding is dynamic polymorphism.
28. Static Polymorphism
• Overloading in simple words means more than one method having
the same method name that behaves differently based on the
arguments passed while calling the method.
• This called static because, which method to be invoked is decided at
the time of compilation.(compile time)
29. Dynamic Polymorphism
• Overriding means a derived class is implementing a method of its
super class. The call to overridden method is resolved at runtime,
thus called runtime polymorphism
It improves maintainability and flexibility and re-usability: for e.g. In the above code the implementation code of void setEmpName(String name) and String getEmpName() can be changed at any point of time. Since the implementation is purely hidden for outside classes they would still be accessing the private field empName using the same methods (setEmpName(String name) and getEmpName()). Hence the code can be maintained at any point of time without breaking the classes that uses the code. This improves the re-usability of the underlying class.