Home | Login | Blog | Forum | Download | RSS Feed | Ajax | Hello-World | J2EE | BPM | EJB | FileNet | Hibernate | ORM | IBM | ILOG | Interview | FAQs |

Java | Apache | JVM | Others | BRMS | Oracle | Pega | SOA | Sun-Certification | Web-Service | websphere | XML |
 

JAVA DEVELOPER SITE - COMPLETE JAVA TUTORIALS

Java Abstract Class Example. How to use Abstract Class in Java?


Share
 
 

Useful Java Links


       


Abstract classes are used in Object Oriented Programming languages like Java. An abstract class, in terms of implementation, is somewhere between an implementation class and an interface.

What is an abstract class?

Let us figure out what is an abstract class. Let us assume that you have a base class called BaseClass and a derived class DerivedClass that is the implementation class and is a sub-class of BaseClass.

 

public class BaseClass {
BaseClass(){}
//Default implementation for the method.
//Sub-classes need not override this method
//if they are happy with the default implementation.
public void defaultMethod_1(){
}
//Other default methods go here
//Ideally all sub-classes should have own
//implementations for all the dummy methods
public void dummyMethod_1(){
}
//Other dummy methods go here.
}

 

 


public class DerivedClass extends BaseClass {
//Own implemetation for the dummy method
public void dummyMethod_1() {
}
//Similarly for all the other dummy methods
}
Now say, you want the following
  1. BaseClass needs to have implementation for some of its object methods (let us call them Default methods). So that the DerivedClass need not override them, if they don’t want any other behavior.
  2. BaseClass have some of the methods (let us call them Dummy Methods) which, it STRICTLY want all of its child classes to override. Infact, the BaseClass wants to throw an error, if they don’t override these methods.
  3. BaseClass is too generic a class (like a Vehicle Class) and it does not make any sense to create an object of it by calling

newBaseClass()
All the initialization should be done through the subclasses. The above implementation of the BaseClass and DerivedClass clearly will not help us in achieving these. One modification to support (1) and (2) may be to throw UnSupportedOperationException() from the dummy methods of BaseClass.

 

//Base class implementation of the dummy methods
public void dummyMethod_1(){
throw new UnsupportedOperationException();
}

 
But they have the following issues.
  1. They will be found only during the run time and not at compile time.
  2. You cannot use UnSupportedOperationException in case of BaseClass constructor to prevent calling the BaseClass constructor directly. Because this constructor gets called even when you are calling a DerivedClass constructor using

BaseClass bc =newDerivedClass();
In fact, it will be the first one to get called.

How to achieve all the three requirements

All the above three requirements can be achieved by modifying the BaseClass to have the dummy methods as abstract methods and make it an abstract class. So here is what you have to do to the BaseClass(you need to modify only the base class
  1. All the dummy methods will have the keyword ‘abstract’. These methods will not have any method bodies but will have only declaration.
  2. If you have more than one abstract methods in a class, then Java does not allow the instantiation of objects of this class. Java achieves this by making sure at compile time that all the classes which have more than one abstract methods, will be abstract classes.

public abstract class BaseClass {
//You can still have an implementation
//for constructors
BaseClass(){
}
//Default implementation for the method.
//Sub-classes need not override this method
//if they are happy with the default implementation.
public void defaultMethod_1(){
}
//Other default methods go here
//Abstract methods have to be implemented by sub-classes
//Notice that they don't have implementation and ends
//with a semicolon.
public abstract void dummyMethod_1();
//Other abstract methods go here.
}
 



Download Latest Java/J2EE eBooks, SCJP Dumps, SCWCD Dumps, Pega Tutorial, IBM WebSphere BPM tutorials and many more


If you need any urgent assistance on java-abstract-class-example-program, kindly email your requirement to us at : info@javagenious.com or Contact-an-Expert. Our experts will try their best to solve your problem.

You can also subscribe to our newsletters on java-abstract-class-example-program, to receive updates on java-abstract-class-example-program,via email.Enter you email below:

Enter your email address:

Delivered by FeedBurner



Keyword Tags: java-abstract-class-example-program tutorial,java-abstract-class-example-program in java,Concepts of java-abstract-class-example-program,Java,J2EE,Interview Questions,java-abstract-class-example-program Examples


Comments:


Post Your Comment:

*

Yes, I would like to recieve email notifications on my reply.

Popular Posts:

 
Popular Downloads

SCJP 1.6 Dumps
JSP Interview Questions
Struts Interview Questions
Hibernate Interview Questions
SCWCD Dumps
SCBCD Dumps

Java/J2EE Tutorial
JMS Tutorials
seam richfaces tutorial
richfaces live demo
jquery example demo
JQuery Tutorial and Source Code
JSF Tutorial
XSL Tutorial
J2EE JMS
CORBA Applications
Java CERTIFICATION
SCJP 1.5
SCJP 1.6
SCWCD 1.5
SCBCD
Mis
Ajax autosuggest autocomplete from database
Struts forms and validation with webflow
Java Apache POI Examples
JDBC Tutorial
Oracle 9i & 10g
MYSQL Tutorial
php Tutorial
JQuery Examples
Upload Java File
JAXB Examples
PL-SQL Tutorial
Dojo AJAX
EJB Examples
Java IDE
Eclipse
NetBeans
IBM Rational Weblogic Workshop
Downloads
SCJP 1.6 Dumps
SCJP 1.5 DUMP
SCJP Material
SCWCD
Java Security
SCBCD Dumps
External Links
SUN Java
JOracle
IBM
PHP Certification
Technical Interview Questions
Amazon Interview Questions
Google Interview Questions
Microsoft Interview Questions
IBM Interview Questions
Yahoo Interview Questions
All Tutorials
php mysql tutorial
SCJP 1.5/1.6
SCWCD 1.5/1.6
Polymorphism
Thread Tutorial
Generics & Exceptions
Spring Struts Hibernate
Java Collections
MAP
SET
LIST
VECTOR
Advanced Java
Stateless Session Bean
Introduction to EJB
Stateful Session Bean
Java Design Patterns
HOME | FORUM | ABOUT | BLOGS | CONTACT-US