Defining Constructor – Car Example

Download Report

Transcript Defining Constructor – Car Example

Constructors & An Introduction
to Methods
Defining Constructor – Car Example
Public class car {
String Model;
double speed;
String colour;
{
Public Car ()
}
{
Public Car (String make)
{
model = make;
…
}
}
Car car1 = new Car();
Car car 2 = new car ("Audi");
Constructors
 A constructor is a method that creates an
object. In java constructors are instance
methods with the same name as their class.
Constructors are invoked using the new
keyword
 A constructor is a special method which is
executed only when an object is created
 The purpose of a constructor is to setup (or
construct) the environment for the object
(variable values, etc) and to initialise an
objects data when it is created
 A constructor method is easily recognised as
it must:
– Have the same name as the class
Constructors: Summary
 A constructor is generally the first method in a
class - it has the exact same name as the class
(but has parentheses and optional arguments)
 The arguments in a constructor are typically used
to initialise the instance variables of the class.
 Java has a default constructor if none is provided
which does not initialisation - this constructor is
called the no args constructor.
Methods
Methods
 A method is a group of instructions (also called a
function or subroutine in other languages)
 A method can only be defined within a class
definition
 Every java program must contain at least one
method
 Large programs can get very complex. Methods
help to reduce complexity by splitting programs
into relatively isolated sections.
Method Structure
 A method must take the following form
returnType name (argument list){
// method body
}
Defining Arguments for methods
 In the method definition:
– The argument list contains zero or more formal
arguments separated by commas and enclosed in
parenthesis
Writing a method - Example
void printHi()
{
System.out.print(“hi”);
}
 This method produces no result after it
finishes, it requires no value (parameter) to
operate.
Writing a method - Example
int addNumbers(int number1, int number2)
{
int result = number1+number2;
return result;
}
 The method produces an integer when it finishes.
It also requires two integers to be given to it.
 The method would therefore be used like this:
System.out.print(“answer=“+addNumbers(5,4));
Method Declaration
 The compiler identifies methods by their name, number and type of
arguments.
 The compiler also knows (from the return type in the declaration) the
return type of the method
 There can be zero or more arguments passed to a method, but the
parenthesis are mandatory (as is a name and return type)
 The return operator is required within every method unless the void
keyword is used in its declaration or unless it is a constructor.
 If the data type returned by the method does not match the return type,
the class will not compile.
class Square
{
int length;
int breadth;
public Square (int length, int breadth){
this.length = length;
this.breadth = breadth;
}
int area(){
int answer = length*breadth;
return answer;
}
}//end class Square
Square Example
class Square_Example
{
public static void main(String args[])
{
Square s;
s = new Square(10,20);
System.out.println("result = " + s.area());
}//end main
}