Menu
Topics Index
...
`

Abstract Classes2828


If you need explanation Read this topic

If you need Answer Take test on this topic

Abstract Classes2828
What will be the output of the following program?
public class AbstractClass {
    public static void main(String[] args) {
        Shape shape1;  // LINE A
        Shape shape2 = new Shape(); // LINE B
        System.out.println("Compiles Successfully");
    }
}
abstract class Shape {}
class Rectangle extends Shape {}

A. Compiles Successfully
B. Compilation Error at LINE A because object is not created.
C. Compilation Error at LINE B because object is creating to the abstract class Shape.
D. Compilation Error or Runtime Error for some other reason
Topic: Java Abstract Class

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is C.
As we know that we cannot create object for abstract class.. Here in this program we are attempting to create an object for abstract class "Shape".. so it will raise the compilation error as "cannot be instantiated". We can rectify the error by changing the statement as Shape shape2 = new Rectangle();

Posted by Gopinath Manchikanti    2015-02-20 13:57:38


ANS IS C (compilation Error at line B)

Here, shape is abstract class and we can not create instances of abstract class so there will be compilation error at line B.

--Though, we can create object without new keyword called instances. Abstract class can not be instantiated.

Posted by Mânïshå Mùlchåndânï    2015-02-20 14:30:10


ans c. Abstract classes are closely related to interfaces. They are classes that cannot be instantiated, and are frequently either partially implemented, or not at all implemented.    Abstraction refers to the ability to make a class abstract in OOP. An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class. If a class is abstract and cannot be instantiated, the class does not have much use unless it is subclass.   here we hav 3 classes.rectangle class exttenda abstract shape class,in main class we created reference for shape,thn we created object for abstract class which is an illegal method. so compiler throws error

Posted by Maheshwari Natarajan    2015-02-22 14:48:03


Ans is C..here we have the concept of the abstract classes which means the class with out complete methods we can say abstract class...abstract classes can have the normal methods as well as abstract methods as well as normal methods..if class has an abstract method than that class must be made as abstract..as class is incomplete we can not create the instance for the abstract class but we can have constructor in it..it is invoked when we create the instance for its parent class
Execution:
here we have the Shape class which is abstract and Rectangle class which is extending the Shape class in the main we create the reference for the abstract class Shape and next we are creating the instance for the abstract class Shape..bcz we can't create the instance for the abstract class

Posted by Uday Kumar    2015-02-24 02:09:28


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is ?????????? ?????. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-24 03:48:00


© meritcampus 2019

All Rights Reserved.

Open In App