Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Core Java Interview Questions and Answers

Ques 81. What does it mean that a method or class is abstract?

An abstract class cannot be instantiated. Only its subclasses can be instantiated. You indicate that a class is abstract with the abstract keyword like this:

public abstract class Container extends Component {
Abstract classes may contain abstract methods. A method declared abstract is not actually implemented in the current class. It exists only to be overridden in subclasses. It has no body. For example,
public abstract float price();
Abstract methods may only be included in abstract classes. However, an abstract class is not required to have any abstract methods, though most of them do. Each subclass of an abstract class must override the abstract methods of its superclasses or itself be declared abstract.

Is it helpful? Add Comment View Comments
 

Ques 82. What is an abstract method?

An abstract method is a method whose implementation is deferred to a subclass.

Is it helpful? Add Comment View Comments
 

Ques 83. You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces?

Sometimes. But your class may be a descendent of another class and in this case the interface is your only option.

Is it helpful? Add Comment View Comments
 

Ques 84. What are interfaces?

Interfaces provide more sophisticated ways to organize and control the objects in your system.
The interface keyword takes the abstract concept one step further. You could think of it as a 'pure' abstract class. It allows the creator to establish the form for a class: method names, argument lists, and return types, but no method bodies. An interface can also contain fields, but The interface keyword takes the abstract concept one step further. You could think of it as a 'pure'?? abstract class. It allows the creator to establish the form for a class: method names, argument lists, and return types, but no method bodies. An interface can also contain fields, but an interface says: 'This is what all classes that implement this particular interface will look like.'?? Thus, any code that uses a particular interface knows what methods might be called for that interface, and that'??s all. So the interface is used to establish a 'protocol'?? between classes. (Some object-oriented programming languages have a keyword called protocol to do the same thing.)  Typical example from "Thinking in Java":

import java.util.*; 
interface Instrument { 
	int i = 5; // static & final 
	// Cannot have method definitions: 
	void play(); // Automatically public 
	String what(); 
	void adjust(); 
}
 
class Wind implements Instrument { 
	public void play() { 
		System.out.println("Wind.play()"); 
	}
	public String what() { return "Wind"; } 
	public void adjust() {} 
}

Is it helpful? Add Comment View Comments
 

Ques 85. What is similarities/difference between an Abstract class and Interface?

Differences are as follows:
Interfaces provide a form of multiple inheritance. A class can extend only one other class. Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class. Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast.
Similarities:
Neither Abstract classes or Interface can be instantiated.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2021 WithoutBook