-1
public class Animal
{
    public String name;
    public boolean legs;
    public boolean eyes;

    public Animal(String name, boolean legs, boolean eyes)
    {
        this.name = name;
        this.legs = legs;
        this.eyes = eyes;
    }
}

public class Dog extends Animal
{
    String name;
    boolean legs;
    boolean eyes;
    public boolean vacinated;
    public boolean rabid;
    public Dog(boolean vacinated, boolean rabid, String name, boolean legs, boolean eyes)
    {
        super(name, legs, eyes);
        this.vacinated = vacinated;
        this.rabid = rabid;
    }
}

public class Bulldog extends Dog
{
    String name;
    boolean legs;
    boolean eyes;
    public boolean vacinated;
    public boolean rabid;
    public String breedtype;

    public Bulldog(String breedtype, String name, boolean legs, boolean eyes, boolean vacinated, boolean rabid)
    {
        super(vacinated, rabid, name, legs, eyes);
        this.breedtype = breedtype;
    }
}

As you can see, if this keeps going on and on, in other words, if I have a really long inheritance line, would I seriously need to list out every single variable over and over again? I just feel like there is a a much more efficient way to do this.

Andrew
  • 3
  • 1
  • *would I seriously need to list out every single variable over and over again?* - No. Your variables are public, you can access them from child classes. If you decide to follow the standard and make them private you will still be able to acces them via setters and getters. So, in both cases, no, you don't have to repeat them. – BackSlash Nov 23 '18 at 08:08
  • So i dont need to re-declare them, but i DO need to have them in my constructors as parameter variables? – Andrew Nov 23 '18 at 08:11
  • No. You currently have multiple `name` variables in the same class. Since member variables aren't polymorphic, this will near-certainly not do what you expect. – Andy Turner Nov 23 '18 at 08:12
  • @Andrew Yes, you still need them in constructor. But you can call `super(...)` as you are already doing to avoid re-assigning them. – BackSlash Nov 23 '18 at 08:12
  • You need a tutorial in objects and classes, not this site. – Raedwald Nov 23 '18 at 09:38

3 Answers3

0

Your classes are violated OOP. You should define common properties which could be inherited to child classes by protected. It means child classes can access those

Secondly, the common properties should be defined in parent class and dont need to be re-created in child ones.

P/S: updated by BlackFlash comment. I will use private modifier for such common properties. In the scope of this question, child classes don't need to access parent's properties.

Your classes should be changed as follows:

public class Animal
{
  private String name;
  private boolean legs;
  private boolean eyes;

  public Animal(String name, boolean legs, boolean eyes)
  {
    this.name = name;
    this.legs = legs;
    this.eyes = eyes;
 }
}

public class Dog extends Animal
{
   private boolean vacinated;
   private boolean rabid;
   private Dog(boolean vacinated, boolean rabid, String name, boolean legs, boolean eyes)
    {
      super(name, legs, eyes);
      this.vacinated = vacinated;
      this.rabid = rabid;
  }
}

public class Bulldog extends Dog
{
   private String breedtype;
   public Bulldog(String breedtype, String name, boolean legs, boolean eyes, boolean vacinated, boolean rabid)
   {
    super(vacinated, rabid, name, legs, eyes);
    this.breedtype = breedtype;
  }
}
Tran Ho
  • 1,442
  • 9
  • 15
  • 1
    *You should define common properties which could be inherited to child classes by protected* - Actually, you should make them private and define setters and getters instead. – BackSlash Nov 23 '18 at 08:32
  • @BackSlash: why cannot use `protected` for common properties? – Tran Ho Nov 23 '18 at 08:41
  • 1
    @htpvl For example, because `protected` variables can be seen by classes inside the same package too. This means that if at some point you need to manipulate those variables or change some behaviour, you'll have to make a breaking change, requiring all the developers who used that variable to update their code. If you make the variable private and write setters/getters, you'll be able to control that variable and make any change to those methods without breaking other code. https://stackoverflow.com/a/8353371/1759845 – BackSlash Nov 23 '18 at 08:48
  • @BackSlash: thanks for this useful info. Actually I don't know `protected` ones could be accessed in same package. I learnt OOP by C++ and there is no package in that langugae. anw, it is a new learn. tks again. – Tran Ho Nov 23 '18 at 08:53
  • @htpvl Check this for more details: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html – BackSlash Nov 23 '18 at 08:54
0

This is a violation of the whole OOP concept. You can have common variables in the parent class as protected variables so that it can be accessed by that class and its children.

Not only variables but also common methods can be defined and implemented in parent class as protected.

Read more here

0

Here's a simpler example of one of the problems with what you're doing:

class A {
  String a;

  A(String a) { this.a = a; }
}

class B extends A {
  String a;

  A(String a) { super(a); }
}

This code will probably give results that you don't expect:

B b = new B("hello");
System.out.println(b.a);  // null

This may be surprising to you, because it might appear that you initialized a in the constructor of A, via the super(a) call.

But what's more surprising is that if you cast it to A, it doesn't print null:

System.out.println(((A) b).a);  // hello

which looks odd: it prints something different when you treat it as a different type.

The problem here is that fields are not polymorphic in Java: the a in B hides the a in A. Both fields are there, but when you refer to a on an variable of type B, you get a different field to when you refer to a on a variable of type A.

For this to work "less confusingly", don't repeat the declaration of a in B:

class A {
  String a;

  A(String a) { this.a = a; }
}

class B extends A {
  A(String a) { super(a); }
}

Now:

B b = new B("hello");
System.out.println(b.a);  // hello
System.out.println(((A) b).a);  // hello
Andy Turner
  • 137,514
  • 11
  • 162
  • 243