Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Core Java Interview Questions and Answers

Test your skills through the online practice test: Core Java Quiz Online Practice Test

Ques 76. What are abstract classes, abstract methods?

Simply speaking a class or a method qualified with "abstract" keyword is an abstract class or abstract method. You create an abstract class when you want to manipulate a set of classes through a common interface. All derived-class methods that match the signature of the base-class declaration will be called using the dynamic binding mechanism. If you have an abstract class, objects of that class almost always have no meaning. That is, abstract class is meant to express only the interface and sometimes some default method implementations, and not a particular implementation, so creating an abstract class object makes no sense and are not allowed ( compile will give you an error message if you try to create one). An abstract method is an incomplete method. It has only a declaration and no method body. Here is the syntax for an abstract method declaration: abstract void f(); If a class contains one or more abstract methods, the class must be qualified an abstract. (Otherwise, the compiler gives you an error message.). It's possible to create a class as abstract without including any abstract methods. This is useful when you've got a class in which it doesn't make sense to have any abstract methods, and yet you want to prevent any instances of that class. Abstract classes and methods are created because they make the abstractness of a class explicit, and tell both the user and the compiler how it was intended to be used.
For example:

abstract class Instrument { 
	int i; // storage allocated for each 
	public abstract void play(); 
	public String what() { 
		return "Instrument";
	}	
	public abstract void adjust(); 
} 

class Wind extends Instrument { 
	public void play() { 
		System.out.println("Wind.play()"); 
	} 
	public String what() { 
		return "Wind"; 
	} 
	public void adjust() {}
}
Abstract classes are classes for which there can be no instances at run time. i.e. the implementation of the abstract classes are not complete. Abstract methods are methods which have no defintion. i.e. abstract methods have to be implemented in one of the sub classes or else that class will also become Abstract.

Is it helpful? Add Comment View Comments
 

Ques 77. What does the "abstract" keyword mean in front of a method? A class?

Abstract keyword declares either a method or a class.
If a method has a abstract keyword in front of it,it is called abstract method.Abstract method hs no body.It has only arguments and return type.Abstract methods act as placeholder methods that are implemented in the subclasses.
Abstract classes can't be instantiated.If a class is declared as abstract,no objects of that class can be created.If a class contains any abstract method it must be declared as abstract

Is it helpful? Add Comment View Comments
 

Ques 78. What is the purpose of abstract class?

It is not an instantiable class. It provides the concrete implementation for some/all the methods. So that they can reuse the concrete functionality by inheriting the abstract class.

Is it helpful? Add Comment View Comments
 

Ques 79. How to define an Abstract class?

A class containing abstract method is called Abstract class. An Abstract class can't be instantiated.
Example of Abstract class:

abstract class testAbstractClass {
	protected String myString; 
	public String getMyString() { 
		return myString; 
	} 
	public abstract string anyAbstractFunction();
}

Is it helpful? Add Comment View Comments
 

Ques 80. Can there be an abstract class with no abstract methods in it?

Yes

Is it helpful? Add Comment View Comments
 
5) Can there be an abstract class with no abstract methods in it? " />

Most helpful rated by users:

©2024 WithoutBook