📕
Knowledge
  • Knowledge Repository
  • Java
    • Intro
    • Persistence
      • Java Persistence API (JPA)
        • Entity Manager
        • Transaction Management
        • Relationship Mapping
      • Java Transaction API (JTA)
    • Resources
  • Angular
    • Intro
    • Overview
    • CLI Commands
    • Directives
  • Tools
    • IDEs
      • IntelliJ
        • Shortcuts
  • Book Notes
    • Intro
    • Java A Beginner's Guide
      • Chapter 1. Java Fundamentals
      • Chapter 2. Data Types and Operators
      • Chapter 3. Program Control Statements
      • Chapter 4. Introducing Classes, Objects, and Methods
      • Chapter 5. More Data Types & Operators
      • Chapter 6. Closer Look At Methods and Classes
      • Chapter 7. Inheritance
      • Chapter 8. Packages & Interfaces
      • Chapter 9. Exception Handling
      • Chapter 10. Using I/O
    • Data Pipelines With Airflow
      • Chapter 1. Meet Airflow
      • Chapter 2. Anatomy Of a DAG
  • Course Notes
    • Intro
    • Spring: TDD With JUnit
Powered by GitBook
On this page
  • Packages
  • Java Class Library Packages
  • Interfaces
  • Variables in Interfaces
  • Extending
  • Default Interface Methods
  • Private Interface Methods

Was this helpful?

  1. Book Notes
  2. Java A Beginner's Guide

Chapter 8. Packages & Interfaces

Packages are groups of related classes and using them help to organize code and provide another layer of encapsulation.

Interfaces define a set of methods that must be implemented by a class that implements it. In this way an interface specifies what a class will do (the contract), but not how it should do it.

Packages

Packages serve two purposes:

  1. organization

  2. access control

Packages utilise Java's access control features so classes defined within a package can be made private to that package only and cannot be accessed outside, to do this do not apply an access modifier to the class member. This applies the default access which is public within the package but private outside.

Another access modifier that is used within packages is protected. Using this, allows a member to be public within the package, private outside but also accessible to subclasses of it that are outside the package.

Java Class Library Packages

The Java class library defines a large number of standard classes that are available to all programs. This library is referred to as the Java API, which is stored in packages. The top level being Java, which several subpackages below:

Subpackage
Description

java.lang

Contains general purpose classes

java.io

Contains I/O classes

java.net

Contains classes for networking

java.util

Contains utility classes, inc Collections framework

java.awt

Contains classes for Abstract Window Toolkit

Interfaces

If you want to be able to instruct what a class must do, but not how it should do it, then you can use an interface to enforce whichever class implements it must do. This is similar to an abstract method, which defines the signature of the method but provides no implementation. In this way, an abstract method specifies the interface to the method but not the implementation. In a similar way, an interface can be used to define methods with no body. The difference with using an interface is that it fully seperates a class interface from its implementation.

This means that an interface can only specify asbtract methods, whereas asbtract classes can define both asbtract and non abstract methods.

Interfaces can also only have static and final variables.

An interface allows you to utilise the 'one interface, multiple methods' aspect of polymorphism.

// Interface
interface Shape {
 
    // Abstract methods
    void draw();
    double area();
}
 
// Class 1
class Rectangle implements Shape {
 
    int length, width;
 
    Rectangle(int length, int width)
    {
        this.length = length;
        this.width = width;
    }
 
    @Override 
    public void draw()
    {
        System.out.println("Rectangle has been drawn ");
    }
 
    @Override 
    public double area()
    {
        return (double)(length * width);
    }
}

Variables in Interfaces

The following info is considered controversial and should be considered before implementing.

In large programs, a convenient way to share constant values is to create an interface without any methods and only contains these shared constants. Any class that requires these values can then implement the interface.

Extending

Interfaces can be extended by inheriting other interfaces. This means that a class must provide implementations for all methods in the interface chain.

Default Interface Methods

Starting with JDK 8, interfaces can define some default behaviour using the default method. One of the reasons for the reasons for the introduction of default method was to allow interfaces to be expanded without breaking existing code. As a general rule, default methods are a special purpose feature and should be used only if required.

Private Interface Methods

An interface can include a private method, but it can only be called by a default method or another private method defined in that interface. The primary purpose is to allow default methods to share a common piece of code, and so avoid duplication.

PreviousChapter 7. InheritanceNextChapter 9. Exception Handling

Last updated 3 years ago

Was this helpful?