Presentation 3
Presentation 3
Object
Oriented
Programming
Dr. Abdulaziz Saleh Algablan
[email protected] :Email
2024
Outline
• Defining and Using Classes
• Defining and Using Multiple Classes
• Matching Arguments and Parameters
• Passing Objects to a Method
• UML Class Diagram
• Methods
• Constructors
• Access Modifiers
• This keyword
• Software reusability
Classes and Objects
• In the Java, we must provide a definition, called a class to be able to
create an object.
• A class is a kind of mold or template that dictates what objects can
and cannot do.
• An object is called an instance of a class.
• An object is an instance of exactly one class.
• An object is comprised of
• data (attributes)
• operations (methods) that manipulate these data.
• A program written in object-oriented style will consist of interacting
objects.
UML Class Diagram Notation
• UML stands for Unified Modeling
Language.
• The top section is used to name the
class.
• The second one is used to show the
attributes of the class.
• The third section is used to describe the
operations (methods) performed by the
class.
• The fourth section is optional to show
any additional components.
UML Class Diagram
public class Book { public class Person {
//Returns the name of this bicycle's owner public void setSizeInInch(int aNumber) {
card1.checkOut(3);
card2 = new LibraryCard( );
card2.setOwner(student); //the same student is the owner
//of the second card, too
System.out.println("Card1 Info:"); System.out.println(card1.toString() + "\n");
System.out.println("Card2 Info:");
System.out.println(card2.toString() + "\n"); }
}
.Passing Objects to a Method – cont
Remember
• When we pass an object to a method, we are actually passing the
address, or reference, of an object to the method.
• It is possible to return the Student object itself by defining the
following method in the LibraryCard class:
public Student getOwner( ) {
return owner;
}
Program Modules in Java
• Java programs are written by combining new methods and classes
that you write + predefined ones available in the Java Application
Programming Interface (API) and in various other class libraries.
• Related classes are typically grouped into packages so that they can
be imported into programs and reused
• package package_name;
• The Java API provides a rich collection of predefined classes.
• import java.util.Scanner;
Package and Access Modifiers
• Any Java members such as class or methods or data members when not
specified with any access modifier they are by default considered as
default access modifiers.
• These methods or data members are only accessible within the same
package and they cannot be accessed from outside the package.
• Package-level accessibility provides more visibility than a private access
modifier. But this access modifier is more restricted than protected and
public access modifiers.
• we conclude that the default access modifier members can be accessed
only within the same package and cannot be accessed from outside the
package.
Declaring Methods
• Generally, method declarations have six components:
• Modifiers—such as public, private, and others.
• Return type—the data type of the value returned by the method, or void if
the method does not return a value.
• Method name—the rules for field names apply to method names as well, but
the convention is a little different.
• Parameter list in parenthesis—a comma-delimited list of input parameters,
preceded by their data types, enclosed by parentheses, (). If there are no
parameters, you must use empty parentheses.
• An exception list—to be discussed later.
• Method body, enclosed between braces—the method's code, including the
declaration of local variables, goes here.
Methods
• You can send a message only to the classes and objects that
understand the message you send.
• For a class or an object to process the message it receives, it must
possess a matching method.
• A method is a sequence of instructions that a class or an object
follows to perform a task.
• A method defined for a class is called a class method, and a method
defined for an object is an instance method.
• A value passed to an object is called an argument of a message
Access Modifiers
• In Java, methods and data members can be encapsulated by the
following four access modifiers:
1. private (accessible within the class where defined)
2. default or package-private
i. when no access modifier is specified
ii. Allow package level access
3. protected (accessible only to classes that subclass your class directly within
the current or different package)
4. public (accessible from any class)
Instance method vs Static method
• A method defined for a class is called a class method or a static
method.
• Static methods:
• can access the static variables and static methods directly.
• can’t access instance methods and instance variables directly.
• A method defined for an object is an instance method.
• Instance method
• Access the instance methods and instance variables directly.
• Access static variables and static methods directly.
When to Use Static Methods?
• Using static methods makes sense when we are developing methods
with standard behavior that operates on input arguments.
• Declaring main as static allows the JVM to invoke main without
creating an object of the class.
• Static keyword allows methods to be accessed via the class name
Math and a dot (.) separator.
Calling Static Methods & Static
Fields
• Cass’s static methods and static field are called by specifying the name
of the class in which the method is declared, followed by a dot (.) and
the method/field name.
• Math.max(55, 1.6666);
Static method
• Example of standard behaviour that operates on their input
arguments
Overloading Methods
• Java supports overloading of methods.
• Java can distinguish between methods with
different method signatures.
• This means that methods within a class can
have the same name if they have different
parameter lists.
• For Example: draw method is overloaded in
the DataArtist class.
Overloading Methods
• The order of parameter types cause a new version
• This is fine, for example:
void aMethod(){ }
void aMethod(int a ){ }
void aMethod(double a ){ }
void aMethod(int a, double d ){ }
void aMethod(double a, int d ){ }
Overloading Methods
• Methods cannot be overloaded by changing their return type.
• For example:
String printMe()
{
return ”I am a String";
}
int printMe()
{
return 0;
}
• This causes a compilation error.
Overloading Methods
• Its fine when the return type and a parameter has been changed.
• For example:
String printMe()
{
return ”I am a String";
}
int printMe(int a)
{
return 0;
}
Method-Call Stack and Activation
Records
• A call to method creates a stack memory for the method
• Stack follows Last-in, first-out (LIFO) data structures
• The first item is pushed down to the bottom of the stack.
• The last item pushed onto the top of the stack.
• the last item is the first item popped from the stack.
x = 10
}
main Stack
Method-Call Stack and Activation
Records
static int getANumber( )
{ int a =100;
return a;
}
public static void main(String [] arg)
{
int x = 10;
int y = x;
int w = getANumber();
y = 10
x = 10
}
main Stack
Method-Call Stack and Activation
Records
static int getANumber( )
{ int a =100;
return a;
}
Stack
public static void main(String [] arg) created
{
int x = 10;
int y = x; w=
int w = getANumber(); y = 10
x = 10
} }
;int aNumber = 10
;protected int bNumber
;int cNumber
} }
Scope of Declarations
{ public class ClassA { public class Test
;aNumber = 30 ;System.out.println(a1.aNumber)
;int aNumber = a }
}
;int aNumber = 10
;protected int bNumber
;int cNumber
} }
Scope of Declarations
Scope of Declarations
Constructors
• A constructor in Java is a special method that is used to initialize
objects.
• The constructor is called when an object of a class is created.
• It can be used to set initial values for object attributes.
• It is possible to define more than one constructor to a class.
• Multiple contractors are called overloaded constructors.
• It is a good idea to define multiple constructors to a class.
Constructors
• Note: It is not necessary to write a constructor for a class.
• Java compiler creates a default constructor (constructor with no-
arguments) if your class doesn’t have any.
Constructors vs. Methods
• Constructors must have the same name as the class within which it is
defined it is not necessary for the method.
• Constructors do not return any type while method(s) have the return
type or void if does not return any value.
• Constructors are called only once at the time of object creation while
method(s) can be called any number of times.
Usage of java this keyword
• Every object can access a reference to itself with keyword this
(sometimes called the this reference).
z
Usage of java this keyword
• A static block helps to initialize the static data members, just like
constructors help to initialize instance members.