Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
COE 351
OBJECT ORIENTED PROGRAMMING:
Introduction to Java
1
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Lecture Outline
Java Basic Syntax
Java: Variables, Constants
Objects and Classes
• Constructors
• Creating objects
• Accessing instant variables and Methods
2
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
What is Java?
High-level programming language that is class-based and object-oriented
Key advantages include:
Object-oriented
Platform independent
Simple and secure
Portable and robust
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Java Basic Syntax
Java programs or applications follow the following syntax:
Case sensitive e.g. MyJava is different from myjava
Class name - For all class names the first letter should be in Upper
Case. If sevreral words form the name of the class, each inner word
should be capitalised e.g. MyFirstJava
Method names - It should begin with a lower case. If several words
are used the inner words are capitalised. E.g. validateMyName()
Program File Name − Name of the program file should exactly match
the class name. e.g. Assume a program has a class name HelloWorld,
then the file should be saved as [Link]
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Java Variables
Java has the following types of variables
Local Variables- Variables defined inside methods, constructors or
blocks are called local variables. The variable will be declared and
initialized within the method and the variable will be destroyed
when the method has completed
Instance variables − Instance variables are variables within a class
but outside any method. These variables are initialized when the
class is instantiated
Class variables − Class variables are variables declared within a class,
outside any method, with the static keyword.
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Java Keywords
abstract assert boolean break
byte case catch char
class const continue default
do double else enum
extends final finally float
for goto if implements
import instanceof int interface
long native new package
private protected public return
short static strictfp super
switch synchronized this throw
throws transient try void
volatile while
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Java Classes and Objects
Classes in Java – It forma the template/blueprint from which
objects are created. A class can contain any of the variable types:
local, instant and class variables. For example:
public class Students {
String name;
int age; Attributes/ fields/ instant variables
String color;
void reading() { }
void jumping() { } Class
methods
void sleepingHours() { }
} 3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Java Classes and Objects…
Objects in Java – An instant of a class. Thus, realisation of the
class or blueprint.
Creating an object - There are three steps when creating an
object from a class:
Declaration − A variable declaration with a variable
name with an object type
Instantiation − The 'new' keyword is used to create
the object
Initialization − The 'new' keyword is followed by a call
to a constructor. This call initializes the new object
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Constructors
A special method for initializing objects
It has the same name as its class
All classes have constructors, whether you define one or not,
because Java automatically provides a default constructor that
initializes all member variables to zero
You can define your own constructor, if that happens the
default constructor is no longer used.
Syntax
class ClassName {
ClassName() {
}
} 3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Constructors
Java allows two types of constructors
(i) No argument constructor- It (ii) Parameterized constructor- It
takes no parameter takes one or more parameters
Example Example
public class MyJavaClass { public class MyJavaClass {
Int num; Int num1, num2;
public MyJavaClass() { public MyJavaClass(int x, int y) {
num = 100; } this.num1=x;
} this.num2= y; }
}
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Creating objects
To create an object the “ new”
keyword is used, followed by a call
to the constructor
Example 1 Example 2
public class MyJavaClass { public class MyClass {
public MyJavaClass() { Int num1, num2;
[Link](“Hi”);} public MyClass(int x, int y) {
public static void main(String [] args){ this.num1=x;
MyJavaClass t= new MyJavaClass(); this.num2= y; }
} public static void main(String []args){
} Myclass b= new Myclass( 3,8);
}
}
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Accessing instant Variables and Methods
To access an instant variable, first create an object with a
reference:
/* First create an object */
ObjectReference = new Constructor();
Use the reference variable to call the instant variable as follows
[Link]
You can call a class Method in a similar fashion as:
[Link]();
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Accessing instant Variables and Methods
Example: How to access instant variables and methods of a class
public class Puppy {
private int puppyAge;
// This constructor has one parameter, name.
public Puppy(String name) {
[Link]("Name chosen is :" + name ); }
public void setAge( int age ) {
puppyAge = age; }
public int getAge( ) {
[Link]("Puppy's age is :" + puppyAge );
return puppyAge; }
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Accessing instant Variables and Methods…
Example: How to access instant variables and methods of a class…
public static void main(String []args) {
/* Object creation */
Puppy myPuppy = new Puppy( "tommy" );
/* Call class method to set puppy's age */
[Link]( 2 );
/* Call another class method to get puppy's age */
[Link]( );
/* You can access instance variable as follows as well */
[Link]("Variable Value :" + [Link] ); }}
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Accessors and Mutators
One of the ways we can enforce data encapsulation is through
the use of accessors and mutators
The role of accessors and mutators are to return and set the
values of an object's state respectively
It follows a naming scheme prefixing the word "get“ or “set”
to the start of the method name
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Accessor Method
It is used to return the value of a private field
Its naming scheme starts with “get” e.g. getAge();
These methods always return the same data type as their
corresponding private field
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Mutator Method
It is used to set the value of a private field
Its naming scheme starts with “set” e.g. setAge();
These methods do not have a return type and accept a
parameter that is the same data type as their corresponding
private field
The parameter is then used to set the value of that private
field
3
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Example
(i) Define a class called Students with two private fields age and
name of types int and String respectively. Provide a one-
argument constructor that intitialise the name and the age of
Students object. Provide accessor and mutator methods to
manipulate the fields.
Define another method call printStudentRecords().
This method should print the name and age of
Students object created
(ii) Write a test class called StudentTest and create two Students
objects; studentOne and studentTwo. Call the printStudentRecords()
to print the student details as follows:
("Name:"+ name );
3
("Age:" + age );
Kwame Nkrumah University of
Science & Technology, Kumasi, Ghana
Example-Solution