-----------Class and method Structure------------
public class Name {
//member variables VAR_TYPE VAR_NAME;
//method
modifier RetrunType methodName(){
return VAR_NAME;
}
}
Description:
- Replace “Name” with valid class Name such as Car.
- VAR_TYPE = Fundamental or Reference data types. e.g. int, double, Car
- VAR_NAME = Valid name, e.g. price, currency
- modifier = public, private, protected, default (when not specified)
- ReturnType = Fundamental or Reference data types. e.g. int, double, Car. Use void when method is not returning anything.
- methodName = valid method name, e.g. getPrice()
-----------Source [Car.java]------------
public class Car {
private double price;
private String currency;
public String getCurrency() {
return currency;
}
public void setCurrency(String c) {
currency = c;
}
public double getPrice() {
return price;
}
public void setPrice(double p) {
price = p;
}
}
Description:
The Car class contains
one private member variable (property) called price. As the price is private no other object can access or modify it. Hence, to access or modify the Car price, the Car class should expose the price to the
external world via public methods
(interfaces) such as getPrice() and setPrice(). By using these interfaces other
Objects can access and modify value of the price.
No comments :
Post a Comment