13 Replies - 255 Views - Last Post: 10 July 2019 - 05:03 AM Rate Topic: -----

#1 Larry71   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 05-June 12

Inheritence problem

Posted 09 July 2019 - 04:46 AM

Hallo,

I have a problem with super and sub classes.

Suppose we have the following code:

public class Animal {

}

public Class Cat extends Animal {

}

public Class Dog extends Animal {

}

public Class AnimalHotel {

private ArrayList<Animal> animals;


}




If we accept that an AnimalHotel can have only one species but we don't know which one until we create one, how can we define its constructor properly?

public Class AnimalHotel {

private ArrayList<Animal> animals;

public AnimalHotel(String selectedAnimal)
 {

   if(selectedAnimal.equals("cat")
      this.animals = new ArrayList<Cat>();
   else
      this.animals = new ArrayList<Dog>();
}

}




Or should I change the programm structure by creating more classes?

Is This A Good Question/Topic? 0
  • +

Replies To: Inheritence problem

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3638
  • View blog
  • Posts: 16,706
  • Joined: 20-September 08

Re: Inheritence problem

Posted 09 July 2019 - 05:10 AM

Why not this?

public AnimalHotel(List<Animal> animals)


but of course you don't necessarily need a parameterized ctor. You could just give that class an addAnimal(Animal a) method

This post has been edited by g00se: 09 July 2019 - 05:11 AM

Was This Post Helpful? 0
  • +
  • -

#3 Larry71   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 05-June 12

Re: Inheritence problem

Posted 09 July 2019 - 05:18 AM

But if I try do this, I get type mismatch error.
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3638
  • View blog
  • Posts: 16,706
  • Joined: 20-September 08

Re: Inheritence problem

Posted 09 July 2019 - 05:34 AM

I'd have to see exactly what you're attempting to do. Please post code
Was This Post Helpful? 0
  • +
  • -

#5 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,842
  • Joined: 25-December 13

Re: Inheritence problem

Posted 09 July 2019 - 05:59 AM

Quote

an AnimalHotel can have only one species

Can you use Generics for the AnimalHotel?
   class AnimalHotel<T extends Animal> {

  ...
      AnimalHotel<Cat> forCats = new AnimalHotel<>();



This post has been edited by NormR: 09 July 2019 - 06:00 AM

Was This Post Helpful? 0
  • +
  • -

#6 Larry71   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 05-June 12

Re: Inheritence problem

Posted 09 July 2019 - 12:08 PM

OK, now the real code:

public class Match {

private String finalScore;

public Match () {

}

// Getters-setters follow

}

public class SoccerMatch extends Match {

private String halftimeScore;

public SoccerMatch() {
super;
halftimeScore = "";
}

// Getters-setters follow

}

public class BasketballMatch extends Match {

private String quarter1Result;
private String quarter2Result;
private String quarter3Result;
private String quarter4Result;

public BasketballMatch() {
super;
quarter1Result = "";
quarter2Result = "";
quarter3Result = "";
quarter4Result = "";

// Getters-setters follow
}

}

public class MatchDay {

ArrayList<Match> matches;

}




When I create a Matchday and try to use its setter for setting an ArrayList of SoccerMatch, I get errors.

main {

 MatchDay md = new MatchDay();

 ArrayList<SoccerMatch> soccermatches = new ArrayList<SoccerMatch>();
 soccermatches.add(new SoccerMatch());
 ...
 ...

 md.setMatches(soccermatches);

// Will not work 

}




But if I change the arraylist soccermatches to type Matches, it works but I cannot have the right to see or set the field halftimeScore of the class SoccerMatch.

This post has been edited by Larry71: 09 July 2019 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,842
  • Joined: 25-December 13

Re: Inheritence problem

Posted 09 July 2019 - 12:11 PM

Quote

I get errors.

Please copy the full text of the error messages and paste it here so we can see what you are talking about.
Was This Post Helpful? 0
  • +
  • -

#8 Larry71   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 05-June 12

Re: Inheritence problem

Posted 09 July 2019 - 12:16 PM

View PostNormR, on 09 July 2019 - 12:11 PM, said:

Quote

I get errors.

Please copy the full text of the error messages and paste it here so we can see what you are talking about.


Type mismatch. Cannot convert from Arraylist<SoccerMatch> to Arraylist<Match>.
Was This Post Helpful? 0
  • +
  • -

#9 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,842
  • Joined: 25-December 13

Re: Inheritence problem

Posted 09 July 2019 - 12:27 PM

What source line is that error on?
Was This Post Helpful? 0
  • +
  • -

#10 Larry71   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 05-June 12

Re: Inheritence problem

Posted 09 July 2019 - 12:30 PM

View PostNormR, on 09 July 2019 - 12:27 PM, said:

What source line is that error on?


10, when i try

md.setMatches(soccermatches);
Was This Post Helpful? 0
  • +
  • -

#11 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,842
  • Joined: 25-December 13

Re: Inheritence problem

Posted 09 July 2019 - 12:37 PM

Where is the method setMatches defined?
Was This Post Helpful? 0
  • +
  • -

#12 Larry71   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 05-June 12

Re: Inheritence problem

Posted 09 July 2019 - 12:39 PM

In class MatchDay, as a setter.
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15116
  • View blog
  • Posts: 60,494
  • Joined: 12-June 08

Re: Inheritence problem

Posted 09 July 2019 - 12:44 PM

Perhaps show the whole class the actual code. This has been a super vague, and misdirected, post thus far. :^:
Was This Post Helpful? 1
  • +
  • -

#14 Larry71   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 05-June 12

Re: Inheritence problem

Posted 10 July 2019 - 05:03 AM

View PostNormR, on 09 July 2019 - 12:27 PM, said:

What source line is that error on?


I would like to apologize, because you NormR were right. Problem solved now.
In fact, the error appeared when I was trying to iterate in this arraylist and I solved it, by casting each object of this list to subclass, at each iteration.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1