0
public abstract class PlayableItem{
    private int duration;
    protected PlayableItem(int duration){
    this.duration = duration;
    }

    //getters and setters 

    public abstract String play();


public Song extends PlayableItem(){
    private String name = name;
    private String title = title;

    protected Song(int duration, String name, String title){
        super(duration);
        this.name = name;
        this.title = title;
    }

    //getters and setters 

    @Override
    public String play(){
    return "artist name - " + name + "song title - " + title + "duration - "
            + duration;
    }

For some reason I keep getting an error on the Override method, specifically on duration. But my professor wants me to keep the private int field in the abstract class. All the lessons we had on abstract classes used private fields but never actually used as part of the parameter in the subclasses. I can't help but think that we overlooked something very important during our lessons because clearly it seems that the fields in an abstract class is treated differently from regular superclass fields.

Namely, in a regular superclass of Animals that has a private String field type

public class Animals{
    private String type;

    public Animals(String type){
         this.type = type;
    }
 }

public class Pet extends Animals{
    private String color;

    public Pet(String type, String color){
        super(type);
        this.color = color;
    }
 }

when creating a Pet object, you are required to use the Parent class parameter but for abstract classes it seems different. Can someone explain?

  • there is no difference between private fields in abstract/non-abstract superclasses. if you want to get access to private field in another class (in your case - in subclass) - you should either change the visibility of this field (there are 4 visibity levels in Java - private, package, protected and public) or use `get`-ters – star67 Oct 06 '18 at 19:56
  • Yes, but why do I keep getting an error on my example with the abstract class? – swordlordswamplord Oct 06 '18 at 19:58
  • How would I use the getter? and where? – swordlordswamplord Oct 06 '18 at 19:59
  • use `+ getDuration()` instead of `+ duration` in last line of your example – star67 Oct 06 '18 at 20:00
  • okay I did that and it worked. Can you please explain why I have to do that instead of simply writing duration? – swordlordswamplord Oct 06 '18 at 20:01
  • Because in one of my other examples, we have a private field in the abstract superclass and we did not have to use getColor() in our subclass, much less use it in our parameter. – swordlordswamplord Oct 06 '18 at 20:03
  • that's because of visibility modifier `private` for field `duration`. it makes this field non-accessible everywhere except the itself. this link may be useful - https://stackoverflow.com/questions/215497/what-is-the-difference-between-public-protected-package-private-and-private-in – star67 Oct 06 '18 at 20:04
  • `color` is not defined in superclass – star67 Oct 06 '18 at 20:05

0 Answers0