Skip to main content

Posts

Showing posts with the label Abstract Class

Is It Possible To Define All The Methods In An Abstract Class?

It is possible create an abstract class with all concrete methods. But this class can’t be instantiated. Since it is an abstract class.    We may have situations where we don’t want to instantiate a class which is the base class for all the classes in the system or application. This base class may have lot of methods used for different modules in the application and we do not want to instantiate the object of the base class and want to use only the reference of the class.   An abstract class without any abstract methods should be a rare thing and you should always question your application design if this case arises. Normally you should refactor to use a concrete super class in this scenario.   One specific case where abstract class may justifiably have no abstract methods is where it partially implements an interface, with the intention that its subclasses must complete the interface.

When To Use Abstract Class and Interface?

What is the difference between interface and an abstract class?  This is the first question most of the java interviewers start in interviews. Ofcourse it seems a silly question and can answer. But if interview goes depth of the concept of abstract class and interface, most of the candidates not able to answer. To overcome that ignorance just read the points that I have highlighted below. 1. Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. An abstract class can contain no abstract methods also i.e. abstract class may contain concrete methods. A Java Interface can contain only method declarations and public static final constants and doesn't contain their implementation. The classes which implement the Interface must provide the method definition for all the methods present. 2. Abstract class definition begins with the keyword "abstract" keyword followed by Class definition. An Interface definition be...

What is the difference between an abstract class and an interface and when should you use them?

In design, you want the base class to present only an interface for its derived classes. This means, you don't want anyone to actually instantiate an object of the base class. You only want to upcast to it (implicit upcasting, which gives you polymorphic behavior), so that its interface can be used. This is accomplished by making that class abstract using the abstract keyword. If anyone tries to make an object of an abstract class, the compiler prevents it. The interface keyword takes this concept of an abstract class a step further by preventing any method or function implementation at all. You can only declare a method or function but not provide the implementation. The class, which is implementing the interface, should provide the actual implementation. The interface is a very useful and commonly used aspect in OO design, as it provides the separation of interface and implementation and enables you to: