I have a class Animal and subclass Cat and Dog that extends Animal.
I have a class called Zoo having a variable as List;
i.e.
Class Animal {
String name;
}
Class Cat Extends Animal {
String color;
}
Class Zoo {
List<Animal> animalsInZoo;
public void printAnimalClass()
{
for(Animal a :animalsInZoo)
{
System.out.println(a.getClass.getName());
}
}
}
The object of zoo will have animals that can be objects of Animal or subclass of Animal
Following is sample JSON representation of Object of Zoo class.
{ "animalsInZoo" :
[
{"name":"A"},
{"name": "B","color":"white"}
]
}
I have to convert this into java object in such a way that first animal in list get converted into Object of class Animal and second gets converted into object of Class Cat
You have to do the conversion yourself, supposing you have another animal with same property:
Class Horse Extends Animal {
String color;
}
jackson doesn't know convert {"name": "B","color":"white"} into Cat or Horse.
you may add a property to mark which animal you need to convert to.
Related
https://dart.dev/guides/language/language-tour#extending-a-class
Argument types must be the same type as (or a supertype of) the
overridden method’s argument types. In the preceding example, the
contrast setter of SmartTelevision changes the argument type from int
to a supertype, num.
I was looking at the above explanation and wondering why the arguments of subtype member methods need to be defined more "widely"(generally) than the original class's one.
https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#Function_types
class AnimalShelter {
Animal getAnimalForAdoption() {
// ...
}
void putAnimal(Animal animal) {
//...
}
}
class CatShelter extends AnimalShelter {
//↓ Definitions that are desirable in the commentary
void putAnimal(Object animal) {
// ...
}
//↓Definitions that are not desirable in the commentary
void putAnimal(Cat animal) {
// ...
}
//I can't understand why this definition is risky.
//What specific problems can occur?
}
I think this wikipedia sample code is very easy to understand, so what kind of specific problem (fault) can occur if the argument of the member method of the subtype is defined as a more "narrower"(specific) type?
Even if it is explained in natural language, it will be abstract after all, so it would be very helpful if you could give me a complete working code and an explanation using it.
Let's consider an example where you have a class hierarchy:
Animal
/ \
Mammal Reptile
/ \
Dog Cat
with superclasses (wider types) above subclasses (narrower types).
Now suppose you have classes:
class Base {
void takeObject(Mammal mammal) {
// ...
}
Mammal returnObject() {
// ...
}
}
class Derived extends Base {
// ...
}
The public members of a class specify an interface: a contract to the callers. In this case, the Base class advertises a takeObject method that accepts any Mammal argument. Every instance of a Base class thus is expected to conform to this interface.
Following the Liskov substitution principle, because Derived extends Base, a Derived instance is a Base, and therefore it too must conform to that same Base class interface: its takeObject method also must accept any Mammal argument.
If Derived overrode takeObject to accept only Dog arguments:
class Derived extends Base {
#override
void takeObject(Dog mammal) { // ERROR
// ...
}
}
that would violate the contract from the Base class's interface. Derived's override of takeObject could be invoked with a Cat argument, which should be allowed according to the interface declared by Base. Since this is unsafe, Dart's static type system normally prevents you from doing that. (An exception is if you add the covariant keyword to disable type-safety and indicate that you personally guarantee that Derived.takeObject will never be called with any Mammals that aren't Dogs. If that claim is wrong, you will end up with a runtime error.)
Note that it'd be okay if Derived overrode takeObject to accept an Animal argument instead:
class Derived extends Base {
#override
void takeObject(Animal mammal) { // OK
// ...
}
}
because that would still conform to the contract of Base.takeObject: it's safe to call Derived.takeObject with any Mammal since all Mammals are also Animals.
Note that the behavior for return values is the opposite: it's okay for an overridden method to return a narrower type, but returning a wider type would violate the contract of the Base interface. For example:
class Derived extends Base {
#override
Dog returnObject() { // OK, a `Dog` is a `Mammal`, as required by `Base`
// ...
}
}
but:
class Derived extends Base {
#override
Animal returnObject() { // ERROR: Could return a `Reptile`, which is not a `Mammal`
// ...
}
}
void main(){
Animal a1 = Animal();
Cat c1 = Cat();
Dog d1 = Dog();
AnimalCage ac1 = AnimalCage();
CatCage cc1 = CatCage();
AnimalCage ac2 = CatCage();
ac2.setAnimal(d1);
//cc1.setAnimal(d1);
}
class AnimalCage{
Animal? _animal;
void setAnimal(Animal animal){
print('animals setter');
_animal = animal;
}
}
class CatCage extends AnimalCage{
Cat? _cat;
#override
void setAnimal(covariant Cat animal){
print('cats setter');
_cat = animal;
/*
if(animal is Cat){
_cat = animal;
}else{
print('$animal is not Cat!');
}
*/
}
}
class Animal {}
class Cat extends Animal{}
class Dog extends Animal{}
Unhandled Exception: type 'Dog' is not a subtype of type 'Cat' of 'animal'
In the above code, even if the setAnimal method receives a Dog instance, a compile error does not occur and a runtime error occurs, so making the parameter the same type as the superclass's one and checking the type inside the method is necessary.
I try to POST the object
{
"id": "SOME_ID",
"foo": "http://localhost:8080/api/subclass-of-foo/OBJECT_ID"
}
The Foo Class is abstract. Information about concrete class is in the link subclass-of-foo. But I get always
"Cannot construct instance of Foo (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information".
I can't use #JsonDeserialize(as = Subclass.class) on Foo, because there are multiple sub-classes of Foo.
Where and how can I provide this information to fix the problem?
#Entity
#Inheritance(strategy = SINGLE_TABLE)
#DiscriminatorColumn(name = "type", discriminatorType = STRING)
class abstract Foo {
private id;
}
class Bar1 extends Foo {
}
class Bar2 extends Foo {
}
class Bar3 extends Foo {
}
#Entity
class X {
private String id;
#OneToOne
private Foo foo;
}
And json payload to POST an X object:
{
"id": "X_ID",
"foo": "http://localhost:8080/api/bar1/Bar1_ID"
}
I know most of you already know of the animal-cow-grass-food problem
-which states that you have a code like below, that has a type constraint on Cow (which inherits Animal ) to only eat SuitableFood (which inherits Food )
Below is the SCALA representation of the same
class Food
class Grass extends Food
class Cookies extends Food
class Fish extends Food
abstract class Animal {
type SuitableFood <: Food
def eat(food: SuitableFood)
}
class Cow extends Animal {
type SuitableFood = Grass
override def eat(food: SuitableFood) = {}
}
val bessy: Animal = new Cow
bessy eat new Fish
bessy eat new Cookies
I was wondering if similar is possible in KOTLIN or JAVA ?
Not sure what you want to achieve. Restriction for Cow to eat only Grass ?
I think this can be done through generic types.
abstract class Food
open class Grass : Food()
class GreenGrass : Grass()
class Fish : Food()
abstract class Animal<T : Food> {
fun eat(food: T) { ... }
}
class Cow : Animal<Grass>()
class Bear : Animal<Fish>()
class Test {
fun test() {
val cow = Cow()
cow.eat(Grass()) // ok
cow.eat(GreenGrass()) // ok
cow.eat(Fish()) // not ok
val bear = Bear()
bear.eat(Fish()) // ok
bear.eat(Grass()) // not ok
}
}
I've been learning Scala for web-development for quite some time, and I have stumbled upon a lack of interfaces. Coming from PHP, I used interfaces quite a lot for method-level polymorphism and IoC like this:
interface iAnimal
{
function makeVoice();
}
class Cat implements iAnimal
{
function makeVoice()
{
return "Meow";
}
}
class Dog implements iAnimal
{
function makeVoice()
{
return "Woof!";
}
}
class Box
{
private $_animal;
function __construct(iAnimal $animal)
{
$this->_animal = $animal;
}
function makeSound()
{
echo $this->_animal->makeVoice();
}
}
And so on, and that was an easy way to ensure that anything I passed to the Box object had a makeVoice method that I was calling elsewhere. Now, what I'm curious of, is how do I implement similar functionality with Scala. I tried searching for this, but info is quite scarce. The only answer I found was to use traits, but as far as I know, they are used for concrete implementation, not declaration.
Thanks in advance.
As per other answers, the solution is to use traits :
trait Animal {
def makeVoice(): Unit //no definition, this is abstract!
}
class Cat extends Animal{
def makeVoice(): Unit = "Meow"
}
class Dog extends Animal{
def makeVoice(): Unit = "Woof"
}
class Box(animal:Animal) {
def makeSound() = animal.makeVoice()
}
A trait in Scala will be directly compiled to an interface in Java. If it contains any concrete members then these will be directly copied into any class that inherits from the trait. You can happily use a Scala trait as an interface from Java, but then you don't get the concrete functionality mixed in for you.
However... this is only part of the picture. What we've implemented so far is subtype polymorphism, Scala also allows for ad-hoc polymorphism (a.k.a typeclasses):
// Note: no common supertype needed here
class Cat { ... }
class Dog { ... }
sealed trait MakesVoice[T] {
def makeVoice(): Unit
}
object MakesVoice {
implicit object CatMakesVoice extends MakesVoice[Cat] {
def makeVoice(): Unit = "Meow"
}
implicit object DogMakesVoice extends MakesVoice[Dog] {
def makeVoice(): Unit = "Woof"
}
//helper method, not required, but nice to have
def makesVoice[T](implicit mv: MakesVoice[T]) = mv
}
import MakesVoice._
//context-bound version
class Box[T : MakesVoice] {
//using a helper:
def makeSound() = makesVoice[T].makeVoice()
//direct:
def makeSound() = implicitly(MakesVoice[T]).makeVoice()
}
//using an implicit param
class Box[T](implicit mv : MakesVoice[T]) {
def makeSound() = mv.makeVoice()
}
What's important here is that the MakesVoice type class can be associated with any type regardless of what hierarchies it belongs to. You can even use type classes with primitives or types imported from a 3rd party library that you couldn't possibly retrofit with new interfaces.
Of course, you have parametric polymorphism as well, which you'd probably know better as "generics" :)
Traits are used for both declaration and concrete implementation. Here is a direct translation of your example
trait Animal {
def makeVoice()
}
class Cat extends Animal{
override def makeVoice(): Unit = "Meow"
}
class Dog extends Animal{
override def makeVoice(): Unit = "Woof"
}
class Box(animal:Animal) {
def makeSound()={
animal.makeVoice()
}
}
Additionally, you are able to actually define a concrete implementation directly in the trait which is useful for behaviours shared by members of different class hierarchies:
trait Fish{
def swim()="Swim"
}
class Truit extends Fish
trait Bird{
def fly() = "Fly"
}
class Eagle extends Bird
class Duck extends ???{
def swim=???
def fly=???
}
The duck both swims ans flies, you could define it as such :
trait Swimmer{
def swim
}
trait Fish extends Swimmer{
def swim()="Swim"
}
class Truit extends Fish
trait Bird{
def fly()="Fly"
}
class Eagle extends Bird
class Duck extends Bird with Swimmer
The example
Java:
#JsonTypeInfo(
use = JsonTypeInfo.Id.MINIMAL_CLASS,
include = JsonTypeInfo.As.PROPERTY,
property = "#type")
public class Pet{
String name;
}
public class Dog extends Pet{}
public class Cat extends Pet{}
public class PetHouse {
List<Pet> pets;
}
public class BarkingData {
int decibels;
Dog dog;
}
JSON Serialization
petHouse = {
pets :
[
{'#type': 'Dog', 'name':'Droopy'},
{'#type': 'Cat', 'name':'Scratchy'},
{'#type': 'Dog', 'name':'Snoopy'}
]
}
barkingData = {
decibels:15,
dog:{'#type':'Dog', 'name':'Droopy'}
}
The Question
Class BarkingData has a field of type Dog (cats don't bark do they). Is it possible to tell Jackson not to include typeInfo for instances where that type can be "hinted" from the declaring field ?
So that the output of Barking data looks like :
barkingData = {
decibels:15,
dog:{'name':'Droopy'}
}
Your idea that you know the dynamic type (actual type) of this field because the static type is Dog and not Animal only works if there are no subclasses of Dog. If you make the Dog class final, then Jackson knows it can safely leave out the type info.
Additionally, you can override Jackson's type info settings, in more complex ways, for fields of static type Dog, by adding a #JsonTypeInfo annotation to the definition of the Dog class.