Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
archu sweet
Ranch Hand
Posts: 66
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!