{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Intro to Java Web-Notes_Part81

Intro to Java Web-Notes_Part81 - abstract class Transport...

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

View Full Document Right Arrow Icon
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: abstract class Transport { private int numPassengers = 0; public int getNumPassengers() { return numPassengers; } public void setNumPassengers(int num) { if (num > O && num < 1000000) numPassengers = num; } abstract public int maXSpeed(); } class Bicycle extends Transport { private int weight; public Bicycle(int weight) { this.weight 2 weight; setNumPassengers(l); // use the public "set" method } public int maxSpeed() { return 45; } public String report() { return "Bicycle with weight of " + weight + "pounds"; } Private -- use constructor to set Another alternative is to keep the variable private, set the variable in a constructor and just supply the "get" method: public class MyProgram6 { public static void main (String args) { Bicycle b = new Bicycle<40); System.out.println(b.report()); b.numPassengers = 25; // error for this directory (or another directory)! b.weight = 3; // error for this directory (or another directory)! } abstract class Transport { private int numPassengers = 0; public Transport(int num) { this.numPassengers = num; } public int getNumPassengers() { return numPassengers; } abstract public int maXSpeed(); class Bicycle extends Transport l private int weight; public Bicycle(int weight) l super(l); this.weight = weight; l public int maxSpeed() i return 45; l public String report() l return "Bicycle with weight of " + weight + "pounds"; } Protected One more possibility is to use the keyword "protected". Protected is very similar to package access (saying nothing) in that a protected variable is open to access by all of the classes defined in the same file/directory. It is less restrictive than package access in that a "protected" variable CAN be accessed by a class outside the current package (directory) if that class is a subclass of the class containing the protected variable. public class MyProgramS l public static void main (String args) l Bicycle b = new Bicycle(40); System.out.println(b.report()); b.numPassengers = 25; // OK if in this directory, still an error if this // class is in different package than Transport (unless this // outside class extends Transport l abstract class Transport l protected int numPassengers = O; // changed to protected abstract public int maxSpeed(); l class Bicycle extends Transport l protected int weight; public Bicycle(int weight) l this.weight = weight; numPassengers = l; // NO LONGER an error l public int maxSpeed() i return 45; l public String report() i return "Bicycle with weight of " + weight + "pounds"; } } The following list gives, in order, the levels of accessibility for a variable defined as: ° public Accessible to all code in all classes, in or out ofthe this package. ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern