|
Java is a class-based object oriented language. It constitutes several general-purpose programming constructs. Among them, a ‘class’ and an ‘interface’ are the two key constructs. A Java class provides common implementation for a set of similar objects. It does so by defining a set of methods and data variables. Unlike Java class, a Java interface merely describes a common public behavior of similar objects. Therefore, it contains a set of method declarations. A Java class may implement an interface by providing implementation for the interface methods. A typical Java application has many class and interface definitions in it. Usually these definitions are not just scattered all over the application, Java uses another construct called package to organize them.
Now that we know about three ‘structuring constructs’, what is left is the construct that actually make the application work- “the Java objects”. When an application made up of classes, packages and interfaces is executing, the Java environment creates required objects from the class definitions. Thus, you will find classes, interfaces, packages and objects in virtually every Java application. In this chapter, we will look at the syntax and semantics of each of these constructs. However, before we dive deep into the details, let us broadly identify these constructs in a real life banking application.
A typical banking involves a customer, a teller, a bank account, an ATM, an account-book and so on. You can encapsulate these entities into classes. For instance, a Java class for bank account will have a set of methods doing what a bank account is supposed to do and variables for storing information the account is supposed to have. Thus, you may have classes- Account, AccountBook, Customer in the banking application. You can further structure these classes using the inheritance principle. For instance, your application may have different types of bank accounts, say checking-account or savings-account. For representing these, you need not declare separate class for each one. You can define a new CheckingAccount class in terms of an existing Account class (the superclass). By doing so, it can reuse (or inherit) the Account class’s implementation. In our banking example, the Account is superclass of CheckingAccount. Note that a Java superclass is typically a class that has been extended by another class. The class that extends the superclass is called as a subclass. Figure 1.1 illustrates the key Java constructs in a simple banking application.

Figure 1.1 Java constructs in a banking application.
On the left side of figure 1.1 you can see a banking application that is organized in several Java packages . Each package may have several class and interface definitions. The figure shows one such package by expanding and revealing its details. You can see the class definitions for general bank entities such as Account, Teller. You can also see the interfaces definitions (Auditable, Searchable) )for common bank activities such as account-searching and auditing. The figure also shows account objects that are created from the class definitions. Equipped with this knowledge of how the Java constructs work together, let us see how to declare and use each one of them.