Will the below code will execute successfully ?abstract class Shape{ private abstract int calcArea();}Select one:TrueFalse
Question
Will the below code will execute successfully ?abstract class Shape{ private abstract int calcArea();}Select one:TrueFalse
Solution 1
False. The code will not execute successfully. In Java, you cannot declare an abstract method as private. Abstract methods should be implemented by any class which extends the abstract class, and declaring it as private will not allow its visibility in subclasses.
Solution 2
The code will not execute successfully.
In Java, you cannot declare an abstract method as private. An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
If a class includes abstract methods, then the class itself must be declared abstract, as in:
public abstract class GraphicObject { // declare fields // declare non-abstract methods abstract void draw(); }
Abstract methods don't have a body, they're just declared with a semicolon at the end. Abstract methods are declared abstract as they must be implemented by the class that extends the abstract class.
Private methods are not visible in subclasses and hence, if an abstract method is declared private, it cannot be accessed from its subclass to provide the implementation, which defies the concept of the abstract methods.
So, the answer is False.
Similar Questions
Will the following code get executed successfully ?abstract class Shape{ int i = 111, j = 222; abstract void calcArea(); abstract void calcVolume();}class Square extends Shape{ void calcVolume() { System.out.println(j); } void calcArea(){ System.out.println(j); }}public class Test{ public static void main(String[] args) { Square c = new Square(); c.calcArea(); c.calcVolume(); }}Select one:a.Yes, the code will get executed successfully.b.No – Compilation error.
public abstract class Shape { private int x; private int y; public abstract void draw(); public void setAnchor(int x, int y) { this.x = x; this.y = y; }} Which two classes use the Shape class correctly? (Choose two.)Select one or more:a.public abstract class Circle extends Shape { private int radius;}b.public class Circle extends Shape { private int radius; public void draw();}c.public class Circle extends Shape { private int radius; public void draw() {/* code here */}}d.public class Circle implements Shape { private int radius;}
Which would declare a compilable abstract class?Select one:a.public abstract class Shape { public Square draw(); }b.public abstract class Shape { public Square draw() { } }c.public class Shape abstract { public abstract Square draw(); }d.public class Shape { public abstract Square draw(); }
Consider the python code below. Assume that necessary imports have been done. class Shape (metaclass=ABCMeta):def __init__(self): print("I am in init") @abstractmethod def draw_shape(self):pass @abstractmethod def set_color(self):passclass Circle(Shape):def draw_shape(self): print("Draw Circle") Which of the following statement(s) is/are TRUE?i) Class Circle cannot be instantiated as it does not implement set_color() method.ii) The above code will not execute because class Shape has two abstract methodsOptionsOnly i) is TrueOnly ii) is TrueBoth i) and ii) are TrueNeither of i) or ii) is True
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
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.