Intro to Java Web-Notes_Part80

Intro to Java Web-Notes_Part80 - Bicycle b = new...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Bicycle b = new Bicycle(40); System.out.println(b.report()); b.numPassengers = 25; // package access (accessible IF this class is // in the same package as Transport) } abstract class Transport { int numPassengers = O; // package: public to all classes in this file/directory // Inaccessible to classes outside this package, even if that // class extends Transport abstract public int maxSpeed(); } class Bicycle extends Transport { int weight; // package: public to all classes in this file/directory public Bicycle(int weight) { this.weight 2 weight; numPassengers = l; // package: public to all classes in this file/directory // but an error if this class is in a different } // package (directory) than Transport public int maxSpeed() { return 45; } public String report() { return "Bicycle with weight of " + weight + "pounds"; } Public lfwe set the two variables to "public", they will still be available to all classes in this file and directory. They will now also be available to any classes in different packages (directories) that might want to import our classes: public class MyProgramZ { public static void main (String args) { Bicycle b = new Bicycle(40); System.out.println(b.report()); b.numPassengers = 25; // public access } abstract class Transport { public int numPassengers = O; // public h r and V rywh r ls abstract public int maxSpeed(); } class Bicycle extends Transport { public int weight; // public here and everwhere else public Bicycle(int weight) { this.weight = weight; numPassengers = l; // public h r and v rywh r ls } public int maxSpeed() l return 45; } public String report() l return "Bicycle with weight of " + weight + "pounds"; } Private The "safest" way to define a variable is to make it private. However, if we simply change "public" to "private" for the current example, we will have problems: public class MyProgram3 l public static void main (String args) l Bicycle b = new Bicycle(40); System.out.println(b.report()); b.numPassengers = 25; // error! l abstract class Transport l private int numPassengers = O; // changed to private abstract public int maxSpeed(); l class Bicycle extends Transport l private int weight; // changed to private public Bicycle(int weight) l this.weight = weight; numPassengers = l; // error! l public int maxSpeed() i return 45; l public String report() l return "Bicycle with weight of " + weight + "pounds"; } Private -- get/set Even the subclass of Transport cannot access its private variables. Either make numPassengers public (or better yet, protected), or else supply "get" and "set" methods for the private variable. Keep numPassengers private and supply public "get" and "set" methods: public class MyProgram4 l public static void main (String l] args) l Bicycle b = new Bicycle(40); System.out.println(b.report()); b.numPassengers = 25; // error for this directory (or another directory)! ...
View Full Document

Page1 / 2

Intro to Java Web-Notes_Part80 - Bicycle b = new...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online