0% found this document useful (0 votes)
55 views9 pages

Lecture Notes and Lab Handouts Object Oreinted Programming (Itec / Seng - 321) Week. 9 Interfaces

The document discusses interfaces and packages in Java. Interfaces allow a class to inherit behavior from multiple sources through abstraction. Packages are used to prevent naming conflicts and make classes easier to locate by grouping related types. Examples are provided to demonstrate interfaces and creating packages in Java.

Uploaded by

Umm E Farwa Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views9 pages

Lecture Notes and Lab Handouts Object Oreinted Programming (Itec / Seng - 321) Week. 9 Interfaces

The document discusses interfaces and packages in Java. Interfaces allow a class to inherit behavior from multiple sources through abstraction. Packages are used to prevent naming conflicts and make classes easier to locate by grouping related types. Examples are provided to demonstrate interfaces and creating packages in Java.

Uploaded by

Umm E Farwa Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

LECTURE NOTES AND LAB HANDOUTS

OBJECT OREINTED PROGRAMMING (ITEC / SENG – 321 ) WEEK. 9

INTERFACES:

In Java, a class can inherit directly from only one class; that is, a class can extend only one class. To allow
a class to inherit behavior from multiple sources, Java provides the interface.
An interface typically specifies behavior that a class will implement. Interface members can be any of the
following:
 classes
 constants
 abstract methods
 other interfaces

Course Instructor: Nazish Basir Page: 1/9


Institute of Information and Communication Technology, University of Sindh.
LECTURE NOTES AND LAB HANDOUTS
OBJECT OREINTED PROGRAMMING (ITEC / SENG – 321 ) WEEK. 9

PROGRAM 1: Program demonstrating interface Vehicle


interface Vehicle {

// all are the abstract methods.


void changeGear(int a);
void speedUp(int a);
void applyBrakes(int a);
}

Course Instructor: Nazish Basir Page: 2/9


Institute of Information and Communication Technology, University of Sindh.
LECTURE NOTES AND LAB HANDOUTS
OBJECT OREINTED PROGRAMMING (ITEC / SENG – 321 ) WEEK. 9

class Bicycle implements Vehicle{


int speed;
int gear;

// to change gear @Override


public void changeGear(int newGear){
gear = newGear;
}

// to increase speed @Override


public void speedUp(int increment){
speed = speed + increment;
}

// to decrease speed @Override


public void applyBrakes(int decrement){
speed = speed - decrement;
}

public void printStates() {


System.out.println("speed: " + speed
+ " gear: " + gear);
}
}

class Bike implements Vehicle {


int speed;
int gear;

// to change gear @Override


public void changeGear(int newGear){
gear = newGear;
}

// to increase speed @Override


public void speedUp(int increment){
speed = speed + increment;
}

// to decrease speed @Override


public void applyBrakes(int decrement){

speed = speed - decrement;


}

public void printStates() {


System.out.println("speed: " + speed
+ " gear: " + gear);
}

Course Instructor: Nazish Basir Page: 3/9


Institute of Information and Communication Technology, University of Sindh.
LECTURE NOTES AND LAB HANDOUTS
OBJECT OREINTED PROGRAMMING (ITEC / SENG – 321 ) WEEK. 9
}

class GFG {

public static void main (String[] args) {

// creating an inatance of Bicycle


// doing some operations
Bicycle bicycle = new Bicycle();
bicycle.changeGear(2);
bicycle.speedUp(3);
bicycle.applyBrakes(1);

System.out.println("Bicycle present state :");


bicycle.printStates();

// creating instance of bike.


Bike bike = new Bike();
bike.changeGear(1);
bike.speedUp(4);
bike.applyBrakes(3);

System.out.println("Bike present state :");


bike.printStates();
}
}

PROGRAM 2:

interface Appliances{
abstract public void onSwitchOn();
abstract public void onSwitchOff();
}

class Bulb implements Appliances{


public void onSwitchOn(){
glow();
}
public void onSwitchOff(){
noGlow();
}
public void glow(){

System.out.println("Glowing...");
}
public void noGlow(){
System.out.println("Bulb is not glowing");
}
}

Course Instructor: Nazish Basir Page: 4/9


Institute of Information and Communication Technology, University of Sindh.
LECTURE NOTES AND LAB HANDOUTS
OBJECT OREINTED PROGRAMMING (ITEC / SENG – 321 ) WEEK. 9

class Fan implements Appliances{

public void onSwitchOn(){


rotate();
}
public void onSwitchOff(){
noRotate();
}
public void rotate(){
System.out.println("Rotating");
}
public void noRotate(){
System.out.println("Not Rotating");
}
}

class TV implements Appliances{


public void onSwitchOn(){
tvon();
}
public void onSwitchOff(){
tvoff();
}
public void tvon(){

System.out.println("Tv is on");

}
public void tvoff(){
System.out.println("Tv is off");
}
}

class Switch {
Appliances ap;
public Switch(Appliances ap){
this.ap = ap;
}

public void on(){


ap.onSwitchOn();
}
public void off(){
ap.onSwitchOff();
}
}

Course Instructor: Nazish Basir Page: 5/9


Institute of Information and Communication Technology, University of Sindh.
LECTURE NOTES AND LAB HANDOUTS
OBJECT OREINTED PROGRAMMING (ITEC / SENG – 321 ) WEEK. 9

