0% found this document useful (0 votes)
36 views9 pages

Oop L8

This document provides an overview of object-oriented programming concepts including accessors and mutators, arrays, and examples. It defines accessors as getter methods that return private fields, and mutators as setter methods that set private fields. It discusses array declaration and syntax, noting that arrays are objects that hold a fixed number of values of a single type. Examples of declaring and using arrays of different data types are also provided.

Uploaded by

RollingRage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views9 pages

Oop L8

This document provides an overview of object-oriented programming concepts including accessors and mutators, arrays, and examples. It defines accessors as getter methods that return private fields, and mutators as setter methods that set private fields. It discusses array declaration and syntax, noting that arrays are objects that hold a fixed number of values of a single type. Examples of declaring and using arrays of different data types are also provided.

Uploaded by

RollingRage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Object Oriented

Programming

BITS Pilani
CS F213
Dubai Campus
Contents
• Accessor and Mutator
• Examples
• Arrays
• Introduction
• Declaration
• Example

Sources:
1. Chapter 1, Cay Horstmann, Object Oriented Design & Patterns, John Wiley & Sons, 2006, 2nd Edition
2. Chapter 3, 13, Herbert Schildt, The complete Reference Java 2, 5th Edition, Tata McGraw Hill.
3. https://www.c-sharpcorner.com/UploadFile/3614a6/accessors-and-mutators-in-java/
4. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
5. https://www.geeksforgeeks.org/arrays-in-java/

BITS Pilani, Dubai Campus


Accessor
• An Accessor method is commonly known as a get method or
simply a getter.
• A property of the object is returned by the accessor method.
• They are declared as public. A naming scheme is followed by
accessors, in other words they add a word to get in the start
of the method name.
• They are used to return the value of a private field.
• The same data type is returned by these methods depending
on their private field.

BITS Pilani, Dubai Campus


Accessor
Syntax Example

public return typr getName() public class Employee


{ {
private int number;
return privateVar;
public int getNumber()
}
{
return number;
}
public void setNumber(int
newNumber)
{
number = newNumber;
}
}
BITS Pilani, Dubai Campus
Mutators
• A Mutator method is commonly known as a set method
or simply a setter.
• A Mutator method mutates things, in other words
change things. It shows us the principle of
encapsulation.
• They are also known as modifiers.
• They are easily spotted because they started with the
word set.
• They are declared as public.
• Mutator methods do not have any return type and they
also accept a parameter of the same data type
depending on their private field.
• After that it is used to set the value of the private field.
BITS Pilani, Dubai Campus
Mutators
Syntax Example

public void setName(int var1) public class Cat


{ {
this.var1 = var1; private int Age;
} public int getAge()
{
return this.Age;
}
public void set Age(int Age)
{
this.Age = Age;
}
}
BITS Pilani, Dubai Campus
Array
• An array is a container object that holds a fixed number of values
of a single type.
• The length of an array is established when the array is created.
• In Java all arrays are dynamically allocated.
• Since arrays are objects in Java, we can find their length using
member length.
• Java array can be also be used as a static field, a local variable or
a method parameter.
• Example: ArrayDemo.java

BITS Pilani, Dubai Campus


Declaring Array
• We can declare arrays of other types:
• byte[] anArrayOfBytes;
• short[] anArrayOfShorts;
• long[] anArrayOfLongs;
• float[] anArrayOfFloats;
• double[] anArrayOfDoubles;
• boolean[] anArrayOfBooleans;
• char[] anArrayOfChars;
• String[] anArrayOfStrings;
• You can also place the brackets after the array's name:
• // this form is discouraged
float anArrayOfFloats[];
• Convention discourages this form; the brackets identify the
array type and should appear with the type designation.

BITS Pilani, Dubai Campus


BITS Pilani
Dubai Campus

Thank You!

You might also like