Professional Documents
Culture Documents
Unit 4: Methods and Constructors
Unit 4: Methods and Constructors
Unit 4
Success Criteria
By the end of this Unit, you should be able to:
define a method
describe the components of a method
state types of methods in Java
implement methods using Java
explain methods overloading and overriding
describe the Java constructor
state types of Java constructors
state the difference between Java methods and
constructors
Key Words
You will find the following key words or phrases in this
unit: method, constructor, method overloading,
access modifiers and method overriding. Watch for
these and make sure that you understand what they
mean and how they are used in the unit.
Java Methods
In Unit 2, you were introduced to methods. In Java, a
method is defined as a block of code or collection of
statements or a set of code grouped together to
perform a certain task or operation (JavaTPoint, n. d).
A method can be invoked at any point in the program.
This is done by utilizing the name of the method.
From the above examples, you will notice that after the
method’s name, there is a pair of parentheses, (),
followed by braces {}.
Components of a Method
// method body
System.out.println(num+" is even");
else
System.out.println(num+" is odd");
}
From the above example, you should note that a
method called findEvenOdd()is defined. This method
has a parameter num of type int.
The method body contains the steps to check whether
the number is even or odd. If the number is even, it
prints the number is even, else prints the number is
odd.
Static methods
Notice that you can use static or non-static methods in
both types of Java methods.
A static method has static keyword. It is a method
that belongs to a class rather than an instance of a
class.
The following is an example of static method:
}
Characteristics of static methods
Aravindan (n. d), explains that a static method can
access static methods and variables as follows:
1. A static method can call only other static
methods; it cannot call a non-static method
2. A static method can be called directly from the
class, without having to create an instance of
the class
3. A static method can only access static
variables; it cannot access instance variables
4. Since the static method refers to the class, the
syntax to call or refer to a static method is:
class name.method name
5. To access a non-static method from a static
method, create an instance of the class
Instance Methods
These are methods that require an object of its class to
be created before calling it. They are also called non-
static methods.
Below is an example of instance method:
public void example(String random) {
}
You must take note that the void keyword allows us to
create methods which do not return a value.
Characteristics of Non-Static Methods
Aravindan (n. d), explains that a non-static method in
Java can access static methods and variables as
follows:
1. A non-static method can access any static
method without creating an instance of the class
2. A non-static method can access any static
variable without creating an instance of the class
because the static variable belongs to the class
}
}
class NalikuleCampus
{
public void display()
{
System.out.println("I am non-static
method");
}
public static void main(String agrs[])
{
NalikuleCampus obj=new
NalikuleCampus();
obj.display();
}
If you try to access it directly like this: display() then
you will get compilation error.
Method Overloading
You should take note that method overloading occurs
when a class has two or more methods by the same
name but different parameters (Hejazi, 2019).
Example (Copy this code and run it in your IDE):
public class Sum {
static int sum(int x, int y) {
return x + y;
}
class Plants
{
class Car
{
static int add(int a, int b)
{
return a+b;
}
static double add(double a, double b)
{
return a+b;
}
}
class Car2
{
public static void main(String[] args)
{
System.out.println(Sum.add(17,13));
System.out.println(Sum.add(10.4,10.6));
}
}
Method overriding
class People{
//Overridden method
public void eat()
{
System.out.println("People are
eating");
}
}
class Girl extends People{
//Overriding method
public void eat(){
super.eat();
System.out.println("Girl is eating");
}
public static void main( String args[]) {
Girl obj = new Girl();
//This will call the child class
version of eat()
obj.eat();
}
}
class OOP{
public void myMethod()
{
System.out.println("Overridden method");
}
}
class Domcol extends OOP{
public void myMethod(){
//This will call the myMethod() of
parent class
super.myMethod();
System.out.println("Overriding method");
}
public static void main( String args[]) {
Domcol obj = new Domcol();
obj.myMethod();
}
}
Practise Activity
A nonnegative integer is called a palindrome if it reads
forward and backward in the same way. For example, the
numbers 5, 121, 3443, and 123454321 are palindromes.
Write a method that takes as input a nonnegative integer
and returns true if the number is a palindrome; otherwise,
it returns false. Also write a program to test your method.
Java Constructors
A constructor in Java is considered as a special
method that is used to initialize objects. You should
note that every time an object is created using the
new() keyword, at least one constructor is called.
Constructor name must match the class name, and it
cannot have a return type (like void).
Look at the example below:
// Create a Main class
public class Domcol {
int x; // Create a class attribute
Types of Constructors
Parameterized constructor
It is a constructor which has a specific number of
parameters.
Example:
Practise Activity
Write a program to print the names of students by
creating a Student class. If no name is passed while
creating an object of Student class, then the name
should be "Unknown", otherwise the name should be
equal to the String value passed while creating object
of Student class.
Summary
In this unit, you have been introduced to methods and
constructors.
Reflection
Which part of this unit have you found so challenging?
Write a brief summary of what you have learned in this
unit and how it will impact your skills in programming.
Unit 4 Test
1. Why constructors in Java cannot be static?
import java.util.Scanner;