class AppDemo{
public static void main(String arg[]){

Bulb b= new Bulb();


Switch s1 = new Switch(b);

Fan f = new Fan();


Switch s2 = new Switch(f);

TV t = new TV();
Switch s3 = new Switch(t);

s1.on();
s2.on();
s3.on();
}
}

PACKAGES
Packages are used in Java in order to prevent naming conflicts, to control access, to make
searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.
A Package can be defined as a grouping of related types (classes, interfaces, enumerations and
annotations) providing access protection and namespace management.
Some of the existing packages in Java are:
 java.lang − bundles the fundamental classes
 java.io − classes for input , output functions are bundled in this package
Programmers can define their own packages to bundle group of classes/interfaces, etc. It is a good
practice to group related classes implemented by you so that a programmer can easily determine that
the classes, interfaces, enumerations, and annotations are related.
Since the package creates a new namespace there won't be any name conflicts with names in other
packages. Using packages, it is easier to provide access control and it is also easier to locate the related
classes.

Course Instructor: Nazish Basir Page: 6/9


Institute of Information and Communication Technology, University of Sindh.
LECTURE NOTES AND LAB HANDOUTS
OBJECT OREINTED PROGRAMMING (ITEC / SENG – 321 ) WEEK. 9

CREATING A PACKAGE
While creating a package, you should choose a name for the package and include a package statement
along with that name at the top of every source file that contains the classes, interfaces, enumerations,
and annotation types that you want to include in the package.
The package statement should be the first line in the source file. There can be only one package statement
in each source file, and it applies to all types in the file.
If a package statement is not used then the class, interfaces, enumerations, and annotation types will be
placed in the current default package.
To compile the Java programs with package statements, you have to use -d option as shown below:

javac -d Destination_folder file_name.java

Then a folder with the given package name is created in the specified destination, and the compiled class
files will be placed in that folder.

For Example
Let us look at an example that creates a package called animals. It is a good practice to use names of
packages with lower case letters to avoid any conflicts with the names of classes and interfaces.
Following package example contains interface named animals −

/* File name : Animal.java */


package animals;

interface Animal {
public void eat();
public void travel();
}

Now, let us implement the above interface in the same package animals:

package animals;
/* File name : MammalInt.java */

public class MammalInt implements Animal {


public void eat() {
System.out.println("Mammal eats");
}
public void travel() {
System.out.println("Mammal travels");
}
public int noOfLegs() {
return 0;
}
public static void main(String args[]) {
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}

Course Instructor: Nazish Basir Page: 7/9


Institute of Information and Communication Technology, University of Sindh.
LECTURE NOTES AND LAB HANDOUTS
OBJECT OREINTED PROGRAMMING (ITEC / SENG – 321 ) WEEK. 9

Now compile the java files as shown below:


javac -d . Animal.java
javac -d . MammalInt.java

Now a package/folder with the name animals will be created in the current directory and these class files
will be placed in it as shown below.

To Run:
Java animal.MammalInt

If a class wants to use another class in the same package, the package name need not be used. Classes in
the same package find each other without any special syntax.

Example
Here, a class named Boss is added to the payroll package that already contains Employee. The Boss can
then refer to the Employee class without using the payroll prefix, as demonstrated by the following Boss
class.

package payroll;
public class Boss {
public void payEmployee(Employee e) {
e.mailCheck();
}
}

What happens if the Employee class is not in the payroll package? The Boss class must then use one of
the following techniques for referring to a class in a different package.
The fully qualified name of the class can be used. For example:
payroll.Employee

The package can be imported using the import keyword and the wild card (*). For example:
import payroll.*;

The class itself can be imported using the import keyword. For example:
import payroll.Employee;

A class file can contain any number of import statements. The import statements must appear after the
package statement and before the class declaration.

THE DIRECTORY STRUCTURE OF PACKAGES


Two major results occur when a class is placed in a package:
 The name of the package becomes a part of the name of the class, as we just discussed in the
previous section.
 The name of the package must match the directory structure where the corresponding bytecode
resides.

Course Instructor: Nazish Basir Page: 8/9


Institute of Information and Communication Technology, University of Sindh.
LECTURE NOTES AND LAB HANDOUTS
OBJECT OREINTED PROGRAMMING (ITEC / SENG – 321 ) WEEK. 9

For Example
A company's Internet domain name is apple.com, then all its package names would start with com.apple.
Each component of the package name corresponds to a subdirectory.
Example − The company had a com.apple.computers package that contained a Dell.java source file, it
would be contained in a series of subdirectories like this:

....\com\apple\computers\Dell.java

At the time of compilation, the compiler creates a different output file for each class, interface and
enumeration defined in it. The base name of the output file is the name of the type, and its extension
is .class.

For example

// File Name: Dell.java


package com.apple.computers;

public class Dell {


}

class Ups {
}

Now, compile this file as follows using -d option:


javac -d . Dell.java

The files will be compiled as follows:


\com\apple\computers\Dell.class
\com\apple\computers\Ups.class

To Run:
java com.apple.computers.Dell

Course Instructor: Nazish Basir Page: 9/9


Institute of Information and Communication Technology, University of Sindh.

You might also like