thinkinterview » interview questions » Java
« Previous

Java Interview Questions

next »
question What is an abstract class? Can Abstract Class be instantiated directly?
Answer Description: Abstract classes contain abstract and concrete methods. Abstract classes cannot be instantiated directly it means that neither we can call the constructor of an abstract class directly nor we can create an instance of an abstract class. However, if we create an instance of a class that extends an Abstract class, compiler will initialize both the classes. Here compiler will implicitly call the constructor of the Abstract class. Any class that contains an abstract method must be declared “abstract” and abstract methods can have definitions only in child classes. By overriding and customizing the abstract methods in more than one subclass makes “Polymorphism” and through Inheritance we define body to the abstract methods. Basically an abstract class serves as a template. Abstract class must be extended/subclasses for it to be implemented. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated. Abstract class is a class that provides some general functionality but leaves specific implementation to its inheriting classes. Example of Abstract class:abstract class ExampleAbstractClass{ protected String name;
public String getname() {
return name;
}
public abstract void function();
}

Next Queston » Can Abstract Class have constructors?...
Can Abstract Class have constructors?View full queston »

Posted in: Java(30) |