-2

Can somebody explain (like for dummies) the following examples of inheritance in Java:

1) public class Dog <T extends Animal> {....
2) public class Buldog extends Dog<DogFood, DogCommands> {....
3) public class Buldog<T extends DogFood, K extends DogCommands> extends Animal implements LivingBeign, LivingThing<T,K> { ....
Adrian Ivasku
  • 1,118
  • 3
  • 16
  • 31
  • 1
    without more context you could aswell just ask what this does: `class Blob extends Stuff implements Thingy {...}` – Lino Feb 14 '19 at 07:34
  • @Lino Be free to ask whatever new info you need. I though my examples are written in a clear way. Simply ask if you need any more information. Or if it is to much of a hustle to answer, please point me to a direction where I can find any relevant information about my examples. – Adrian Ivasku Feb 14 '19 at 07:36
  • Currently there is no clear concise answer, as we don't know what all the other classes and/or interfaces are. And even if you'd show these it wouldn't really change the fact that it still isn't clear what you're asking. What don't you understand from the given examples? It's just 3 different class declarations, nothing more. – Lino Feb 14 '19 at 07:51
  • @ Lino Eg 1 question, It is obvious that class Dog extends Animal, but I do not understand what does the diamond brackets mean in this context ? What is T in this context ? In java, everything has a type, what type is T ? – Adrian Ivasku Feb 14 '19 at 07:54
  • Possible duplicate [What are generics in Java](https://stackoverflow.com/questions/7815528/what-are-generics-in-java) – Lino Feb 14 '19 at 08:05

1 Answers1

3
1) public class Dog <T extends Animal> {...

There will be an generic type In your Dog class which is inherit variables and methods from class (probably an abstract class Animal) This T must have and animal property.. For example assume you have a class Mammalian. We know that all mammalian are animal so they have what all animals have, and they can what all animals can.

So yo can call this as

public Dog<Mammalian> myDog = ...

There is a different situation.

2) public class Buldog extends Dog<DogFood, DogCommands> {....

So your dog class should be written like

/** T refers the food, and K refers commands*/
public class Dog<T,K> {....

So when you want to extend your class with Bulldog, you can leave generic or specify those generic types..

3) public class Buldog<T extends DogFood, K extends DogCommands> extends Animal implements LivingBeign, LivingThing<T,K> { ....

This is also as easy as above codes. The difference that you are desiring subclass of DogFood, which can be anyting, it can be Pap or Milk or Meat, and some subclass of DogCommands for example SitCommand, PlayCommand.. And as you are creating Buldog you know that It is Animal, so you don't want to write animal's property and methods again and since in Java you can't multiple inherit, you want also the other interfaces methods in your class..

I hope it is more understandable now.

Said Alır
  • 170
  • 2
  • 15
  • Thank you. This is exactly what I was looking for. I am just not clear at the 3rd example, about the interfaces, they also have generic property ? ? Why are they passed ? How are they used in those 2 interfaces ? Are those T and K the same property as of DogFood and DogCommands ? – Adrian Ivasku Feb 14 '19 at 08:00
  • Let think about more concrete example.. For example Map Key refers your key and V refers your value. However Java does not want to limit your key types for String and Value types as Integer or sth like that. So whatever your T and K refers in LivingThing, for example T refers it's name and K refers it's size.. You can call LivingThing .. Size is also your custom class and keeps primative types that you want to hold – Said Alır Feb 14 '19 at 08:06
  • or you can create an instance public class Human implements LivingThing and in your Name class you can keep firstName, secondName, middleName, title, lastname etc. and in your BodySize class you can keep whatever you want also.. @AdrianIvasku I hope now the puzzle is completed – Said Alır Feb 14 '19 at 08:16