in Education by
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 child classes that extend Product and an Ad class which lists the products. My question is: can I use the Product class as an attribute? so it could be instantiated with the different child classes like this: public class Ad implements Serializable { private Integer id; private Product product; //constructors and methods.. } Ad example = new Ad(1,childClass); JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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.

Related questions

0 votes
    Which of these can be used to fully abstract a class from its implementation? (a) Objects (b) Packages (c) ... & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    What is the attribute of java bean to specify scope of bean to have single instance per Spring IOC? (a) prototype ... & JDBC of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Can an abstract class have a constructor?...
asked Feb 9, 2023 in Technology by JackTerrance
0 votes
    Can abstract keyword be used with constructor, Initialization Block, Instance Initialization and Static Initialization ... Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of these methods is a part of Abstract Window Toolkit (AWT) ? (a) display() (b) paint() (c) drawString( ... I/O & Applets of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Can servlet class declare constructor with ServletConfig object as an argument? (a) True (b) False I have been ... & Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Abstract class cannot have a constructor. (a) True (b) False I have been asked this question in ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    If a class inheriting an abstract class does not define all of its function then it will be known as ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Which of the following code is used to get an attribute in a HTTP Session object in servlets? (a) session. ... & Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    I am considering writing two limited precision alternatives to BigDecimal, namely DecimalInt and DecimalLong. These ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 8, 2022 in Education by JackTerrance
0 votes
    Which of these class is used to read characters and strings in Java from console? (a) BufferedReader (b) ... & Applets of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which of the following is not a class of java.util.regex? (a) Pattern class (b) matcher class (c) ... Regular Expressions of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    What is the significance of Matcher class for regular expression in java? (a) interpretes pattern in the string ... Expressions of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which of the below is true about java class structure? (a) The class name should start with lowercase (b) ... & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of these is a class which uses String as a key to store the value in object? (a) Array (b) ... The Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
...