aspose file tools
The moose likes Beginning Java and the fly likes Array of interface type  -Where do we use it in real time applications? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Array of interface type  -Where do we use it in real time applications?" Watch "Array of interface type  -Where do we use it in real time applications?" New topic
Author

Array of interface type -Where do we use it in real time applications?

archu sweet
Ranch Hand

Joined: Mar 07, 2011
Posts: 66

interface Sporty {
void beSporty();
}
class Ferrari extends Car implements Sporty {
public void beSporty() {
// implement cool sporty method in a Ferrari-specific way
}
}
class RacingFlats extends AthleticShoe implements Sporty {
public void beSporty() {
// implement cool sporty method in a RacingShoe-specific way
}
Initializing an Array (Exam Objective 1.3) 221
222 Chapter 3: Assignments
}
class GolfClub { }
class TestSportyThings {
public static void main (String [] args) {
Sporty[] sportyThings = new Sporty [3];
sportyThings[0] = new Ferrari(); // OK, Ferrari
// implements Sporty
sportyThings[1] = new RacingFlats(); // OK, RacingFlats
// implements Sporty
sportyThings[2] = new GolfClub();
// Not OK; GolfClub does not implement Sporty
// I don't care what anyone says
}
}



I jus swa that array was declared as interface type i have doubt regarding why it was declared as such ?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9950
    
    6

because you want an array of Sporty things.

You can't declare your array as one of Cars, because running shoes are sporty. You can't declare is as AthleticShoes, because then you couldn't put in cars.

You don't want to declare it as an array of Objects, because then you COULD put in golfClubs which are NOT sporty (according to this code).


Never ascribe to malice that which can be adequately explained by stupidity.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array of interface type -Where do we use it in real time applications?
 
Similar Threads
interface and abstract real time Advantages
Conversion
difference between abstract class and interface?
Sun Cirtification
Anonymous Inner Class Problem