Menu
Topics Index
...
`

Abstract Methods


If you need explanation Read this topic

If you need Answer Take test on this topic

Abstract Methods
What will be the output of the following program?
public class CalculateAreas {
    public static void main(String arg[]) {
        Circle circle = new Circle();
        circle.radius = 7.5;
        System.out.println("Area of circle is " + circle.getArea());
    }
}
abstract class Shape {
    abstract double getArea();
}
class Circle extends Shape {
    double radius;
    @Override
    double getArea() {
        return 3.14 * radius * radius;
    }
}

A. Area of circle is
B. Area of circle is 176.625
C. Compilation Error
D. 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.

B

Posted by Abhishek Kumar    2014-10-13 09:17:03


b

Posted by Srujan Barai    2014-10-13 09:17:10


B; from main circle object is created.the radius is assigned to 7.5 then in print it calls getArea method . It returns 3.14*7.5*7.5 so value will be 176.625

Posted by Bahunadam Narendra    2014-10-13 09:50:31


b.

Posted by Shubham Bansal    2014-10-13 13:47:41


Ans is B

here we have 3 classes in..
Shape is abstract class means a class with abstract methods is called abstract classes..abstract method means incomplete..means method withot defination..
example: void fun();
we cant create object for the abstract class bt we can create reference for these classes..
class Circle extending shape
@Override is an inbult annoation which is used to give info to the compiler that we are overiding the getArae() method from it's parent class.if these doesnt present in parent class than compiler wil give error

in main class we create Object for the Circle than memroy will allocate for radius..next we are assigning raius as 7.5
circle--->radius=7.5

we are calling getArea() method it wil return the correspondin value..

Posted by Uday Kumar    2014-10-13 15:20:08


ans b.
abstract class define a superclass that declares the structure of a givn abstraction without provide a complete implementation of every metho.

this generalizd form is shard with all subclasses.

it allows overridden which is called as run time polymorphism.

here circle class inherits properties of shape class.

instance var for circle class creatd and assigned with 7.5

circle. getarea cald.
it ovrride shape class.

and return 3.14*7.5*7.5.
thn main sop get printd.

Posted by Maheshwari Natarajan    2014-10-13 17:26:06


This dose is now closed and the winners are Bahunadam Narendra,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Shubham Bansal. 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    2014-10-14 04:16:25


© meritcampus 2019

All Rights Reserved.

Open In App