OOP 8 - Object-Oriented Programming Principles
OOP 8 - Object-Oriented Programming Principles
PRINCIPLES
INHERITANCE
DATA
ABSTRACTION
POLYMORPHISM
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
start of the
method 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
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.
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.
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.
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.
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();