5 - Packages and Interface
5 - Packages and Interface
14-Feb-24
1
14-02-2024
Abstract Class
An abstract class cannot be instantiated, but other
classes are derived from it.
An Abstract class serves as a superclass for other
classes.
The abstract class represents the generic or abstract
form of all the classes that are derived from it.
A class becomes abstract when you place the abstract
key word in the class definition.
Abstract Method
An abstract method has no body and must be overridden in a
subclass.
An abstract method is a method that appears in a superclass,
but expects to be overridden in a subclass.
Notice that the key word abstract appears in the header, and
that the header ends with a semicolon.
public abstract void setValue(int value);
Any class that contains an abstract method is automatically
abstract.
If a subclass fails to override an abstract method, a compiler error will
result.
Abstract methods are used to ensure that a subclass implements the
method.
2
14-02-2024
Abstract Class
abstract class GraphicObject {
int x, y;
...
void moveTo(int newX, int newY) {
...
}
abstract void draw();
abstract void resize();
}
class Circle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}
Interface
An interface is a contract between its producer and client
The general format of an interface definition:
public interface InterfaceName
{
//(Method headers...)
}
All methods specified by an interface are public by default.
A class (as a producer) can implement one or more interfaces.
If a class implements an interface, it uses the implements
keyword in the class header.
public class person implements athelete
{
// must implement all amethods in the Interface
}
3
14-02-2024
Interface
An interface can contain field declarations:
all fields in an interface are treated as final and static.
Interface
interface Singer {
void sing();
void warmUpVoice();
}
interface Dancer {
void dance();
void stretchLegs();
}
4
14-02-2024
Difference
An interface cannot implement any methods, whereas
an abstract class can
A class can implement many interfaces but can have
only one superclass (abstract or not)
An interface is not part of the class hierarchy.
Unrelated classes can implement the same interface
5
14-02-2024
6
14-02-2024
14-Feb-24
14
7
14-02-2024
Packages
In general, a unique name had to be used for each class to
avoid name collisions
After a while, without some way to manage the name
space, you could run out of convenient, descriptive names
for individual classes.
Java provides a mechanism for partitioning the class name
space into more manageable chunks. - Package
A package in Java is used to group related classes.
Think of it as a folder in a file directory. We use packages
to avoid name conflicts, and to write a better
maintainable code.
The package is both a naming and a visibility control
mechanism. --- HOW??
15
Uses
Packages are used for:
A Preventing naming conflicts.
For example there can be two classes with name
college.staff.ee.Employee
Providing controlled access:
protected and default have package level access
16
8
14-02-2024
Defining a Package
To create a package simply include a package command
as the first statement in a Java source file.
Any classes declared within that file will belong to the
specified package.
The package statement defines a name space in which
classes are stored.
If you omit the package statement, the class names are
put into the default package, which has no name.
17
Defining a Package
18
9
14-02-2024
Defining a Package
This forces the compiler to create the "mypack" package.
Access Protection
Classes and packages are both means of encapsulating and
containing the name space and scope of variables and
methods.
Classes act as containers for data and code.
The class is Java’s smallest unit of abstraction.
Packages act as containers for classes and other
subordinate packages.
Java addresses four categories of visibility for class
members:
Subclasses in the same package
Non-subclasses in the same package
Subclasses in different packages
Classes that are neither in the same package nor subclasses
10
14-02-2024
Access Protection
Public - Anything declared public can be accessed from
anywhere.
Private - Anything declared private cannot be seen outside
of its class.
Package - When a member does not have an explicit
access specification, it is visible to subclasses as well as to
other classes in the same package. - This is the default
access.
Protected - If you want to allow an element to be seen
outside your current package, but only to classes that
subclass your class directly
Access Protection
11
14-02-2024
Demo
12
14-02-2024
import packagename.classname;
Example: import java.util. Scanner;
or
import packagename.*;
Example: import java.util.*;
13
14-02-2024
Info bits!!!!
The star form may increase compilation time—especially if you
import several large packages.
For this reason it is a good idea to explicitly name the classes
that you want to use rather than importing whole packages.
However, the star form has absolutely no effect on the run-time
performance or size of your classes.
All of the standard Java classes included with Java are stored in
a package called java.
The basic language functions are stored in a package inside of
the java package called java.lang.
Normally, you have to import every package or class that you
want to use, but since Java is useless without much of the
functionality in java.lang, it is implicitly imported by the
compiler for all programs.
Package Types
Packages are divided into two categories:
Built-in Packages (packages from the Java API)
User-defined Packages (create your own packages)
Built-in Packages
The Java API is a library of prewritten classes, that are free to use,
included in the Java Development Environment.
The complete list can be found at Oracles website:
https://docs.oracle.com/javase/8/docs/api/.
User-defined Packages
These are the packages that are defined by the user.
To create your own package, you need to understand that Java
uses a file system directory to store them. Just like folders on your
computer
14
14-02-2024
Example1-Package
15
14-02-2024
Example2-Package
Example 3- Package
16
14-02-2024
The End
17