OBJECT-ORIENTED PROGRAMMING
PRINCIPLES
Object Oriented Programming
Object-oriented programming (OOP) is a programming paradigm that
represents the concept of objects having data fields (attributes that
describe the object) and associated procedures known as methods.
Objects, which are usually instances of classes, are used to interact
with one another to design applications and computer programs.
C++, Objective-C, Smalltalk, Delphi, Java, Javascript, C#, Perl,
Python, Ruby and PHP are few example of Programming languages
that employs OOP.
Four Main OOP Principles
Ability or technique of making the fields in a class
ENCAPSULATION
private and providing access to the fields via public
methods.
Process where one object acquires the properties of
INHERITANCE
DATA
ABSTRACTION
another. Information is made manageable in a
hierarchical order.
Ability to make a class abstract, one that cannot be
instantiated.
Ability of an object to take on many forms, such that
POLYMORPHISM
it can be used by different objects.
Encapsulation
Described as a protective barrier that prevents the code and data
being randomly accessed by other code defined outside the class.
Access to the data and code is tightly controlled by an interface.
Major benefit is the ability to modify the implemented code without
breaking the code of others who utilizes the implemented code.
Encapsulation is also known as information hiding.
Encapsulation
Usually done by hiding attributes (fields) of an object and only
exposing them via getter and setter methods.
Getter or Accessors method that used to return the value of a
private
field with a naming scheme prefixing the word get to the
start of the
method name.
Setters or Mutators - method is used to set a value of a private field
with a
method
naming scheme prefixing the word set to the start of the
name.
Encapsulation (illustration)
field1
fieldn
inputMethod2
()
(
od
field2
h
et
ge
() tMe
th
o
tM
se
output
(Data)
inputMethod1
()
input
Polymorphism
Polymorphism is an OOP principle that allows to define one interface
and can have multiple implementations.
A feature that allows one interface to be used for a general class of
actions.
Polymorphism can be achieved in two types:
(a) Static Polymorphism (Compile-time Polymorphism)
(b)Dynamic Polymorphism (Runtime Polymorphism)
Polymorphism (illustration)
SUBCLASS1
SUBCLASS2
Methods and Constructors of subclasses
define their own unique behaviors and yet
SUPERCLASS
share some of the same functionality of the
super class.
Method Overloading
Method Overloading is a feature of OOP that allows a class to have
two or more methods having same name with different argument or
parameter lists.
Method overloading can be done by changing the number of
arguments or changing the data types of the arguments.
Method overloading is one of the ways through which java supports
polymorphism.
Method overloading is also known as Static Polymorphism.
Method Overloading (example)
Create object reference for
OverloadingMethods Class
Assign and invoke
multiplyNumbers method with
different parameters to a
variable.
Method Overloading (example)
Overloaded method with different
parameters
Base Class and Derived Class Concepts
Derived Class is a class that is derived from another class.
Derived class is also referred as child class or subclass.
Derived Class inherits state and behavior in the form of variables and
methods from its base class (inheritance).
Base Class is a class from which states and behaviors of the other
classes have been derived or the derived class.
Base Class is also known as parent class or superclass.
Base Class and Derived Class Concepts
Base Class
field1
field2
inherited
field
Derived Class
fielda
field1
fieldn
inherited
fieldn
methodA( )
methodB( )
methodN()
methods
methodA( )
methodB( )
methodC( )
methodN()
Method Overriding
Method Overloading occurs when a method in derived class has the
same name and type signature as a method in its base class.
Derived class overrides the parent class method without even
touching the source code of the base class and creates its own
functionality.
Override methods must have the same return type and must not
have more restrictive access modifier.
Overriding Method is also known as Dynamic Polymorphism.
Method Overriding (example)
Reference of SuperClass and
object for SuperClass
Reference of SuperClass but
object of SubClass
Method Overriding (example)
Override
Method
Constructor
Constructor is a block of code, which runs when you use new keyword
in order to instantiate an object.
Constructor has the same name as its class and is syntactically
similar to a method.
Constructors have no explicit return type nor void.
Constructor is used to initialize values to the instance variables
defined by the class, or to perform any other startup procedures
required to create a fully formed object.
Constructor
Java automatically provides a default constructor that initializes all
member variables to zero.
Once you define your own constructor, the default constructor is no
longer used.
Polymorphism is also achieved through constructor overloading.
You make use of constructor chaining, wherein one constructor calls
the constructor of its super class implicitly or explicitly.
Shadowing and this Keyword
Attributes/Fields/Variables identifiers can be the same as long as the
scope is different.
Java performs a lookup based of the variables within and outside the
method but within the class.
If the same variable name exist in the class, the local variable shadows the
class/instance variable. this Keyword is used to refer the class and instance
variable.
Constructor (example)
Constructor with
parameter
Mutator and Accessor for field name
Reference with
argument
Overload
Constructor
Inheritance
Reusing the fields and methods of the existing
class without having to write (and debug!) them
SUPERCLASS
yourself.
A subclasses inherit all the members from its
superclass.
Constructor of the superclass can be invoked from
the subclass, except for the constructor of the
subclasses.
SUBCLASS
Inheritance (example)
MountainBike (subclass) inherits all the fields and methods of Bicycle
and adds the field seatHeight and a method to set it (mutator).
Except for the constructor, it is as if you had written a new
MountainBike class entirely from scratch, with four fields and five
methods.
A subclass inherits all of the public and protected members of its
parent except for the private members.
Keywords super, extends, & Multiple
Inheritance
super keyword is used to refer immediate parent class instance
variable.
super keyword is used to invoke immediate parent class constructor.
super keyword is used to invoke immediate parent class method.
extends keyword is used to indicate that you are making a new class
(subclass) that derives from an existing class (superclass).
Java disallow a class extends multiple classes (multiple inheritance) to
reduce the complexity and simplify the language
Data Abstraction
Abstraction can be achieved in two ways (1) Abstract Classes and (2)
Interfaces.
An abstract class is a class that cannot be instantiated. Members of the
abstract class still exists and can be accessed in the same manner with
a regular class.
An abstract method is a method that is declared without an
implementation (without braces, and followed by a semicolon) like
abstract void methodName();
Interfaces vs. Abstract Classes
Abstract classes can declare fields that are not static and final, and
define public, protected, and private concrete methods while;
With Interfaces, all fields are automatically public, static, and final, and
all methods that you declare (as default methods) are public.
In addition, you can extends only one class, whether or not it is
abstract, whereas you can implement any number of interfaces.
If class include abstract methods, then the class itself must be
declared abstract.
Data Abstraction (Abstract Class Example)
Data Abstraction (Interface Example)
Interface & Abstract Class (Example)
implements keyword requires that the class being defined also include
declarations and definitions of all the method signatures defined in the
specified interface class.
implements keyword allows you to have multiple inheritance while
extends keyword does not.
Implementing an interface class may think of the class as signing a
contract, agreeing to perform the specific behaviors of the interface, if
not, the class must declare itself as abstract.