Overview of OOP: Network System Level-7
Overview of OOP: Network System Level-7
Lecture 1
Overview of OOP
[email protected]
Overview of OOP
Object Oriented Programming is a programming method that
combines:
a) Data
b) Instructions for processing that data into a self-sufficient ‘object’ that
can be used within a program or in other programs.
• Advantage of Object Oriented Programming
a) Objects are modeled on real world entities.
b) This enables modeling complex systems of real world into manageable
software solutions.
• Programming techniques
a) Unstructured Programming (Assembly language programming)
b) Procedural Programming (Assembly language, C programming)
c) Object Oriented Programming (C++, Java, Smalltalk, C#, Objective C)
Unstructured Programming
This consists of just writing the sequence of commands or statements in the main
program, which modifies the state maintained in Global Data. Example: Assembly
Language programs.
• Limitations of Unstructured Programming:
a) The data is global and code operates on it
b) As the size of code increases, maintenance is a problem
c) Does not have independent data for processing
d) The concept of local variables did not exist
e) Reusability of code was not supported
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 (or functions) on that object. Through the
interaction of objects, programmers achieve a higher order of
functionality which has complex behavior. One object invoking
methods on another object is known as Message passing. It is also
referred to as Method Invocation.
Class
• A class is a prototype that defines the variables and the methods common to
all objects of a certain kind. Member Functions operate upon the member
variables of the class. An Object is created when a class in instantiated.
How to create an Object? An object is created when a class is instantiated
Declaring an Object of class :
ClassName Objectname;
Object definition is done by calling the class constructor
Constructor: A special member function which will be called
automatically to initialize the data member of a class whenever object is
instantiated. Memory space is allocated only when a class is
instantiated i.e. when an object is created
Defining a variable : E.g. GraduationCourse mycourse= new
GraduationCourse();
:Object Oriented Programming features
Abstraction
The purpose of abstraction is to hide information that is not
relevant or rather show only relevant information and to
simplify it by comparing it to something similar in the real
world.
Abstraction means “The process of forming of general and
relevant concept from more complex scenarios”.
Note 1: Abstraction is used to build complex systems. Key to
simplify a complex design into smaller, manageable parts
which then become the components of a bigger and
complex system. The idea of hiding the complexity within a
smaller/simple component of a system.
Note 2: Abstraction is not a feature of Object oriented
concepts alone. Even in procedural language
programming, abstraction can be achieved to a limited
Encapsulation
Encapsulation means the localization of the information or
knowledge within an object.
Encapsulation is also called as “Information Hiding”.
1) Objects encapsulate data and implementation “details. To the
outside world, an object is a black box that exhibits a certain
behavior.
2) The behavior of this object is what which is useful for the
external world or other objects.
3) An object exposes its behavior by means of methods or functions.
4) The set of functions an object exposes to other objects or external
world acts as the interface of the object.
Benefits of Encapsulation
1) The functionality where in we can change the implementation code
without breaking the code of others who use our code is the biggest
benefit of Encapsulation.
2) Here in encapsulation we hide the implementation details behind a
public programming interface. By interface, we mean the set of
accessible methods our code makes available for other code to call—in
other words, our code’s API.
3) By hiding implementation details, We can rework on our method code
at a later point of time, each time we change out implementation this
should not affect the code which has a reference to our code, as our
API still remains the same
How to bring in Encapsulation
4) Make the instance variables protected.
5) Create public accessor methods and use these methods from within the
calling code.
6) 3) Use the JavaBeans naming convention of getter and setter.
Eg: getPropertyName, setPropertyName.
Inheritance
The process by which one class acquires the properties and functionalities of
another class. Inheritance provides the idea of reusability of code and each sub
class defines only those features that are unique to it.
1. Inheritance is a mechanism of defining a new class based on an existing class.
2. Inheritance enables reuse of code. Inheritance also provides scope for
refinement of the existing class. Inheritance helps in specialization
3. The existing (or original) class is called the base class or super class orparent
class. The new class which inherits from the base class is called the derived
class or sub class or child class.
4. Inheritance implements the “Is-A” or “Kind Of/ Has-A” relationship.
Note : The biggest advantage of Inheritance is that, code in base class need
not be rewritten in the derived class. The member variables and methods of
the base class can be used in thederived class as well.
Note: Multi-level inheritance is allowed in Java but not multiple
inheritance
Types of Inheritance
Multilevel Inheritance:
Multilevel inheritance refers to a mechanism in OO technology where one
can inherit from a derived class, thereby making this derived class the base
class for the new class.
Multiple Inheritance :
“Multiple Inheritance” refers to the concept of one class inheriting from
more than one base class. The inheritance we learnt earlier had the concept
of one base class or parent. The problem with “multiple inheritance” is that
the derived class will have to manage the dependency on two base classes.
Note 1: Multiple Inheritance is very rarely used in software projects. Using
Multiple inheritance often leads to problems in the hierarchy. This results
in unwanted complexity when further extending the class.
Note 2: Most of the new OO languages like Small Talk, Java, C# do not
support Multiple inheritance. Multiple Inheritance is supported in C++.
Polymorphism
Polymorphism is a feature that allows one interface to be used for a
general class of actions. It’s an operation may exhibit different
behavior in different instances. The behavior depends on the types of
data used in the operation. It plays an important role in allowing
objects having different internal structures to share the same external
interface. Polymorphism is extensively used in implementing
inheritance.
Types of Polymorphism:
1) Static Polymorphism.
2) Dynamic Polymorphism
Static Polymorphism: Function Overloading – within same class more
than one method having same name but differing in signature.
Resolved during compilation time. Return type is not part of method
signature.
Dynamic Polymorphism Function Overriding – keeping the signature and
return type same, method in the base class is redefined in the derived
class. Resolved during run time. Which method to be invoked is
Overriding
Redefining a super class method in a sub class is called method
overriding. The method signature ie. method name, parameter list and
return type have to match exactly. The overridden method can widen
the accessibility but not narrow it, ie if it is private in the base class,
the child class can make it public but not vice versa.
Overriding Examples:
Consider a Super Class Doctor and Subclass Surgeon. Class Doctor has a
method treatPatient(). Surgeon overrides treatPatient method ie gives
a new definition to the method.
Doctor doctorObj = new Doctor();
// Call the treatPatient method of Doctor class
doctorObj.treatPatient()
Surgeon surgeonObj = new Surgeon();
// Call the treatPatient method of Surgeon class
surgeonObj.treatPatient()
Doctor obj = new Surgeon();
// calls Surgeon’s treatPatient method as the reference is pointing to Surgeon
obj.treatPatient();
Method/Function Overloading
Abstract Classes Outlines the behavior but not necessarily implements all of its
behavior. Also known as Abstract Base Class. Provides outline for behavior by
means of method (abstract methods) signatures without an implementation.
Note 1: There can be some scenarios where it is difficult to implement all the
methods in the base class. In such scenarios one can define the base class as an
abstract class which signifies that this base class is a special kind of class which
is not complete on its own. A class derived from the abstract base class must
implement those member functions which are not implemented in the abstract
class.
Note 2: Abstract Class cannot be instantiated. To use an abstract class one has to
first derive another class from this abstract class using inheritance and then
provide the implementation for the abstract methods. Note 3: If a derived class
does not implement all the abstract methods (unimplemented methods), then
the derived class is also abstract in nature and cannot be instantiated.
What is a Constructor
1) A method with the same name as class name used for the purpose of creating an
object in a valid state
2) It does not return a value not even void
3) It may or may not have parameters (arguments)
4) A class contains one or more constructors for making new objects of that class
5) If (and only if) the programmer does not write a constructor, Java provides a default
constructor with no arguments.
The default constructor sets instance variables as:
numeric types are set to zero
boolean variables are set to false
char variables are set to ‘’
object variables are set to null.
When a constructor executes, before executing its own code: It implicitly call the
default constructor of it’s super class Or can make this constructor call
explicitly, with super(…);
A constructor for a class can call another constructor for the same class by putting
this(…); as the first thing in the constructor. This allows you to avoid repeating
code.
A Class do not inherit the constructors of a super class.
Generalization and Specialization: In order to implement the concept of
inheritance in an OO solution, one has to first identify the similarities
among different classes so as to come up with the base class.
This process of identifying the similarities among different classes is
calledGeneralization.
The class which is identified after generalization is the base class. The classes
over which the generalization is made are the derived classes.
The classes over which the generalization is built are viewed asSpecialization.
Generalization identifies the common attributes and behavior among
different entities whereas specialization identifies the distinct
attributes and their behavior which differentiate a derived class from
the other classes.
A Generalization–Specialization hierarchy is used to depict the
relationship between the generalized class and the corresponding
specialized classes.
Access Specifiers
Access specifiers in a class An access specifier is a keyword in OO
Programming languages, which specify what access is permitted
on a member.
There are three types of access specifiers:
1. public: Accessible to all. Other objects can also access this
member variable or function.
2. private: Not accessible by other objects. Private members can be
accessed only by the methods in the same class. Object accessible
only in Class in which they are declared.
3. protected: The scope of a protected variable is within the class
which declares it and in the class which inherits from the class
(Scope is Class and subclass) Default: Scope is Package Level
Approach to Object Oriented Design
Step 1. Identify all the classes (Nouns; Type of objects) in the requirement
specification,
Step 2. Identify the commonalities between all or small groups of classes
identified (generalization) if it is obvious. Do not force fit generalization where
it doesn’t make sense.
Step 3. In any given situation, start with the simplest object which can be
abstracted into individual classes.
Step 4. Identify all the member variables and methods the class should
have.
Step 5. Ensure that the class is fully independent of other classes and
contains all the attributes and methods necessary.
Step 6. Keep all the data members private or protected.
Step 7. The methods in the class should completely abstract the
functionality .
Step 8. The methods in the class should not be a force fit of procedural code into a
class .
Step 9. Inherit and extend classes from the base classes ONLY IF the
situation has scope for it