Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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

K.Mallikarjunarao
__________________
K.Mallikarjunarao
Reply With Quote
  #2 (permalink)  
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
Default

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:

bark
bang

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.

            AndrewH:)

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 05:02 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.