Prev Next

Java / Abstract Class

1. Concrete class.

Refers to the class with complete implementation i.e. no abstract methods and only concrete methods.

Read full answer

2. Can I create an abstract variable?

No, abstract keyword is applicable only for classes and methods.

Read full answer

3. What is an abstract class?

A class that has only partial implementation and has abstract methods.

Read full answer

4. Can an abstract class be instantiated?

No. Abstract classes cannot be instantiated since it is abstract and not concrete.

Read full answer

5. Can a abstract class be defined without any abstract methods?

Yes. It cannot be instantiated since declaring a class as abstract restrict it to be not instantiated on its own.

Read full answer

6. What is the difference between an Interface and an Abstract class?

All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain both abstract and non-abstract methods. A class may implement a number of Interfaces, but can extend only one abstract class. In order for a class to implement an interface, it must implement all...

Read full answer

7. Can an abstract method be declared static in Java?

No. An abstract method cannot be static. You cannot override a static method, so making it abstract would not make any sense.

Read full answer

8. Java 8, difference between abstract class and Interface.

Both abstract class and interface are used for abstraction by hiding the internal implementation of the feature and only showing the functionality to the users. method types : Interface can have only abstract methods. From Java 8, interface can have default and static methods also. Abstract class...

Read full answer

9. Can abstract class have Constructor?

Yes, but its object cannot be created by calling the constructor instead it is invoked during constructor chaining.

Read full answer

10. Can abstract class implement an interface in Java?

Yes, an abstract class can implement the interface by using implements keyword. Since they are abstract, they donΒ’t need to implement all methods .

Read full answer

11. Can an abstract class be final in Java?

12. Can abstract class have a static method in Java?

Yes, the abstract class can declare and define static methods. It is not recommended though as per design principles.

Read full answer

13. What is an abstract method in Java?

An abstract method is a method without a body. You just declare method, without defining it and use abstract keyword in method declaration. All method declared inside Java Interface are by default abstract. Here is an example of an abstract method in Java. public void abstract anExampleAbstractMe...

Read full answer

14. Can an abstract class have the main method?

Yes, it is also a static method and it can execute the abstract class. public abstract class AbstractClassMain { public static void main (String [] args) { System.out.println( "Hello from abstract class" ); } }

Read full answer

15. Can we call an abstract method from a non-abstract method?

16. Can we mark a local inner class as abstract?

Yes. public class OuterClass { public void instanceMethod (){ abstract class InnerClass { } } } Below is an example that illustrates the usage of local inner abstract class. public class SendNotification { public enum NotifyBy { MESSAGE, EMAIL, PHONECALL } public Runnable createNotification (Noti...

Read full answer

17. Can abstract method include throws clause?

Yes. Abstract methods can throw exceptions.

Read full answer

18. Can an abstract class have a final concrete method?

Yes. An abstract class can have the final concrete method. See the below example. public class AbstractClassFinalConcreteMethod { public static void main (String [] args) { Parent p = new Child(); p.parentMethod(); } } abstract class Parent { final void parentMethod () { System.out.println( "Fina...

Read full answer

19. What is Double Brace initialization in Java?

Double brace initialization creates an anonymous class derived from the specified class and provides an initializer block. new ArrayList() {{ add("ONE"); add("TWO"); }}; The outer brace creates the anonymous inner class while the inner represents the initializer block.

Read full answer

«
»

Comments & Discussions