-3

Can a subclass also be a superclass of another subclass in Java? Perhaps this is not the best example, but consider the following classes:

public class Animal { }
public class Dog extends Animal { } 
public class Cat extends Animal { } 
public class Siamese extends Cat { }
public class JackRussel extends Dog { }

Does inheritance allow for this sort of behaviour?

Given that JackRussels would require the methods and properties of both an Animal and a Dog, and Siamese's would require the methods and properties of both Animal and Cat.

If not, is there a generalised approach I could take to achieve this sort of behaviour?

Cheers

anditpainsme
  • 601
  • 1
  • 7
  • 14
  • The language is Java in this case. I assumed the question would be fairly language agnostic. I will edit that in. – anditpainsme May 21 '15 at 06:12
  • 7
    Yes, Java allows this. Why didn't you try it for yourself? – Vince May 21 '15 at 06:14
  • Yes , Consider `Mammal` as a super class to dog but a sub class to `Living` – Kenneth Clark May 21 '15 at 06:16
  • All classes *are* subclasses of Object, so of course it's possible. – JB Nizet May 21 '15 at 06:17
  • 4
    You have code, and you want to check whether or not it's valid... surely `javac` is the best way of determining that rather than Stack Overflow. If you're not asking whether it's valid code, what *are* you asking? – Jon Skeet May 21 '15 at 06:18
  • Perhaps you've gotten this wrong - what java doesn't allow is multiple inheritance. Multiple inheritance is one class inheriting (extending) multiple other classes at the same time. This is why java uses interfaces, to simulate multiple inheritance – Mackiavelli May 21 '15 at 06:19
  • 1
    @Mackiavelli Errrm ... "This is why java uses interfaces, to simulate multiple inheritance". The main intention for having interfaces is not to simulate multiple inheritance. There are several purposes of having interfaces, one of them is to have classes _fulfill a role_ or _bind to contract_. Maybe you wanted to express that interfaces are a way to simulate multiple inheritance, but this is not the reason for their existence. – Seelenvirtuose May 21 '15 at 06:24
  • Thanks all. I apologise that the question seems lazy, but I am currently posting from a machine that does not have the JDK (so I can't test) installed and am not in a position where I can download/install it. I'm working on an assignment for my object oriented programming class. – anditpainsme May 21 '15 at 06:36
  • @anditpainsme there are many multiple online compilers and ide's, for such simple tasks you can check it there – Mackiavelli May 22 '15 at 05:38

2 Answers2

1

Yes, that behavior is exactly what is expected when using inheritance in Java. Here's some quick reading that you may find usefull: http://www.homeandlearn.co.uk/java/java_inheritance.html

Your JackRussel object will inherit all fields and methods from it's Animal and Dog super-classes that are:

  • not declared private;
  • are not overridden (in which case will get only have access to the overridden one);
  • are not shadowed (in which case will get only have access to the shadowed one);

Here's another quick link on shadowing and overriding in Java: http://docstore.mik.ua/orelly/java-ent/jnut/ch03_04.htm

Having those point in mind, you can easily design inheritance tree that can propagate parent’s behavior and state to all of its children.

Thanks.

0

Of course this is possible. But saying that the Siamese would require methods and fields from both superclasses is a bit wrong. When cat extends animal it gets all of the fields and methods of animal (if you do not override them). Then, when Siamese extends that cat class, it will automatically get the whole Cat class, including the things that are from the Animal class, regardless of whether they are overriden or not.

In short terms, this is possible.

Mackiavelli
  • 432
  • 2
  • 12