This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
actually i created a Vehicle interface which has methods engine(),body() and wheels(). Car class and Bike class implement this.
I want to create a variable called color and wheel in Vehicle so that every class which inherits it has these. But unfortunately, I cannot make it. Can anybody please help me in making variables which are used by all the inheriting classes of Vehicle?
Thanks in advance
You can't, unless they are constants (static and final). Variables are implementation details, so they shouldn't be part of an interface.
Instead, what you can do is create an abstract class AbstractVehicle that implements Vehicle, and give that class the color variable (which should be private) and give it getter and/or setter methods to view or change the vehicles color. These methods are then inherited by Car and Bike, which both extend AbstractVehicle.