Home > Enterprise >  Can a class have an abstract class as atribute in java
Can a class have an abstract class as atribute in java

Time:04-01

I have an abstract class "product"

public abstract class Product implements Serializable {
   private Integer id;
   private LocalDateTime creationDate;
   private LocalDateTime updateDate;
   //constructors etc..
}

then I have multiple childs that extends the product class and an ad class witch list the products, my question is: can I use the product class as an atribute so it could be instaciated with the diferent child clasess like this?

public class Ad implements Serializable {
   private Integer id;
   private Product product;
   //constructors and methods..
}


Ad example = new Ad(1,childClass);

CodePudding user response:

Your question is whether you can use the Product class which is the abstract class as an attribute so it could be initiated with the different child class implementation of it ? Answer is yes!

I will add a simple example below:

Abstract class

public abstract class AbstractClass {

  abstract void test();

}

Child class

public class ChildClass extends AbstractClass {

  @Override
  void test() {

  }

}

Example usage

public class Example {

  private static AbstractClass abstractClass;

  public static void main(String[] args) {
    abstractClass = new ChildClass();
  }

}

Hope this answers your question.

CodePudding user response:

Yes, member field can be an abstract type

Yes, a member field may be of a type that is abstract.

Of course you will need to use a concrete implementation of that abstract class to instantiate an object to populate that field.

Alternate example

Here is a different example, using an unrealistic portrayal of a veterinary clinic with animal patient visits.

In this example, we show an abstract class as a member field, with either of two concrete classes used to populate that field.

A Visit consists of a date and a Pet object. The Pet class is abstract. So we have concrete classes Cat & Dog which extend Pet. We declare the Visit member field to be of type Pet while we populate with concrete classes for cats and dogs.

Notice how I used this.getClass().getSimpleName() in the toString override on Pet. This will show the magic of polymorphism, and verifies how we have objects of a concrete class being held in a member field of an abstract type.

package work.basil.example.vet;

import java.time.LocalDate;
import java.time.Month;
import java.util.List;

public class App
{
    public static void main ( String[] args )
    {
        App app = new App();
        app.demo();
    }

    private void demo ( )
    {
        Cat fluffy = new Cat( "Fluffy" );
        Dog rover = new Dog( "Rover" );

        List < Visit > visits =
                List.of(
                        new Visit( LocalDate.of( 2022 , Month.JANUARY , 23 ) , fluffy ) ,
                        new Visit( LocalDate.of( 2022 , Month.FEBRUARY , 28 ) , fluffy ) ,
                        new Visit( LocalDate.of( 2022 , Month.MARCH , 23 ) , rover )
                );

        System.out.println( "visits = "   visits );
    }

    abstract class Pet
    {
        private String name;

        String getName ( ) /* Getter */ { return this.name; }

        public Pet ( String name )  /* Constructor*/ { this.name = name; }

        @Override
        public String toString ( )
        {
            return "Pet{ "  
                    " | "  
                    "name="   name  
                    " }";
        }
    }

    class Cat extends Pet
    {
        public Cat ( String name )  /* Constructor*/ { super( name ); }
    }

    class Dog extends Pet
    {
        public Dog ( String name )  /* Constructor*/ { super( name ); }
    }

    record Visit( LocalDate date , Pet pet ) { }
}

visits = [Visit[date=2022-01-23, pet=Pet{ class=Cat | name=Fluffy }], Visit[date=2022-02-28, pet=Pet{ class=Cat | name=Fluffy }], Visit[date=2022-03-23, pet=Pet{ class=Dog | name=Rover }]]

  • Related