Trong một abstract class, khai báo nào sau đây hợp lệ?doneprivate static long note = 100;final abstract void doSomething(int things);public abstract int getTotal(int a, int b) {return 0;}static void writeIt(String a) {System.out.println(a);}
Question
Trong một abstract class, khai báo nào sau đây hợp lệ?doneprivate static long note = 100;final abstract void doSomething(int things);public abstract int getTotal(int a, int b) {return 0;}static void writeIt(String a) {System.out.println(a);}
Solution
Trong một abstract class, khai báo sau đây là hợp lệ:
-
private static long note = 100;- Đây là khai báo hợp lệ vì biếnnoteđược khai báo là private, static và có giá trị khởi tạo là 100. -
static void writeIt(String a) {System.out.println(a);}- Đây cũng là khai báo hợp lệ vì phương thứcwriteItđược khai báo là static và không trả về giá trị nào.
Tuy nhiên, hai khai báo sau đây không hợp lệ:
-
final abstract void doSomething(int things);- Phương thức không thể được khai báo cùng lúc là final và abstract. Final có nghĩa là phương thức không thể bị ghi đè, trong khi abstract có nghĩa là phương thức phải được ghi đè trong lớp con. -
public abstract int getTotal(int a, int b) {return 0;}- Phương thức abstract không thể có phần thân. Nó chỉ có thể khai báo mà không có phần thực thi.
Similar Questions
What happens when the following program is compiled and run?public abstract class MyAbstract { abstract int getSum();abstract int getSum(int x);abstract int getSum(int x, int y);} public abstract class Sub extends MyAbstract { int x = 4; int getSum(){return x + x;}int getSum(int x){return x + x;}int getSum(int x, int y){int z = x + y;System.out.print(z);return z;}public static void main(String[] args){Sub sub = new Sub();sub.getSum(4,2);}}
Select the correct answerWhat will be the output of the following Java program? class access { static int x; void increment() { x++; } } class static_use { public static void main(String args[]) { access obj1 = new access(); access obj2 = new access(); obj1.x = 0; obj1.increment(); obj2.increment(); System.out.println(obj1.x + " " + obj2.x); } }Options 2 21 11 2Compilation Error
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
Given the following methods: static String saySomething(String name) { return "Hi, " + name; } static String saySomething(String name, int count) { String greeting = ""; for(int i = 0; i < count; i++) { greeting += "Welcome, " + name + "\n"; } return greeting; }Which of the following would need to be called to result in the output:Hi, Sophiaa.)System.out.println(saySomething(1,"Sophia"));b.)System.out.println(saySomething("Sophia",1));c.)System.out.println(saySomething("Hi Sophia"));d.)System.out.println(saySomething("Sophia"));
class Calculator { public static int add(int a, int b) { return a + b; } public static int add(int a, int b, int c) { return a + b + c; }}public class Main { public static void main(String[] args) { int result1 = Calculator.add(10, 20); int result2 = Calculator.add(10, 20, 30); System.out.println("Result1: " + result1 + ", Result2: " + result2); }}a.Result1: 30, Result2: 60b.Result1: 20, Result2: 30c.None of the aboved.Compilation 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.