Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

What is an Abstract Class? !!!

Wed Jul 04, 2007 12:23 am

What is an Abstract Class? How to use abstract classes in Java?
----------------------------------------------


Java provides you with a special type of class, called an abstract class, which can help you, organize your classes based on common methods. An abstract class lets you put the common method names in one abstract class without having to write the actual implementation code. Abstract keyword is used to define abstract class. Following are the key points of abstract class :

  • Classes, which contain abstract methods, are called abstract classes.
  • A program cannot instantiated an abstract class as an object.
  • Abstract classes may contain a mixture of non-abstract and abstract methods. Non-abstract methods implement the method statements.
  • Any subclass, which extends the class containing abstract methods, must provide the implementation for all the abstract methods; otherwise the subclass itself becomes an abstract class.
Example

java code
public class AbstractExample {

public static void main(String[] args) {

// Book myBook= new Book(); Error Book is abstract
Book myBook = new TechBook();// Ok
myBook.getInfo();
myBook.readBook();
myBook.writeBook(null);
}
}

abstract class Book {

public abstract String readBook();

public abstract void writeBook(String text);

public String getInfo() {
return "This is a abstractBook";
}
}

class TechBook extends Book
{

@Override
public String readBook() {
System.out.println("readBook");
return null;
}

@Override
public void writeBook(String text) {
System.out.println("writeBook");
}

}




Re: What is an Abstract Class? !!!

Fri Feb 20, 2009 6:56 am

can a non-abstract class have abstract methods?

Re: What is an Abstract Class? !!!

Fri Feb 20, 2009 10:23 am

nope it can't , To have a abstract methods you must define your class as abstract . ;)

Re: What is an Abstract Class? !!!

Thu Jan 24, 2013 12:04 am

updated.

Re: What is an Abstract Class? !!!

Tue Mar 26, 2013 7:19 am

Abstract class is a class that can not be instantiated, it exists extensively for inheritance and it must be inherited.

Re: What is an Abstract Class? !!!

Thu Mar 28, 2013 7:55 am

Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead.

Re: What is an Abstract Class? !!!

Tue Apr 30, 2013 11:05 am

An abstract class may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be sub-classed. Syntax :
public abstract class GraphicObject
{
// declare fields
// declare non-abstract methods
abstract void draw();
}

Re: What is an Abstract Class? !!!

Fri May 17, 2013 1:42 pm

No, We can't create abstract class without abstract method. abstract class must have at-least one abstract method.

Post a reply
  Related Posts  to : What is an Abstract Class? !!!
 java abstract class,concrete class and interface     -  
 make abstract class     -  
 how to Define abstract class in php     -  
 why cant instantiate an abstract class     -  
 The difference between an Interface and an Abstract class     -  
 What is an abstract method     -  
 Abstract Classes in jsp     -  
 Define class helper class to check the method existance     -  
 Button action listener should implement abstract method     -  
 relationship between the Canvas class and the Graphics class     -  

Topic Tags

Java OOP