OOP Session 2 PT 1 Slides
OOP Session 2 PT 1 Slides
OOP Session 2 PT 1 Slides
Objectives
In this Chapter you will learn to:
Create a Class in Java
Define a Constructor
Use access Specifiers and Modifiers
Declare and define Classes and Interfaces
A class is just a platform that we use to create objects in Java and we have some rules that
we need to follow when coming up with a class name like the class name should start with
an alphabet letter not a number and the first letter should be capitalized and the name
should be a noun, for example
public class Student{
}
The code in a class should be put inside the curly brackets as shown above, the first
bracket is the opening and the last is the one that closes the class.
Let us consider this example to help us come up with a class.
Declare and define Classes and Interfaces
void calculate();
void average();
Adding method to a class
A method is just a group of statements that perform a specific task and the method name should use a verb. Let us come
up with a method example
public class Master {
public void Master() {
String name = “James”;
int age = 60;
double salary = 789.70;
System.out.println(“ My name is “ + name);
System.out.println(“ My age is “ + age);
System.out.println(“ My salary is “ + salary);
}
The method signature tells us that this method calculate will not return any value because of the void keyword but if we
replaced the void with String it means the method will return a string value.
Adding method to a class
Let us create another method called sum() which should add two numbers:
public void sum() {
float number1 = 7.99f;
float number2 = 8.77f;
float total = number1 + number2;
System.out.println(“ The total is “ +total)
}
Adding method to a class
Let us call the methods in the main method:
public static void main(String[] args) {
Student p = new Student();
p.calculate();
p.sum();
}
}
Activity
Create a class with two methods. The first method will declare 4 numbers and find their
average
The second method will calculate the monthly salary for an employee by the name
Thomas. Thomas worked for 20 days and each day he gets 200rtgs. The tax is charged at
12% . Calculate his net salary.
Defining Constructors
Constructor is a block of code that initializes the newly created object. A constructor
resembles an instance method in java but it’s not a method as it doesn’t have a return type.
Constructor has same name as the class and looks like this in a java code
public class January{
January() { //constructor
In the main method
January p = new January();
The new keyword creates the object for the January class and invokes the constructor to
initialize this created object
Defining Constructors
no-arg constructor:
Constructor with no arguments is known as no-arg constructor.
The signature is same as default constructor, however body can have any code unlike default
constructor where the body of the constructor is empty.
public class January{
January(){
System.out.println(“This is a no – arg constructor”);
}
In the main method
new January();
Defining Constructors
A parameterized constructor
This is a constructor with parameters inside, let us consider our
Teacher class above and come up with a parameterized constructor.
Parameter: variable named in the method definition that is
standing in for a value that will be supplied when the method is
called.
Argument: is a value that is supplied when a method is called
this keyword: If local variables and instance variables are the
same, we use the this keyword
.
Defining Constructors
Constructor overloading
At times you may want to come up with another constructor but with a
different number of parameters from the first one that we did, this is called
constructor overloading.
The compiler at run time will be able to separate the constructors basing on
the number of parameters specified in the constructor and the sequence of
data types.
For example using the same class of Teacher we can use this constructor:
public class January {
int age;
String name;
int age2;
January( int studentAge, String studentName) {
this.age = studentAge;
this.name = studentName;
System.out.println(" The student age is " + studentAge+ " and name " + studentName);
}
January(int teacherAge){
this.age2 = teacherAge;
System.out.println(" The teacher age is " + teacherAge);
}
public static void main(String[] args) {
January p = new January(50,"Martha");
January p1 = new January(100);
}
Defining Constructors
Static modifier is used to when coming up with class, method and variables
Final modifier used also on class, method and variables
The final modifier indicates that an object is fixed and cannot be changed. When you use
this modifier with a class-level object, it means that the class can never have subclasses.
When you apply this modifier to a method, the method can never be overridden. When you
apply this modifier to a variable, the value of the variable remains constant
Modifiers
Abstract modifier used on classes and methods
The abstract modifier is placed before classes or methods. For a class, it means that it cannot be directly
instantiated, but has to be subclassed. For a method, it means that it does not have an implementation in the
class, but has to be implemented in a subclass. It cannot be applied to variables