Java2 codes,problems ,discussions and solutions are here
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.
Examplejava 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");
}
}
Fri Feb 20, 2009 6:56 am
can a non-abstract class have abstract methods?
Fri Feb 20, 2009 10:23 am
nope it can't , To have a abstract methods you must define your class as abstract .
Thu Jan 24, 2013 12:04 am
updated.
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.
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.
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();
}
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.
Topic Tags
Java OOP
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.