Lecture 2 Fundamentals of Classes in Java
Lecture 2 Fundamentals of Classes in Java
Class Fundamentals
A class is a sort of template which has attributes and methods. An object is an instance of
a class, e.g. Riccardo is an object of type Person. A class is defined as follows:
type method1(parameters)
{ // body of method }
type method2(parameters)
{ // body of method }
The classes we have used so far had only one method, main(), however not all classes specify a
main method. The main method is found in the main class of a program (starting point of program).
Page 1
Al-Farabi University College
class TwoVehicles {
public static void main(String args[]) {
Vehicle minivan = new Vehicle();
Vehicle sportscar = new Vehicle();
int range1, range2; // assign values to fields in minivan
minivan.passengers = 7;
minivan.fuelcap = 16;
minivan.mpg = 21;
// assign values to fields in sportscar
sportscar.passengers = 2;
sportscar.fuelcap = 14;
sportscar.mpg = 12;
// compute the ranges assuming a full tank of gas
range1 = minivan.fuelcap * minivan.mpg;
range2 = sportscar.fuelcap * sportscar.mpg;
System.out.println("Minivan can carry " + minivan.passengers + "
with a range of " + range1);
System.out.println("Sportscar can carry " + sportscar.passengers
+ " with a range of " + range2); } }
Page 2
Al-Farabi University College
Creating Objects
In the previous code, an object was created from a class. Hence ‘minivan’ was an
object which was created at run time from the ‘Vehicle’ class – vehicle minivan =
new Vehicle( ) ; This statement allocates a space in memory for the object and it also
creates a reference. We can create a reference first and then create an object:
We have created a new instance of type Vehicle named car1. However note that car2
is NOT another instance of type Vehicle. car2 is the same object as car1 and has
been assigned the same properties
Page 3
Al-Farabi University College
Methods
Methods are the functions which a particular class possesses. These functions
usually use the data defined by the class itself.
// adding a range() method
class Vehicle { int passengers; // number of passengers
int fuelcap; // fuel capacity in gallons
int mpg; // fuel consumption in miles per gallon
// Display the range.
void range()
{ System.out.println("Range is " + fuelcap * mpg); }
}
Note that ‘fuelcap’ and ‘mpg’ are called directly without the dot (.) operator.
Methods take the following general form:
ret-type name( parameter-list )
{ // body of method }
‘ret-type’ specifies the type of data returned by the method. If it does not return any
value we write void. ‘name’ is the method name while the ‘parameter-list’ would be
the values assigned to the variables of a particular method (empty if no arguments
are passed).
class AddMeth {
public static void main(String args[]) {
Vehicle minivan = new Vehicle();
Vehicle sportscar = new Vehicle();
int range1, range2; // assign values to fields in minivan
minivan.passengers = 7;
minivan.fuelcap = 16;
minivan.mpg = 21; // assign values to fields in sportscar
sportscar.passengers = 2;
sportscar.fuelcap = 14;
sportscar.mpg = 12;
Page 4
Al-Farabi University College
Page 5
Al-Farabi University College
Page 6
Al-Farabi University College
Constructors
A constructor is created by default and initializes all member variables to zero.
However we can create our constructors and set the values the way we want, e.g.
class MyClass {
int x;
MyClass()
{ x = 10; }
}
class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass();
MyClass t2 = new MyClass();
System.out.println(t1.x + " " + t2.x); }
}
Output// 10 10
Page 7
Al-Farabi University College
Method Overloading
// Demonstrate method overloading.
class Overload {
void ovlDemo() {
System.out.println("No parameters");
} // Overload ovlDemo for one integer parameter.
void ovlDemo(int a) {
System.out.println("One parameter: " + a);
} // Overload ovlDemo for two integer parameters.
int ovlDemo(int a, int b)
{ System.out.println("Two parameters: " + a + " " + b);
return a + b; } // Overload ovlDemo for two double parameters.
double ovlDemo(double a, double b)
{ System.out.println("Two double parameters: " + a + " "+ b);
return a + b; }
}
Main Program:
class OverloadDemo {
public static void main(String args[]) {
Overload ob = new Overload();
int resI; double resD; // call all versions of ovlDemo()
ob.ovlDemo();
System.out.println();
ob.ovlDemo(2);
System.out.println();
resI = ob.ovlDemo(4, 6);
System.out.println("Result of ob.ovlDemo(4, 6): " +resI);
System.out.println();
Page 8
Al-Farabi University College
Page 9
Al-Farabi University College
Overloading Constructors
// Overloading constructors.
class MyClass { int x; MyClass() {
System.out.println("Inside MyClass().");
x = 0; }
MyClass(int i) {
System.out.println("Inside MyClass(int).");
x = i; }
MyClass(double d)
{ System.out.println("Inside MyClass(double).");
x = (int) d; }
MyClass(int i, int j)
{ System.out.println("Inside MyClass(int, int).");
x = i * j; }
}
Main Program
class OverloadConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass();
MyClass t2 = new MyClass(88);
MyClass t3 = new MyClass(17.23);
MyClass t4 = new MyClass(2, 4);
System.out.println("t1.x: " + t1.x); System.out.println("t2.x: "
+ t2.x); System.out.println("t3.x: " + t3.x);
System.out.println("t4.x: " + t4.x); }
}
Page 10
Al-Farabi University College
Page 11
Al-Farabi University College
ob.beta = 88;
ob.gamma = 99; }
}
Page 12