4. Contents
• Introduction to Object Oriented Programming
• Encapsulation
• Association
• Inheritance
• Polymorphism
• Exception Handling
• Abstract Class and Interface.
• Advantage Object Oriented Programming
5. Introduction to Object Oriented
Programming
• An object is a unit of code that is responsible to accomplish a particular
task located inside code. In other way, we can tell that an object is a data
structure which solves a particular problem. To do its work, an object may
need some data and methods.
• From books we might have learned how to use classes (class library) or even learned how to write our own
classes and making object from them. But OOP is more than these. Designing beautiful classes, keep them
simple, make them for reusability and easy to change is the ultimate goal of OO code and design.
6. Object:
A thing that has identity, state, and behavior
• Identity: a distinguished instance of a class
• State: collection of values for its variables
• Behavior: capability to execute methods
* variables and methods are defined in a class.
Class:
In object-oriented programming, a class is an extensible program-code-template for creating objects,
providing initial values for state (member variables) and implementations of behavior (member functions or
methods).
Animal
Dog Cat Fish
7. • Encapsulation (Information Hiding):
• When creating new data types (classes) the details of the actual data and the way operations work is hidden
from the other programmers who will use those new data types
• Unnecessary implementation details should be hidden from the user
• In Java/C++, use classes and access modifiers (public, private, protected)
• Example: microwave, radio, iPad.
8. Example:
public class RunEncap{
public static void main(String args[]){
EncapTest encap = new EncapTest();
encap.setName(“MH Abid");
encap.setAge(22);
encap.setIdNum("151-51-226");
System.out.print("Name : " +
encap.getName() + " Age : " + encap.getAge());
}
}
OUTPUT:
Name : MH Abid Age : 22
9. Association:
• It represents a relationship between two or more objects where all objects have their own lifecycle
and there is no owner.
• Let’s take an example of relationship between Teacher and Student. Multiple students can associate
with a single teacher and a single student can associate with multiple teachers. Both object can be
created and deleted independently.
10. Here is an example of
“Association”
from our project.
In the picture we can see that in
the “Main”class we made an
object of “HomePage and
Sample”class.
11. • Inheritance
• In object-oriented programming, inheritance enables new objects to take on the properties of existing
objects. A class that is used as the basis for inheritance is called a superclass or base class. A class that
inherits from a superclass is called a subclass or derived class.
12. • Every Employee has a name, age, and birthday method as well as a salary and a pay
method.
class Person {
String name;
int age;
String id;
void birthday () {
}
}
class Employee extends Person {
double salary;
void pay () {
}
}
13. •Polymorphism:
“Poly”= Many, “Morphism”= forms
• Polymorphism = ability to take more than one form (objects have more than one type)
• A class can be used through its parent interface
• A child class may override some of the behaviors of the parent class.
Two types –
Compile time polymorphism(Overloading).
Run time polymorphism(Overriding).
14. • Compile time polymorphism:
• Compile time Polymorphism ( or Static polymorphism) Compile time polymorphism is nothing but the
method overloading in java.
15. • Run time polymorphism:
• The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a
child class object. Dynamic Binding/Runtime Polymorphism : Run time Polymorphism also known as
method overriding.
16. Exception Handling:
• An exception is a problem that arises during the execution of a program. A exception is a
response to an exceptional circumstance that arises while a program is running, such as an
attempt to divide by zero.
• Exceptions provide a way to transfer control from one part of a program to another.
exception handling keywords: try, catch, throw ,throws and finally.
class Exception
{
public static void main(String args[])
{
int a = 42 / 0;
}
}
17. • try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by
one or more catch blocks.
• catch: A program catches an exception with an exception handler at the place in a program where you want
to handle the problem. The catch keyword indicates the catching of an exception.
• throw: throw keyword is used to throw an exception explicitly. Only object of Throwable class or its sub
classes can be thrown. Program execution stops on encountering throw statement.
18. Throws:
Any method capable of causing
exceptions must list all the exceptions
possible during its execution, so that
anyone calling that method gets a prior
knowledge about which exceptions to
handle. A method can do so by using
the throws keyword.
19. Finally:
A finally keyword is used to create a
block of code that follows a try
block. A finally block of code
always executes whether or not
exception has occurred.
20. • Abstract Class :
• Classes which contain one or more abstract methods or abstract properties, such methods or properties do
not provide implementation.
• Syntax :
• abstract class class name { }
• Abstract method:
• Method that are declared without any body within an abstract class is known as abstract method. The
method body will be defined by its subclass. Abstract method can never be final and static. Any class that
extends an abstract class must implement all the abstract methods declared by the super class.
• Syntax :
• Abstract return_typefunction_name ();
22. • Interface:
• An interface is a description of the actions that an object can do. For example when you flip a light switch,
the light goes on, you don't care how, just that it does. In Object Oriented Programming, an Interface is a
description of all functions that an object must have in order to be an "X“.
• Syntax:
• interface interface name { }
23. Interface
Implementation
This is how a class implements an
interface. It has to provide the body
of all the methods that are declared
in interface.
Note: class implements interface but
an interface extends another
interface.
25. Advantages of OOP
• • 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;
• • Extensibility: adding new features or responding to changing operating
environments can be solved by
• • Maintainability: objects can be maintained separately, making locating and fixing
problems easier;
• •Re-usability: objects can be reused in different programs