Docsity

Public and Helper Classes - Java and the Web - Lecture Slides, Slides of Java Programming

Toggle sidebar
bg1
Public and Helper Classes
•A class intended for general use should be a public class. It
must be declared public and reside in a file is the same as the
class name.
•A subordinate class used only by public classes is a helper
class. It should not be declared public and, if it supports a
single public class, should appear in the same file as the public
class.
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26

Partial preview of the text

Download Public and Helper Classes - Java and the Web - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Public and Helper Classes

  • A class intended for general use should be a public class. It must be declared public and reside in a file is the same as the class name.
  • A subordinate class used only by public classes is a helper class. It should not be declared public and, if it supports a single public class, should appear in the same file as the public class.

Nested or not?

  • If a helper class appears separately in a file with a public class, it is accessible only to that public class. See the example on the next slide.
  • If a helper class is nested inside a public class, it is accessible through that public class by subclasses of that public class unless they are combined in the same package. See the example on the slide following the next slide.

Nested in LinkedList.Java

public class LinkedList implements List { protected Node head, tail; protected int count; static protected class Node { Object element; Node next, prev; } //.. //implementation of Linked List }

Separate interface from

implementation

  • It is good programming practice to use interfaces widely in Java. Whenever the functionality of a class can be implemented in different ways, create an interface that is polymorphic and broadly designed, then create different classes that extend the functionality to different cases. This requires a design practice of thinking about the most general case of a class before implementing the specific cases.

Problems and Solutions

  • Concentrating on understanding the problem before you even consider a solution is an example of deferring decision making. In general, the earlier you make a decision the more likely you are to make a wrong decision. Every time you start to write code, you select one approach to a solution that precludes other approaches. If you decide before fully understanding the problem, you are less likely to choose the best, or even a good approach.

Related Java functions

• The previous example, from the author of the

text, may be confusing, as the Java class

library provides an interface List and a Linked

List in the Collections framework, which we

will see in chapter 8.

• Here he is just showing an example of a

similar class. Do not confuse it with the class

library interfaces and classes.

Implementing the interface

  • Once you have declared an interface, implementing it is simple. All you have to do is declare a class that implements the interface. The implementing class then must provide an implementation for each of the methods declared in the interface. public class LinkedList implements list {

public int size {… implementation of size …} … implementation of remaining class members …

Class Members

  • While the order of class members is semantically insignificant to the Java compiler, it still matters to human readers to make the class easy to understand.
  • A best practice is to order the members according to their accessibility and roles:
  1. Public constructors
  2. Public accessor or selectors (do not modify state)
  3. Public mutators or modifiers (change state of objects)
  4. Nonpublic constructors and auxiliary methods

Avoid public fields

  • The object oriented principle of encapsulation suggests that attributes of a class should not be directly accessible. Instead, the fields should be accessed indirectly through methods of the class.
  • An accessor, get method, or getter is a method used to obtain the value of a field.
  • A mutator, set method, or setter is a method used to change the value of a field.

Accessors and Mutators

  • Requiring access to attributes only through accessors and mutators allow those methods to enforce constraints and security methods to avoid improper use of the fields and inconsistent data.
  • Preventing direct access to the data also protects against problems such as inconsistency related to simultanous use by different objects. Ths is a key virtue of encapsulation. The discussion of PolarPoint on pages 211-213 of the text explains the importance of this and is discussed on the next slide.

Complete Public Interface

• The set of public methods defined in a class

should provide full and convenient access to

all of the functionality of the class.

Separate Interface from

Implementation

  • If the functionality of a class can be

implemented in different ways, it is advisable

to separate the interface from the

implementation.

  • This implements the Object Oriented principle

of information hiding, so that changes in

implementation will not affect clients of the

class.

Java Inheritance

• There are two key object oriented ideas that

drive inheritance in Java, hook methods and

template methods. In both cases, they give

shape to a process that is initially incomplete

and is completed by methods that inherit from

or implement them.

Hook Methods

• A hook method, usually an abstract method in an

abstract class, is a place holder that will be filled

with application specific logic in a later class that

implements the method.

• Java literature often refers to a hook method as a

“hot spot,” but that term doesn’t always refer to a

method. A hot spot can also be implemented as a

class or even an application.