SCJP Certification

Sun Certified Java Programmer Certification exam essentials

September 14th, 2009

What will happen if we inherit an abstract class?

Java Facts, by Daisy Williams.

abstract class Ques0347A{
int x;
int init(){
x = 15;
return x;
}
abstract void disp();
}

public class Ques0347 extends Ques0347A{
void disp(){
int y = init();
System.out.println(y);
}
public static void main(String[] argv) {
Ques0347 ob = new Ques0347();
ob.disp();
}
}

An abstract class can be inherited as same as a simple class by using the extends clause. In the given code, there are two classes named Ques0347A and Ques0347. The Ques0347A class is declared as abstract because it contains a declaration of an abstract method named disp. It also contains an instance variable x and an instance method init().

The extends clause in the declaration of the Ques0347 class suggests that it is a subclass of the abstract class Ques0347A. As the Ques0347 class provides an implementation of the disp() method, it is not declared as abstract. Ques0347 being a subclass of the Ques0347A class inherits the init() method and the variable x.

Hence, it is necessary of we inherit an abstract class then the sublcass will implement all the methods that are left unimplemented in the superclass.

Share

Back Top

Responses to “What will happen if we inherit an abstract class?”

Comments (0) Trackbacks (0) Leave a comment Trackback url
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Your email address will not be published. Required fields are marked *

*