Você está na página 1de 42

CHAPTER 1:

INTRODUCTION
TO OOP
Lectured by:
VILCHOR G. PERDIDO
(Course Instructor)
Topics Covered
01 Overview of Object-
Oriented Programming

02
Principles of OOP

03
OOP vs POP
Learning Outcomes
At the end of this chapter, you will be able to:

Learn the Identify the difference


concepts of OOP, between Object-
how it works, and Oriented and Procedure-
benefits. Oriented programming.

Learn the different Determine some


foundations in object- object-oriented
oriented programming programming
and their concepts and languages compared
other terminologies. to other languages.
OVERVIEW
LESSON 1
TOPICS
OVERVIEW OF OOP

OOP HOW IT BENEFITS


CONCEPT WORKS?
What is OOP?
» a programming paradigm or style that
uses objects in programming
– aims to implement real-world entities like inheritance,
data-hiding, polymorphism etc…
» a software design method that models the
characteristics of real or abstract objects
– an object is a software bundle of
related fields (variables) and methods
What is OOP?
» a programming
language model in
which programs are
organized around data
or objects, rather than
object can be defined within a functions and logic
class as a data field that has
unique attributes or properties
and behavior or methods
How OOP works?

Identify all of the Group the Define the kind of Implement


objects and their objects and data (properties) message passing
relationship generalized as and logic (object can
(data modeling). a class. sequences communicate
(procedures or methods through
methods) messages).
How OOP works?
How OOP works?
The object's interface consists of a set
of commands, each command
performing a specific action. An object
asks another object to perform an
action by sending it a message. The
requesting (sending) object is referred
to as sender and the receiving object
is referred to as receiver.

Control is given to the


receiving object until it
completes the command;
control then returns to the
sending object.
OOP Benefits or Advantages
It provides a clear modular structure for programs which
01 makes it good for defining abstract datatypes in which
implementation details are hidden
02 Objects can also be reused within an across applications.

03 It makes software easier to maintain.

04 Reuse also enables faster development.

It provides a good framework for code libraries where the


05
supplied software components can be easily adapted and
modified by the programmer.
OOP
PRINCIPLES
LESSON 2
TOPICS
OOP CONCEPTS
A. Object
» refer as the basic unit that represents
the real life entities
– entity that has state and behavior (physical or
logical)
– Example: Dog (states: breed, age, color) (behaviors:
bark, sleep, eat)
» defined as an instance of a class
Class Object
Object
» An object consists of:
– State : It is represented by attributes or
variables of an object. It also reflects the
properties of an object.
– Behavior: It is represented by methods of an
object. It also reflects the response of an
object with other objects.
– Identity: It gives a unique name to an object
and enables one object to interact with other
objects.
Objects use Methods
» a collection of statements that perform
some specific task and return result to
the caller
– can perform some specific task without returning
anything
» allow to reuse the code without retyping
the code
Method Declaration
» In general, method declarations has six
components:
Method Declaration

1. Access Modifier: Defines access type of


the method i.e. from where it can be accessed
in your application
– public: accessible in all class in your application.
– private: accessible only within the class in which it is defined.
– protected: accessible within the package
– default: accessible within same class and package within
which its class is defined
Method Declaration
2. The return type
– the data type of the value returned by the method or
void if does not return a value.

3. Method Name
– the rules for field names apply to method names as
well, but the convention is a little different
Method Declaration
4. Parameter list
– comma separated list of the input parameters are
defined, preceded with their data type, within the
enclosed parenthesis

– if there are no
parameters, you
must use empty
parentheses ()
Method Declaration
5. Exception list
– the exceptions you expect by
the method can throw, you can
specify these exception(s)
6. Method body
– it is enclosed between braces
– the code you need to be
executed to perform your
intended operations
Object sends Messages
» objects communicate with one another by
sending and receiving information to
each other known as message passing
– it involves specifying the name of the object, the
name of the function, and the information to be
sent
Object sends Messages
» a message can also contain information
the sending objects needs to pass to the
receiving object, called the argument

