0% found this document useful (0 votes)
42 views16 pages

CH 11

Packages allow for organizing classes into logical groups and providing a namespace. To place classes in a package, include a package statement with the package name at the top of the source file. Interfaces define public abstract methods that classes can implement to define common behaviors regardless of class hierarchy. Abstract classes cannot be instantiated directly but provide a template for subclasses to implement abstract methods.

Uploaded by

manojabhole
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views16 pages

CH 11

Packages allow for organizing classes into logical groups and providing a namespace. To place classes in a package, include a package statement with the package name at the top of the source file. Interfaces define public abstract methods that classes can implement to define common behaviors regardless of class hierarchy. Abstract classes cannot be instantiated directly but provide a template for subclasses to implement abstract methods.

Uploaded by

manojabhole
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Packages

• A package is a set of related classes. They provide


a structuring mechanism.
• To put classes in a package, you must place a line
package packagename;
package com.hotsmann.bigjava;
as the first line in the source file containing
classes.
• If you did not include package statement at the
top of your source file, its classes are placed in the
default package.
Importing packages
• The import directive lets you refer to a class of a
package by its class name, without the package
prefix.
import java.awt.Color;
• There is no need to import the classes in the
java.lang package explicitly. This package contains
most basic Java classes.
• There is no need to import the other classes in the
same package.
How to program with packages
• Step 1: Come up with a package name : Say
homework1.
• Step 2: Pick a base directory: Say
C:\CIS381\Assignments
• Step 3: Make a subdirectory from the base
directory that matches your package name.
mkdir C:\CIS381\Assignments\homework1
• Step 4: Place your source files into the package
subdirectory.
Say your homework1 consists of BankAccount.java
and BankAccountTest.java, then put them in
C:\CIS381\Assignments\homework1
How to program with packages
• Step 5: Use the package statement in each of your
source files.
package homework1; as the first line of code in
your BankAccount.java and BankAccountTest.java.
• Step 6: Change to the base directory to compile
your files.
cd \CIS381\Assigments
javac homework1\BankAccount.java
javac homework1\BankAccountTest.java
How to program with packages
• Step 7: Run your program from the base
directory
cd CIS381/Assignments
java homework1.BankAccountTest
Interfaces
• Interfaces declare public abstract methods
• Cannot define any other methods
• Interfaces can declare fields
• Fields have constant value (public static final)

• Classes implement interfaces


• Class can implement more than 1 interface

• Must implement all the interfaces’ methods


Methods in an Interface
• All methods are abstract
• Adding abstract qualifier is optional
• Methods cannot have any implementation
• Implementation left strictly to Classes

• Methods public by default


• Methods cannot be native, static,

synchronized, or final
Pet Animal

Canine Feline

Dog Wolf Cat Tiger Lion

But remember we said that Java does not support


multiple inheritance. There is a solution however:
interfaces.
Interfaces
• Interface: A collection of constants and
abstract methods that cannot be instantiated.
• A class implements an interface by providing
method implementations for each of the
abstract methods defined in the interface.

public class Dog extends Canine implements Pet


Explicitly typing
Interfaces in public and
abstract is not
public interface Pet { necessary since
public abstract void beFriendly(); they MUST be
public abstract void play(); public and
} abstract
public class Dog extends Canine implements Pet {
public void beFriendly() {
wagTail();
}
Must implement
public void play() {
these methods
chaseBall();
since they are in
} Pet
. . . all the other Dog methods . . .
}
Interfaces vs. Subclasses
• Make a subclass only when you want to make
a more specific version of a class.
• Use an interface when you want to define a
role that other classes can play, regardless of
where those classes are in the inheritance
tree.
Polymorphism via Interfaces
• An interface reference variable can be used to
refer to any object of any class that implements
that interface.
• This works the same with superclasses.
Pet myPet = new Dog();
• The same side effects of polymorphism occur
with interfaces as with inheritance.
Abstract Class
• Abstract classes cannot be instantiated
• Any class could be declared abstract
• Subclass need not override all abstract methods
• But subclass will also be abstract

• Can instantiate subclasses of abstract class only if


all inherited abstract methods overriden
• Can still use abstract classes as a reference
variable, for the purposes of polymorphism.
• An abstract class has no use until it is extended!
• A class that is not abstract is called concrete.
Abstract Methods
• An abstract method has no body and is marked
with the keyword abstract.
public abstract void eat();

• If a method is abstract, the class it is contained


in must also be abstract.
• Abstract methods help the programmer to
provide a protocol for a group of subclasses.
• The first concrete class in the inheritance
hierarchy must implement the abstract method
(i.e. override it and provide it a body)
abstract
• abstract methods must be overridden and contain
no implementation
• abstract classes contain one or more methods and
cannot be instantiated
• interfaces
• allows specification form of inheritance

• all methods are abstract

• no instance variables

• constants are OK

• a class implements an interface

• can implement multiple interfaces

• similar to an abstract class?


Comparable Interface
• Defined in the java.lang package
• Only contains one method: compareTo which
takes an object as a parameter and returns an
integer. Returns a negative integer, zero, or a
positive integer as this object is less than, equal
to, or greater than the specified object.
• Provides a common mechanism for comparing
one object to another.
• http://java.sun.com/j2se/1.5.0/docs/api/java/lan
g/Comparable.html

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy