ch3-Package Access
Chapter_3 Exercise_3-15 | Exercise_3-16 |
Note: See Java_Class_Methods on the Java_Tutorial on W3Schools.
CONTENTS: readme.txt Car.java Test.java
readme.txt
Folder package-access contains the files Car.java and Test.java.
Open the terminal in this folder.
Compile Car.java:
javac Car.java
Compile Test.java:
javac Test.java
Run Test:
java Test
Output:
The car is going as fast as it can!
Max speed is: 200
Car.java
class Car
{
void fullThrottle()
{
System.out.println("The car is going as fast as it can!");
}
void speed(int maxSpeed)
{
System.out.println("Max speed is: " + maxSpeed);
}
}
/*
javac Car.java
*/
Test.java
class Test
{
public static void main(String[] args)
{
Car myCar = new Car(); // Create a myCar object
myCar.fullThrottle(); // Call the fullThrottle() method
myCar.speed(200); // Call the speed() method with argument `200'
}
}
/*
javac Car.java
javac Test.java
java Test
The car is going as fast as it can!
Max speed is: 200
*/
Note: Test.java has default_(package)_access to the classes in the same folder and their methods (that are not private). Thus, class Car and its methods in Car.java do not have to be public, default (package) access suffices.
Chapter_3 Exercise_3-15 | BACK_TO_TOP | Exercise_3-16 |
Comments
Post a Comment