Topics Index

Abstract class and method creation

If you need explanation Read this topic

If you need Answer Take test on this topic

Abstract class and method creation
What will be the output of the following program?
abstract class Shape
    int b = 20;
    public void display()
        System.out.println("This is display method");
    abstract public void calculateArea();
class Rectangle extends Shape
    public static void main(String args[])
        Rectangle obj = new Rectangle();
        obj.b = 200;
       public void calculateArea() {    }

A. This is display method
B. This is display method
C. 20
D. Compilation Error or Runtime Error
Topic: Abstract Method In Java

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 B

This is display method

here we using an abstract class and as well as a concrete class .
here the concrete class is extending the abstract class and defining the abstract methods..

after creating the object of rectangle class with reference obj the memory allocation is done for the child class and as well as the parent class

Rectangle obj=new Rectangle();

here we are re-assinging the variable b with value 200 which is intially was to 20..


here the method is called in this two printing statement are printed and

1.This is display method

and since we are giving the defination of the calculateArea () method it does not raise any error if not it raises an error as

** Rectangle is not abstarct and does not override abstarct method calculateArea() in shape

** class Rectangle extends shape

Posted by Vicky Maduri    2015-02-27 15:52:41

ANS IS B (This is display method 200)

--Here the concept of Abstract class is used.

--Abstract class is a class which can contain one abstract method and one or more other methods.we can not create objects of abstract class i.e. abstract classes are not instantiable.

--Here,Abstract class shape is defined in which one abstrcat method that is calculateArea() and onother method is display in which "This is display method" will get printed first then value of b is displayed.

--b is assigned 20 in shape class.

--Now,Rectangle class is defined which extends abstract class shape and override the abstract method of shape class i.e. calculateArea() with empty body.
In Rectangle class first of all main() method is defined in which object of rectangle is created and b is assigned 200 using that object name and display method is called.

This is display method

Posted by Mânïshå Mùlchåndânï    2015-02-27 16:33:14

This dose is now closed and the winners are Vicky Maduri,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Cool Pawan. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-03-02 07:09:18

© meritcampus 2019

All Rights Reserved.

Open In App