im trying to fugure out how to make a variable of an abstract class available for the subclass but not available externaly.
// The abstract
public abstract class Animal
{
protected String name;//<---This is the variable
public String getName()
{
return (name);
}
public void setName(String nm)
{
name=nm;
}
public abstract void speak();
}
//The subclass
public class Bird extends Animal
{
public Bird(String nm)
{
name = nm;
}
public void speak()
{
System.out.println("Tweet tweet chirp");
}
}
//The main class, i dont want this to work because i would prefer a setName() method
public static void main(String[] args)
{
Bird abird=new Bird("");
abird.name="Dodo";//<======Stop this
abird.setName("Dodo")//<====Leaving only this
}
this is just a practice and is not part of an important project. if anyone can help i would be very greatfull. thankyou