Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old March 19th, 2007, 01:13 AM
Authorized User
Join Date: Mar 2007
Location: Hyderabad, A.P, India.
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default interfaces

why need of intefaces

Old April 23rd, 2007, 05:21 AM
Authorized User
Join Date: Nov 2005
Location: , , United Kingdom.
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts

Java is a single-inheritance language which means each class can extend only one other class. This is because languages that are multi-inheritance languages such as C++ where you can make a class extend more than one other class are rather prone to subtle error as a result of this because multi-inheritance often leads to complex class hierarchies and name-clash errors and all sorts of horrible bugs. It is for this reason that java has been designed to be single-inheritance language.

But there are occasions where you would want a class to inherit the characteristics of more than one other class. This is when interfaces came to be useful; a class in java may only be allowed to extend one other class but a class in java can implement any number of interfaces. This creates a similar effect as multi-inheritance but, because of the severe constraints on what an interface can have, this multi-inheritance-like effect does not cause the usual complications that comes from true multi-inheritance.

Suppose you want a number of classes to inherit a method called getNoise() that returns a string that represents a ‘noise’ that objects of those classes are said to make. You could do this by making an interface called NoisyThing that has the public abstract method getNoise() and then you can make a number of classes such as ones named Dog and Cat and Gun etc implement the NoisyThing interface. These classes may otherwise be completely unrelated because, for example, the class Gun may be in a completely different class lineage from the class Cat in the single-inheritance class hierarchy.

The class Dog could be written as:

class Dog extends Animal implements NoisyThing{

//overrides the abstract method inherited from NoisyThing
    public String getNoise(){return “bark”; }


While the class Gun could be written as:

class Gun extends Weapon implements NoisyThing{

//overrides the abstract method inherited from NoisyThing
    public String getNoise(){return “bang”; }


Now it is possible to write a method, perhaps in some other unrelated class, that accepts an object of type NoisyThing as an argument and then invoke the getNoise() method on that object:

public static void printNoise( NoisyThing n ){
       System.out.println( n.getNoise() ); }

Then if you now call the above method with:

Dog d = new dog();

Gun g = new Gun();

…printNoise( d );
…printNoise( g );

Then the output to system.out will be:


The point is, given that java is a single-inheritance language, if you want to get this precise above behaviour from your java program and java did not have interfaces, then this would impossible. So this is an example of why java has interfaces.


Similar Threads
Thread Thread Starter Forum Replies Last Post
returning interfaces mike72 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 April 17th, 2008 05:53 AM
Instantiating Interfaces simsdagr8 C# 2005 4 November 30th, 2006 07:37 AM
when implementing two interfaces ratheesh_param ASP.NET 1.x and 2.0 Application Design 5 November 10th, 2006 01:52 PM
[CH04] Interfaces Norm 2782 BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 3 June 16th, 2005 04:56 AM
Polymorphism with Interfaces digby_dog VB.NET 2002/2003 Basics 2 May 11th, 2005 12:50 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.