









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
java interface
Typology: Study notes
Uploaded on 02/26/2015
1 / 15
This page cannot be seen from the preview
Don't miss anything!










Java Programming
ANIL SAROLIYA
/ Constant declarations / / only abstract Method signatures /
Interface can be used as an alternative to fulfill the requirement of multiple inheritance in Java_._
Syntax: Defining an interface
by classes. This is achieved by creating a class that implements the given interface as follows:
implemented by classes(as above) or extended by other interfaces(as below).:
interface NewInterfaceName extends InterfaceName1[,InterfaceName2,.....] { / New Constant declarations / / New abstract Method signatures / }
public class WorkingDog extends Dog implements Worker { private int hoursOfTraining; public void setHoursOfTraining(int hrs) { hoursOfTraining = hrs; } public int getHoursOfTraining() { return hoursOfTraining; } public void work() { speak(); System.out.println("I am a dog who works"); System.out.println("I have " + hoursOfTraining + " hours of professional training!"); } }
Example: The Animal and Dog Classes and a Worker Interface
public abstract class Animal { private String nameOfAnimal; public abstract void speak(); public String getAnimalName() { return nameOfAnimal; } public void setAnimalName(String name) { nameOfAnimal = name; } }
public class Dog extends Animal { public void speak() { System.out.println("Woof!"); } } public interface Worker { public void work(); }
//by default abstract
Animal Worker
Dog
WorkingDog
extends
extends
implements
Output: DemoWorkingDogs Class
OUTPUT: Simon, the Border Collie says Woof! Woof! I am a dog who works I have 40 hours of professional training!
Sophie, the German Shepherd says Woof! Woof! I am a dog who works I have 300 hours of professional training!
Interfaces: An informal way of realizing multiple inheritance
public class DemoDetectiveWorkingDogs { public static void main(String[] args) { DetectiveWorkingDogs aSheepHerder = new DetectiveWorkingDog(); aSheepHerder. setAnimalName ("Simon, the Border Collie"); //access from Animal class aSheepHerder. setHoursOfTraining (40);//access from WorkingDog class
System.out.println(aSheepHerder.getAnimalName() + " says "); aSheepHerder. speak() ;//access from Dog class, declared in Animal class aSheepHerder.work() ;//access from DetectiveWorkingDog class, declared in Worker interface aSheepHerder. speak() ;//access from Dog class, declared in Animal class aSheepHerder. detect() ;//access from DetectiveWorkingDog class, declared in Detective interface System.out.println(); } }
Use of the previous slide classes
Output: DemoWorkingDogs Class
Simon, the Border Collie says Woof! Woof! I am a dog who works I have 40 hours of professional training! Woof! Woof! I am also trained for detective work, to help the Police.
Animal Worker
Dog
DetectiveWorkingDog
extends
extends
implements
Detective
implements Informal Multiple Inheritance
DemoDetectiveWorkingDogs
Used by
Output: PizzaDemo Class
OUTPUT: Welcome to Antonio's Pizzeria We are having a special offer: a 12 inch pizza with four ingredients or a 16 inch pizza with one ingredient for only $11. With tax, that is only $12.