Working with abstract classes and methods

Working with abstract classes and methods


Posted in : Core Java Posted on : October 7, 2010 at 4:03 PM Comments : [ 0 ]

This section describes about the abstract classes and methods in java.

Working with abstract classes and methods

Sometimes you want to create a super class that only defines a generalized form that will be shared by all of its subclasses, leaving it to each class to fill in the details. This is implemented through abstract classes.

Abstract class can be defined as follows :

abstract class Ankit{

//abstract methods

// concrete methods

}

In many situation , it requires that certain methods be overridden by subclass by specifying the abstract type modifier. These methods resides in abstract class and they are known as abstract method.

These methods are sometimes referred to as subclass responsibility because they have no implementation specified in the superclass. The general form to declare abstract method is as follows :

abstract type name(parameter-list);

As you can see no body is present. There can be no objects of an abstract class. An abstract class can't be directly instantiated with the new operator.

Example :


abstract class A {
	abstract void callme();

	// Concrete methods are still allowed in abstract classes
	void demo() {
		System.out.println("This is a concrete method");
	}
}

class B extends A {
	void callme() {
		System.out.println("B class' implementation of callme");

	}
}

public class DemoAbstract {
	public static void main(String args[]) {
		B b = new B();
		b.callme();
		b.demo();
	}

}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac DemoAbstract .java

C:\Program Files\Java\jdk1.6.0_18\bin>java DemoAbstract
B class' implementation of callme
This is a concrete method 

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics