1 / 30

Java Programming

Java Programming. Chapter 3 More about classes. Why?. To make classes easier to find and to use to avoid naming conflicts to control access programmers bundle groups of related classes and interfaces into packages. Official Definition.

Télécharger la présentation

Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming Chapter 3 More about classes

  2. Why? • To make classes easier to find and to use • to avoid naming conflicts • to control access • programmers bundle groups of related classes and interfaces into packages.

  3. Official Definition • A package is a collection of related classes and interfaces providing access protection and namespace management

  4. Where? • The classes and interfaces that are part of the Java platform are members of various packages that bundle classes by function: • fundamental classes are in java.lang, classes for reading and writing (input and output) are in java.io, and so on. • You can put your classes and interfaces in packages, too.

  5. Example • Suppose that you write a group of classes that represent a collection of graphic objects, such as circles, rectangles, lines, and points. • You also write an interface, Draggable, that classes implement if they can be dragged with the mouse by the user:

  6. //in the Graphic.java file public class Graphic { . . . } //in the Circle.java file public class Circle extends Graphic implements Draggable { . . . } //in the Rectangle.java file public class Rectangle extends Graphic implements Draggable { . . . } //in the Draggable.java file public interface Draggable { . . . }

  7. Why Bundle in a Package • You and other programmers can easily determine that these classes and interfaces are related. • You and other programmers know where to find classes and interfaces that provide graphics-related functions. • The names of your classes won’t conflict with class names in other packages, because the package creates a new namespace. • You can allow classes within the package to have unrestricted access to one another yet still restrict access for classes outside the package.

  8. Creating a Package • you put a class or an interface in it. • To do this, you put a package statement at the top of the source file in which the class or the interface is defined. • For example, the following code appears in the source file Circle.java and puts the Circle class in the graphics package:

  9. package graphics; public class Circle extends Graphic implements Draggable { . . . } The Circle class is a public member of the graphics package. You must include a package statement at the top of every source file that defines a class or an interface that is to be a member of the graphics package.

  10. So you would also include the statement in Rectangle.java and so on: • package graphics; • public class Rectangle extends Graphic implements Draggable • { . . . } • The scope of the package statement is the entire source file, so all classes and interfaces defined in Circle.java and Rectangle.java are also members of the graphics package. • If you put multiple classes in a single source file, only one may be public, and it must share the name of the source files base name. • Only public package members are accessible from outside the package.

  11. If you do not use a package statement, your class or interface ends up in the default package, which is a package that has no name. • the default package is only for small or temporary applications or when you are just beginning development. • Otherwise, classes and interfaces belong in named packages.

  12. Naming a Package • With programmers all over the world writing classes and interfaces using the Java programming language, it is likely that two programmers will use the same name for two different classes. • The previous example does just that: It defines a Rectangle class when there is already a Rectangle class in the java.awt package. • Yet the compiler allows both classes to have the same name. Why? • Because they are in different packages, and the fully qualified name of each class includes the package name. • That is, the fully qualified name of the Rectangle class in the graphics package is graphics.Rectangle, and the fully qualified name of the Rectangle class in the java.awt package is java.awt.Rectangle.

  13. Using Package Members • Only public package members are accessible outside the package in which they are defined. • To use a public package member from outside its package, you must do one or more of the following: • Refer to the member by its long (qualified) name • Import the package member • Import the member's entire package • Each is appropriate for different situations, as explained in the following sections.

  14. Refer to a Package my name • so far we’ve used simple names – for classes – Rectangle • you can use the simple name if its within the same package as that member – or if you have imported that package • But – if you want to use a package that has not been imported • graphics.Rectangle -- qualified name • You could use it to : graphics.Rectangle myRect = new graphics.Rectangle()

  15. Importing a package member import graphics.Circle; Now you can refer to the Circle class by its simple name: Circle myCircle = new Circle();

  16. Importing an entire package • To import all the classes and interfaces contained in a particular package, use the import statement with the asterisk (*) wildcard character: • import graphics.*; • Now you can refer to any class or interface in the graphics package by its short name: Circle myCircle = new Circle(); Rectangle myRectangle = new Rectangle();

  17. JRS • For your convenience, the Java runtime system automatically imports three entire packages: • The default package (the package with no name) • The java.lang package • The current package by default

  18. Class Variables • class variables are attributes common to all objects belonging to a certain class • These are also called static variables • static does not mean unchangeable • class variables declared static • exp – a variable that keeps a check on how many instances of a particular class there are at one time

  19. Bank Account • every bank account – a particular balance • a particular account holder • these data need to be available in one instance for each account – ordinary instance variables • Interest rate – class variable • Since all accounts will have the same interest rate – inefficient to store this in every single account

  20. public class Account { //class variables private static double interestRate; //class methods public static double getInterest ( ){ interestRate = newInterest; } //instance variables private int customerNo; private double balance, interestEarned;

  21. //constructor public Account (int customer){ customerNo = customer; } //instance methods public double getBalance() { return balance; } public void transaction(double amount){ //negative amount => withdrawal if (amount < 0 && balance + amount < 0) System.out.println (“Withdrawal cannot be done”); else balance = balance + amount; }

  22. public void getDailyInterest(){ interestEarned = interestEarned + balance * interestRate/100/365; } public void addInterest(){ balance = balance+interestEarned; interestEarned = 0; } }

  23. Every account has three class variables • keep track of customer numbers • current balance • interest earned during the year • constructor initializes a new account, a unique customer number is provided as an argument to the constructor • instance variables balance and interestEarned default to 0 • getBalance is called – checks balance of account • transaction – called each time a deposit or withdrawal is made • getDailyInterest – called once a day – it calculates the interest earned • addInterest – called end of every year

  24. class variable – interestRate use static class methods getInterest – setInterest – read and change the interest rate class variables and class methods can have modifiers – public, private … instance methods can have access to the class variables and methods – read and change if class variable is changed in an instance method, the change will affect all the objects since they all share same class

  25. a class variable is initialized when the class is used for the first time in a program can initialize to a specific value will default to zero of not Constructors are not used to initialize class variables constructors only initialize the variables dealing with a particular instance of the class class variables are used to define constants – final

  26. Recap: class variables are declared – static exist only in one edition – common to all objects belonging to the class are initialized when class is loaded exist for the remainder of the program can be initialized if necessary in a special class initializer are often used to define constants

  27. Class Methods • methods marked with static • getInterest and setInterest • they do not handle individual accounts • they use only class variables no instance variables • if you try – compile will fail

  28. Call Statements • Account.setInterest(4.5); • double ir = Account.getInterest(); • also same as instance methods • Account acc = new Account(1234); • acc.setInterest(5.1); but setInterest does not read or change object acc – it changes something that affects all the objects of the class

  29. And Finally • Not unusual to have a class with all class methods • used to encapsulate the methods • See class Math – pg 3.5

More Related