Introducing: Lab # 5 Classes, Methods and Constructor
Introducing: Lab # 5 Classes, Methods and Constructor
LAB # 5
OBJECTIVE:
To Study classes, methods and constructor.
THEORY:
Class:
The definition of a class is to create objects of that class type — that is, to
create objects that incorporate all the components specified as belonging to
that class.
There are just two kinds of things that you can include in a class definition:
❑Fields— These are variables that store data items that typically differentiate
one object of the class from another. They are also referred to as data
members of a class.
❑Methods— These define the operations you can perform for the class — so
they determine what you can do to, or with, objects of the class.
Methods typically operate on the fields — the variables of the class. The fields
in a class definition can be of any of the primitive types, or they can be
references to objects of any class type, including the one that you are
defining. The methods in a class definition are named, self-contained blocks
of code that typically operate on the fields that appear in the class definition.
A class is declared by use of the class keyword. Notice that the general form
of a class does not specify a main( ) method. Java classes do not need to
have a main( ) method. You only specify one if that class is the starting point
for your program.
class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method }
type methodname2(parameter-list) {
// body of method }
// ...
}
To define a class you use the keyword class followed by the name of the class,
followed by a pair of braces enclosing the details of the definition along with
fields. Let’s consider a concrete example to see how this works in practice:
If a method does not return a value, you can just use the keyword return by
itself to end execution of the method:
5.3 CONSTRUCTORS
❑A constructor never returns a value, and you must not specify a return type
— not even of type void.
As I said, if you don’t define any constructors for a class, the compiler will
supply a default constructor that has no parameters and does nothing. Before
you defined a constructor for the Sphere class, the compiler would have
supplied one, defined like this
To see a practical example you could add a constructor to the Sphere class
definition
class Sphere {
// Instance variables
double radius; // Radius of a sphere
When you declare a variable of type Sphere with the following statement:
As just explained, when you create a class, you are creating a new data type.
You can use this type to declare objects of that type. However, obtaining
objects of a class is a two-step process.
First, you must declare a variable of the class type. This variable does
not define an object. Instead, it is simply a variable that can refer to an
object.
Second, you must acquire an actual, physical copy of the object and
assign it to that variable. You can do this using the new operator. The
new operator dynamically allocates (that is, allocates at run time)
memory for an object and returns a reference to it. This reference is,
more or less, the address in memory of the object allocated by new.
This reference is then stored in the variable. Thus, in Java, all class
objects must be dynamically allocated.
you will recall from the discussion of String objects and arrays that the variable
and the object it references are distinct entities. To create an object of a class
you must use the keyword new followed by a call to a constructor. To initialize ball
with a reference to an object, you could write:
LAB TASK
2. Write a program that creates two classes one is Student another is the
main method class. student contains private fields of roll-no, semester,
GPA, and use a constructor and a method to get and show the above
information of three students using get and show method in java.