Total members 11890 |It is currently Tue Apr 23, 2024 10:51 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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");
}

}




_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 109
Have thanks: 5 time

can a non-abstract class have abstract methods?


Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

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

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

updated.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

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


Author:
Newbie
User avatar Posts: 6
Have thanks: 0 time

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.


Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

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();
}


Author:
Newbie
User avatar Posts: 6
Have thanks: 0 time

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


Author:
Newbie
User avatar Posts: 12
Have thanks: 1 time
Post new topic Reply to topic  [ 8 posts ] 

  Related Posts  to : What is an Abstract Class? !!!
 java abstract class,concrete class and interface     -  
 why cant instantiate an abstract class     -  
 make abstract class     -  
 how to Define abstract class in php     -  
 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






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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