Module 4 Finale
Module 4 Finale
Learning Outcomes
Define and distinguish the differences of procedural/ functional
approach and object-oriented approach
Define and discuss object and methods.
Create member functions/methods for the class.
Construct Java programs using Objects and Classes platform.
Evaluate the output of code fragments having member function
and access modifier
ASSESSMENT TASK
1. What is procedural programming?
2. What is object oriented programming?
3. What is object and member function?
Overview
In this chapter the students will learn to distinguish the differences of
procedural programming and object oriented programming. They will learn
how to create program member functions and access modifier.
Learning Content
39
Module IV Object Oriented Approach
Defining a Class
where,
● public - means that our class is accessible to other classes outside the
package
● class - this is the keyword used to create a class in Java
● StudentRecord - a unique identifier that describes our class
Object
An object is an instance of a class having states and behaviors. Example: A
monkey has states - color, name, breed as well as behaviors - climbing,
eating, sleeping.
A sample of a class is given below:
class monkey {
String breed;
int age;
String color;
void climbing()
{
Statement;
}
void eating()
{
Statement;
40
Module IV Object Oriented Approach
}
void sleeping()
{
Statement;
} }
Instance Variables
public class StudentRecord
{
private String name;
private String address;
private int age;
private double mathGrade;
private double englishGrade;
private double scienceGrade;
private double average;
//we'll add more code here later
}
where, private here means that the variables are only accessible within the
class. Other objects cannot access these variables directly. We will cover
more about accessibility later.
Method Calling
Two ways to call a method:
- method returns a value
- Returning nothing (no return value).
42
Module IV Object Oriented Approach
Accessor methods
– used to read values from our class variables (instance/static).
– usually written as:
get<NameOfInstanceVariable>
– It also returns a value.
public class StudentRecord {
private String name;
:
public String getName(){
return name;
}
}
where,
● public - means that the method can be called from objects outside
the class
43
Module IV Object Oriented Approach
● String - is the return type of the method. This means that the
method should
return a value of type String
● getName - the name of the method
● () - this means that our method does not have any parameters
Example
public class StudentRecord
{
private String name;
:
public double getAverage()
{
double result = 0;
result=(mathGrade+englishGrade+scienceGrade)/ 3;
return result;
}
}
Static methods
public class StudentRecord
{
private static int studentCount;
public static int getStudentCount()
{
return studentCount;
}
}
– where,
● public- means that the method can be called from objects outside
the class
● static-means that the method is static and should be called by
typing,
[ClassName].[methodName].
● int- is the return type of the method. This means that the method
should return a
value of type int
● getStudentCount- the name of the method
● ()- this means that our method does not have any parameters
Void Keyword
The void keyword allows us to create methods which do not return a value.
44
Module IV Object Oriented Approach
Below is an example of a void method pro gram which does not return any value.
class gradesRank
{
public static void graRank (double grade)
{
if (grade >= 1.50)
{
System.out.println("Rank: 1");
}
else if (grade >= 1.25)
{ System.out.println("Rank: 2");
}
else
{ System.out.println("Rank: 3");
}
}
public static void main(String[] args)
{ graRank(1.75);
}
} }
Example:
The value of the arguments remains the same even after the method
invocation.
public class switchValue
{
public static void switchMethod(int num1, int num2)
{
System.out.println("Before switching,the number 1 is:"+num1
+"number2="+num2);
// Switch number 1 with number 2
int three = num1;
num1 = num2;
num2 = three;
System.out.println("After switching the number 1 is: “+num1+"number2
="+num2);
}
45
Module IV Object Oriented Approach
Program Output
Before switching, number1= 30 and number2=45
Before switching, the number 1 is:30 number2=45
After switching the number 1 is: 45 number2=30
Before and after Switching of two numbers would be the same”
After switching, number1=30 and number2 is: 45
Creating an Object
A class provides the blueprints for objects. An object is created from a class.
In Java, the new key word is used to create new objects.
Three steps on how to create an object from a class
Declaration: A variable declaration with a variable name with an
object type.
Instantiation: The 'new' key word is used to create the object.
Initialization: The 'new' keyword is followed by a call to a
constructor. This call initializes the new object.
class dog
{
public dog (String name){
// This constructor has one parameter, name.
System.out.println(" the dog name is :" + name );
}
46
Module IV Object Oriented Approach
47
Module IV Object Oriented Approach
JAVA OBJECTS
The Object Class is the super class for all classes in Java.
Some of the object class methods are:
- Equals
- toString()
- wait()
- notify()
- notifyAll()
- hashcode()
- clone()
Object is an instance of a class created using a new operator. The new
operator returns a reference to a new instance of a class. This reference can
be assigned to a reference variable of the class. The process of creating
objects from a class is called instantiation. An object encapsulates state and
behavior.
Example:
public class Cube
{
int length = 10;
int breadth = 10;
int height = 10;
public int getVolume()
{
return (length * breadth * height);
}
public static void main(String[] args)
{
Cube cubeObj; // Creates a Cube Reference
cubeObj = new Cube(); // Creates an Object of Cube
System.out.println("Volume of Cube is : " +
cubeObj.getVolume());
} }
Abstract classes are classes that contain one or more abstract methods. An
abstract method is a method that is declared, but contains no
48
Module IV Object Oriented Approach
49
Module IV Object Oriented Approach
o You expect that classes that extend your abstract class have
many common methods or fields, or require access modifiers
other than public (such as protected and private).
o You want to declare non-static or non-final fields. This enables
you to define methods that can access and modify the state of
the object to which they belong.
Using interfaces
o You expect that unrelated classes would implement your
interface. For example, the interfaces Comparable and Clone
able are implemented by many unrelated classes.
o You want to specify the behavior of a particular data type, but
not concerned about who implements its behavior.
o You want to take advantage of multiple inheritance of type.
INTERFACES
What is an interface?
Interface looks like class but it is not a class. An interface can have methods
and variables just like the class but the methods declared in interface are by
default abstract (only method signature, no body). Also, the variables
declared in an interface are public, static & final by default. We will discuss
these points in detail, later in this post.
50
Module IV Object Oriented Approach
Sample Program
interface SampleInterface
{
public void FirstMethod();
public void SecondMethod();
}
class mySampleInterface implements SampleInterface
{
public void FirstMethod ()
{
System.out.println("Welcome to Java Classes!");
}
public void SecondMethod()
{
System.out.println("Implementation of Interfaces in Java
Classes");
}
public static void main(String arg[])
{
SampleInterface ointer = new mySampleInterface ();
ointer.FirstMethod();
ointer.SecondMethod();
} }
51
Module IV Object Oriented Approach
Now you will learn how to create your own methods with or without return
values, invoke a method with or without parameters, and apply method
abstraction in the program design.
Exercises
1. Tracing an Error: Trace the program below and determine the error.
public class sAMPLEprogram {
public static void main(String[] args)
{
Square mysQ;
mysQ.height = 25;
mysQ.width = 25;
System.out.println("the area of square is " + mysQ.area());
}
}
52
Module IV Object Oriented Approach
References
Singh, Chaitanya. BeginnersBook. 2019. Retrieved from
https://beginnersbook.com/2013/05/method-overloading/
53