Can an abstract method be defined in a non-abstract class? code example
Example 1: is it necessary for abstract class to have abstract method
No, abstract class can have zero abstract methods.
Example 2: write a program in which an abstract class is being defined containg an abstract method omputer(int a, int b) and a non abstract method as well
abstract class Sum{
public abstract int compute(int a, int b);
public void disp(){
System.out.println("Method of class Sum");
}
}
class Demo extends Sum{
public int compute(int a, int b){
return a+b;
}
public static void main(String args[]){
Sum obj = new Demo();
System.out.println(obj.compute(3, 7));
obj.disp();
}
}
Example 3: can we have abstract class having no abstract method in java?
abstract class AbstractDemo {
private int i = 0;
public void display() {
System.out.print("Welcome to Tutorials Point");
}
}
public class InheritedClassDemo extends AbstractDemo {
public static void main(String args[]) {
AbstractDemo demo = new InheritedClassDemo();
demo.display();
}
}
Example 4: Can we add a non-abstract method into abstract class?
Yes, we can. An abstract class can have both abstract and non-abstract methods
Example 5: can you declare an abstract method in a non abstract class
No. A normal class(non-abstract class) cannot have abstract methods.