student.setName(“Billy”)
B. Class
» a collection of objects
» a user-defined blueprint or prototype
from which objects are created
» represents the set of properties or
methods that are common to all objects
of one type
Class
» has components:
– Modifiers: A class can be public or has default access
– Class name: Should begin with an initial letter.
– Superclass (if any): The name of the class’s parent
(superclass), if any, preceded by the keyword extends.
– Interfaces (if any): A comma-separated list of
interfaces implemented by the class, if any, preceded
by the keyword implements.
– Body: The class body surrounded by braces, { }.
Class
Example:
public class ClassB extends Class A implements
Interface {
private int varX;
private void getValueX () {

}
}
Class
» Can be represented through a
compartmented rectangle
Class Name Class B

Attributes/Properties/Variables varX

Methods/Behaviors getValueX()
C. Inheritance
» an important pillar in OOP which is
mechanism that allows one class to
inherit the features (fields and methods)
of another class
– one child object acquires all the properties and
behaviors of a parent object
extends
Parent class Child class
Inheritance (terminologies)
» Superclass
– the class whose features are inherited (a base class
or a parent class).
» Subclass
– the class that inherits the other (a derived class,
extended class, or child class)
– can add its own fields and methods in addition to the
superclass fields and methods
Inheritance (terminologies)
» Reusability
– supports the concept of “reusability”
– i.e. when we want to create a new class and there is
already a class that includes some of the code that
we want, we can derive our new class from the
existing class
class subclass extends superclass {
Syntax: //fields or variables
//methods
- used extends }
Inheritance (example)
public class A { Output:
public void fun1(int x){
System.out.println("Int in A is: " + x);
}
Int in A is: 6
} Int in B is: 2 and 5
class B extends A {
public void fun2(int x,int y) {
fun1(6);
System.out.println("Int in A is: " + x " and " + y);
}
public static void main(String[] args){
B obj= new B();
obj.fun2(2,5);
}
}
D. Polymorphism
» refers to the ability to
differentiate between
entities with the same
name efficiently
– a way that provide the
different functionality by the
functions having the same
name based on the
signatures of the methods
Polymorphism
Two types:
» Method overloading
– having multiple methods with same name but with
different signature (number, type, order of parameters)
public class A {
public void fun1(int x) {
System.out.println("The value of x is : " + x);
}
public void fun1(int x, int y) {
System.out.println("The value of x and y : " + x + " and " + y);
}
}
Polymorphism
Two types:
» Method overriding
– when a subclass contains a method with the same
name and signature as in the super class

public class A { class B extends A {


public void fun1(int x) { public void fun1(int x) {
System.out.println("Class A is: "+ x); System.out.println("Class B is: "+ x);
} }
}
E. Abstraction
» is the property by virtue of which only the
essential details are displayed to the user
– Example: A car is viewed as a car rather than its
individual components.
» defined as the process of identifying only
the required characteristics of an object
ignoring the irrelevant details
– abstraction is achieved by interfaces and abstract classes
F. Encapsulation

» the wrapping up of data under a single unit


» a mechanism that binds together code
and the data it manipulates

» a protective shield that prevents the data


from being accessed by the code outside
this shield
Encapsulation
» the variables or data of a class is hidden
from any other class and can be accessed
only through any member function of own
class in which they are declared
– the data in a class is hidden from other classes, so it is
also known as data-hiding
– can be achieved by declaring all the variables in the
class as private and writing public methods in the
class to set and get the values of variables
Encapsulation
public class A {

public void setWidth(double w) {


width = w;
}

public double getWidth() {


return w;
}

}
OOP vs POP
LESSON 3
TOPIC
OOP vs POP
TOPICS
OOP vs POP
THANK YOU!
NEXT >>> JAVA FUNDAMENTALS

Você também pode gostar