Java / Abstract Class
1. Concrete class.
Refers to the class with complete implementation i.e. no abstract methods and only concrete methods.
2. Can I create an abstract variable?
No, abstract keyword is applicable only for classes and methods.
3. What is an abstract class?
A class that has only partial implementation and has abstract methods.
4. Can an abstract class be instantiated?
No. new AbstractType() does not compile. You instantiate a concrete subclass, or an anonymous subclass that implements the abstract methods.
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.
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....
7. Can an abstract method be declared static in Java?
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....
9. Can abstract class have Constructor?
Yes, but its object cannot be created by calling the constructor instead it is invoked during constructor chaining.
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 dont need to implement all methods .
11. Can an abstract class be final in Java?
No.
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.
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....
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" ); } }
15. Can we call an abstract method from a non-abstract method?
Yes.
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....
17. Can abstract method include throws clause?
Yes. Abstract methods can throw exceptions.
18. Can an abstract class have a final concrete method?
Yes. An abstract class can have the final concrete method....
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<String>() {{ add("ONE"); add("TWO"); }}; The outer brace creates the anonymous inner class while the inner represents the initializer block.