1

I have the following abstract class :

abstract class OutDoorGames
{
    protected String name;
    protected int num_players;
    protected int min_of_play;

    abstract void game (String a,int g,int r);
};  

I want the public void game function to assign values to the data members to the objects.

This Is The Class Which Implements The abstract void game (String a,int g,int r) :

class Cricket extends OuterDoorGames
{
    public void game (String name,int num_players,int min_of_play)
    {
        OutDoorGames::name = name ; 
        OutDoorGames::num_players = num_players ; 
        OutDoorGames::min_of_play = min_of_play ; 
    }
} 

But its giving Error When Compiled..

I also tried this :

class Cricket extends OuterDoorGames
{
    public void game (String name,int num_players,int min_of_play)
    {
        this.name = name ; 
        this.num_players = num_players ; 
        this.min_of_play = min_of_play ; 
    }
} 

This is also not working... I am new to Java...How Should I Implement This Function.....??

0

3 Answers 3

1

First of all you got the names wrong.. Notice the name you gave to the parent abstract class OutDoorGames is not the same as you wrote when extended it OuterDoorGames.

As a practice, you can also try making a constructor in the parent abstract class which sets the 3 members and then just call it from within the constructor of the child class

An example of such usage:

public abstract class Bicycle {

// the Bicycle class has three fields
public int cadence;
public int gear;
public int speed;

// the Bicycle class has one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
    gear = startGear;
    cadence = startCadence;
    speed = startSpeed;
}

// the Bicycle class has four methods
public void setCadence(int newValue) {
    cadence = newValue;
}

public void setGear(int newValue) {
    gear = newValue;
}

public void applyBrake(int decrement) {
    speed -= decrement;
}

public void speedUp(int increment) {
    speed += increment;
}    }

    public class MountainBike extends Bicycle {

    // the MountainBike subclass adds one field
    public int seatHeight;

    // the MountainBike subclass has one constructor
    public MountainBike(int startHeight,
                        int startCadence,
                        int startSpeed,
                        int startGear) {
        super(startCadence, startSpeed, startGear);
        seatHeight = startHeight;
    }   

    // the MountainBike subclass adds one method
    public void setHeight(int newValue) {
        seatHeight = newValue;
    }   
}

Notice the super call which activates the parent class constructor. Just change it to accept your 3 arguments in the parent class

1

class Cricket extends OuterDoorGames

If you are getting a OuterDoorGames cannot be resolved to a type, then it's a typo. Your abstract class is named as "OutDoorGames". Once you correct that, your Cricket class should just work fine.

0

The only error I can see with the second one is that the class name is wrong. The first class you defined (your abstract class) is called "OutDoorGames", but you're trying to extend "OuterDoorGames".

Not the answer you're looking for? Browse other questions tagged or ask your own question.