Predict the output:public abstract class Abs { public Abs(){ System.out.println("Constructor from Abstract class"); } }public class Test extends Abs { public static void main(String args[]){ Abs obj=new Test(); }}Select one:a.Constructor from Abstract classb.Compile time error: An abstract class cannot have a constructorc.Program will execute successfully but not display anythingd.Compile time error: An abstract class cannot be instantiated
Question
Predict the output:public abstract class Abs { public Abs(){ System.out.println("Constructor from Abstract class"); } }public class Test extends Abs { public static void main(String args[]){ Abs obj=new Test(); }}Select one:a.Constructor from Abstract classb.Compile time error: An abstract class cannot have a constructorc.Program will execute successfully but not display anythingd.Compile time error: An abstract class cannot be instantiated
Solution
The correct answer is:
a. Constructor from Abstract class
Explanation:
Even though Abs is an abstract class, it can have a constructor. When we create an instance of the Test class (which extends the Abs class), the constructor of the Abs class is called first. Therefore, "Constructor from Abstract class" will be printed.
Similar Questions
What is the output of the below Java program with an abstract class?abstract class MathLibrary{ static final float PI = 3.1415f;}public class AbstractClassTesting3{ public static void main(String[] args) { System.out.println(MathLibrary.PI); }}A) 3.1415B) No OutputC) ErrorD) None of the above
Observe the below code : abstract public class Account{ private int accountNumber; private String holderName; private double balance; public Account(int accountNumber, String holderName, double balance) { this.accountNumber = accountNumber; this.holderName = holderName; this.balance = balance; } }Identify the correct statements.Select one:a.Code compiles successfullyb.code will not compile, because abstract class cannot have a constructorc.code will not compile because the abstract class do not have abstract methods
Find the output: abstract class One { int i; abstract void display(); } class Two extends One { int j; void display() { System.out.println(j); } } class AbstractDriver { public static void main(String args[]) { Two obj = new Two(); obj.j=2; obj.display(); } }
In Java, can an abstract class have a constructor?Question 13Answera.Yes, an abstract class can have a constructor.b.No, an abstract class cannot have a constructor.c.It depends on the access modifiers of the constructor.d.It depends on the version of Java being used.
the correct answerWhat will be the output of the given Java program? abstract class java { int x; abstract void display(); } class method extends java { int y; void display() { System.out.println(y); } } class Abstract_demo { public static void main(String args[]) { method obj = new method(); obj.y = 1; obj.display(); } }Options0Runtime Error1Compilation Error
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.