15 interface, Study notes of Computer Fundamentals

java interface

Typology: Study notes

2014/2015

Uploaded on 02/26/2015

unknown user
unknown user 🇮🇳

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Amity School of Engineering
B.Tech., CSE(5th Semester)
Java Programming
Topic: Interface
ANIL SAROLIYA
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download 15 interface and more Study notes Computer Fundamentals in PDF only on Docsity!

Amity School of Engineering

B.Tech., CSE(5th^ Semester)

Java Programming

Topic: Interface

ANIL SAROLIYA

What is Interface?

  • Except, How a class does? Interface explains, What a class does?
  • In many ways, Interface is very similar to abstract class but it marks with

interface keyword instead of abstract class keyword.

public interface InterfaceName

/ Constant declarations / / only abstract Method signatures /

  • Unlike abstract class which can also contain non-abstract methods, interface contain ONLY abstract methods and constants (will be clear in next slide).
  • Inside the interface body, its not essential to write abstract keyword(before method name). - Such keyword is implicitly (by default) attached to the respective members.

Interface can be used as an alternative to fulfill the requirement of multiple inheritance in Java_._

Syntax: Defining an interface

Implementing Interfaces

  • Interfaces are used like super-classes who properties are inherited

by classes. This is achieved by creating a class that implements the given interface as follows:

  • Note: Interfaces cannot be instantiated—they can only be

implemented by classes(as above) or extended by other interfaces(as below).:

class ClassName implements InterfaceName [, InterfaceName2, …]
// Body of Class

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!

  • As we all know, an interface is basically a kind of class—it contains

methods and variables, but they have to be only abstract

methods and public, static, final fields/variables.

  • Therefore, it is the responsibility of the class that implements an

interface to supply the code for methods.

  • In java, a class can implement any number of interfaces, but

cannot extend more than one class at a time. Means no Multiple

Inheritance in Java.

  • Therefore, interfaces are considered as an informal way of

realizing multiple inheritance in Java.

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

OUTPUT:

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.

Interface Abstract Class

 Multiple inheritance possible; a
class can inherit any number of
interfaces.
 Multiple inheritance not possible; a class
can inherit only one class.
 implements keyword is used to
inherit an interface.
 extends keyword is used to inherit a class.
 By default, all methods in an
interface are public and abstract ;
no need to tag it as public and
abstract explicitly.
 Methods have to be tagged as public and
abstract or both, if required.
 All methods of an interface need
to be overridden.
 Only abstract methods need to be
overridden.
 All variables declared in an
interface are by default public ,
static , or final.
 Variables, if required, have to be declared
as public , static , or final.
 Methods in an interface cannot be
static
 Non-abstract methods can be static.

Interface versus Abstract Class