Escolar Documentos
Profissional Documentos
Cultura Documentos
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:
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
}
OOP vs POP
LESSON 3
TOPIC
OOP vs POP
TOPICS
OOP vs POP
THANK YOU!
NEXT >>> JAVA FUNDAMENTALS