[go: up one dir, main page]

0% found this document useful (0 votes)
2 views28 pages

Classes Presentation

The document explains the concepts of objects, reference variables, and classes in Java programming. It details how variables can store data or memory addresses, the role of the 'new' operator in memory allocation, and the classification of class members as private or public. Additionally, it covers constructors, their properties, and includes sample programs demonstrating class creation and method usage.

Uploaded by

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

Classes Presentation

The document explains the concepts of objects, reference variables, and classes in Java programming. It details how variables can store data or memory addresses, the role of the 'new' operator in memory allocation, and the classification of class members as private or public. Additionally, it covers constructors, their properties, and includes sample programs demonstrating class creation and method usage.

Uploaded by

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

OBJECTS AND REFERENCE

VARIABLES
• Consider the following statement:
int x = 45;
• In this declaration memory space is
allocated to store an int value and this
memory space is called x.
• The variable x can store an int value in its
memory space.

45
x
variable x and its data
String str = new String(“Java progamming”);
• Variable str cannot store directly store
data in its memory space.

• The variable str stores the memory


location (address) of the memory space
where the actual data is stored.
str 2500
2500 Java programming
What is new?
• In Java, new is an operator.
• It causes the system to allocate memory
space of a specific type, store specific
data in that memory space and return the
address of that memory space.
• Therefore the declaration on the previous
slide causes the system to allocate
memory space large enough to store the
string “Java Programming”
Reference Variables
• In Java, variables such as str are called
reference variables.
• Reference variables are variables that store the
address of a memory space.
• Any variable declared using a class(such as the
variable str) is a reference variable.
• The memory space 2500 where the string “Java
Programming” is stored is called a String
object.
• We call String objects instances of the class
String.
Instantiation

Using the operator new to create a class


object is called an instantiation of that
class.
CLASSES
• First step in problem-solving with object-oriented
design(OOD) is to identify the components
called objects.

• An object combines data and the operations on


that data in a single unit.

• The mechanism in Java that allows you to


combine data and the operations on that data in
a single unit is called a class.
CLASSES
• A class is a collection of fixed number of
components.

• The components of a class are called


members of the class.

• A member of a class can be either a


variable, a constant and/or a method.
CLASSES
• The members of a class are usually
classified into three categories, namely,
private
public
protected
This presentation will cover the first two
categories : private and public
CLASSES
• If a member of a class is private, you
cannot access it outside the class.

• If a member of a class is public, you can


access it outside the class.

• The methods of a class are called the


instance methods of the class.
Sample Program
// Sample program on classes
// Program to create a class

public class Vehicle // class name


{
public String getVehicle() //method name
{
String transport = "Taxi";
return transport;
//contains no main
method
}
}
// Driver program for class Vehicle
// Test Program
public class TestVehicle
{
public static void main(String[] args)
{ creates object

Vehicle objCar = new Vehicle();


System.out.println("The mode is..." + objCar.getVehicle());
}
} uses object
new Operator
• The new operator creates a new object.

• It causes the system to allocate memory space of a


specific type, store specific data in that memory space,
and return the address of that memory space.

Names of classes
The names of classes are written in capitals and that of
methods and objects in lowercase letters.
Two Methods in a class

// Basic Salary for a Teacher


public class Salary //class name
{
public String getName() //method definition
{
String surname = "Naidoo";
return surname;
}
public int getSalary() //method definition
{
int sal = 3500;
return sal;
}
}
Driver program/Test Program
//Test program for class Salary - driver program
public class TestSalary
{
public static void main(String[] args)
{
Salary objTeacher = new Salary(); //new object created
String surname = objTeacher.getName(); // name of method from the
class

int pay = objTeacher.getSalary(); //name of method from the class

System.out.println(surname + " is paid R " + pay);


}
}
Hands on Activity
Create a class Cellphone that uses
the methds getCellphone() to
display the type of cellphone as
well getAmount() that assigns the
available amount on the cell
phone. Test the class and display
the amount.
Suggested Solution
//Suggested solution to cellPhone question
public class Cellphone
{
public String getCellphone()
{
String cellName = "Nokia";
return cellName;
}
public double getAmount()
{
double amt = 250.50;
return amt;
}
}
Suggested Solution
//Driver program for class Cellphone
public class TestCellphone
{
public static void main(String[] args)
{
Cellphone objCell = new Cellphone();
String cellName = objCell.getCellphone();
double cellAmt = objCell.getAmount();

System.out.println(cellName + " has R" + cellAmt + " airtime");


}
}
Hands on Activity
Create a class Calculate that uses
the methods getProduct() and
getDifference() to the operations
with two numbers that you choose
randomly with : Math.random().
Test the class and display the
values.
Suggested Solution
TestCalculateProDiff.java
CONSTRUCTORS
• Every class has special types of methods called
constructors.

• A constructor has the same name as the class,


and it executes automatically when an object of
that class is created.

• Constructors are used to guarantee that the


instance variables of the class initialize to
specific values – ie. it is used to initialize
variables of a class.
CONSTRUCTORS
• There are 2 types of constructors :
- with parameters
- without parameters
• The constructor without parameters is
called the default constructor.
• Constructors have the following properties:
PROPERTIES OF
CONSTRUCTORS
• A class can have more than one
constructor. However, all constructors of a
class have the same name.

• Constructors execute automatically when


class objects are instantiated. Because
they have no types, they cannot be called
like other methods.
PROPERTIES OF
CONSTRUCTORS
• The name of the constructor is the same
name of the class.

• A constructor, even though it is a method,


has no type. That is, it neither a value
returning method nor a void method
Program showing the use of a
Constructor
//program showing the use of constructor
public class CircleCircum
{
private double radius;
public CircleCircum() //constructor gives an initial value
{
radius = 11.56;
}
public double getCircum() //method
{
double circum = 2 * (22/7) * radius; //making use of radius value from above
return circum;
}
}
Driver program
//Driver program for class CircleCircum
public class TestCircleCircum
{
public static void main (String[] args)
{
CircleCircum objCircle = new CircleCircum();
double circle = objCircle.getCircum();
System.out.printf("The circumference of the circle :..%.2f %n" , circle);
}
}
Hands on Activity
The perfect number is a number of
which the sum of the factors(apart
from the number itself) is equal to
the
number. For example 6 is a perfect
number. The factors of 6 are 1,2,3
(apart from 6). 1+2+3 = 6.
Write a program to determine
whether
a number is a perfect number.
END OF SLIDE SHOW

ON CLASSES

You might also like