X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Abstract Class & Abstract Method :

In Java we have two types of classes. They are concrete classes and abstract classes.

A concrete class is one which contains fully defined methods. Defined methods are also known as implemented or concrete methods. With respect to concrete class, we can create an object of that class directly.

Example :

class Algo { int a,b; void m1() { // statement 1; } void m2() { // statement 1; } } To call the above method : Algo a = new Algo(); a.m1(); a.m2();


An abstract class is one which contains some defined methods and some undefined methods. Undefined methods are also known as unimplemented or abstract methods. Abstract method is one which does not contain any definition. To make the method as abstract we have to use a keyword called abstract before the function declaration.


abstract modifier :

abstract is the modifier applicable for classes and methods but not for variables.


abstract method :

Even though we don't know about implementation but still we can declare a method with abstract modifier i.e. abstract methods can only be declared but not implemented. Hence, every abstract method declaration should be end with terminator(;). Child classes are responsible to provide implementation for parent class abstract methods.

The abstract methods make us to understand what a method can do but it does not give how the method can be implemented.

Syntax for abstract method : abstract return_type method_name(method parameters if any);

Example:

abstract class Algo { public abstract void m1(); } class Example1 extends Algo { public void m1() { System.out.println("Algo"); } } class Example2 extends Algo { public void m1() { System.out.println("Valley"); } }

The modifiers which are not allowed with abstract methods are final, static, synchronized, native, strictfp and private.


abstract class :

For any Java class if we don't want instantiation then we have to declare that class as abstract i.e. for abstract classes instantiation (creation of object) is not possible.

Every abstract method belongs to a class which is known as abstract class. To make the class as abstract we use a keyword abstract before the class specification.

Syntax for abstract class: abstract class <class_name> { abstract return_type method_name(method parameters if any); }

Example:

abstract class Algo { abstract void sum(); }


With respect to abstract class we cannot create an object direct but we can create it indirectly. An object of abstract class is equal to an object of that class which extends that abstract class.

Example 1 :

abstract class Algo { } class Example extends Algo { public static void main(String [ ] args) { Algo a = new Algo(); } }

Output:

Compile time error : Algo is abstract; cannot be instantiated

Example 2:

abstract class Algo { abstract void sum(); } class isum extends Algo { void sum() { int a,b,c; a = 10; b = 20; c = a+b; System.out.println("INT VALUE = " + c); } } class fsum extends Algo { void sum() { float f1,f2,f3; f1 = 10.00f; f2 = 20.00f; f3 = f1+f2; System.out.println("FLOAT VALUE = " + f3); } } class Example { public static void main(String [ ] args) { // Algo o1 = new Algo (); invalid Algo o2; o2 = new isum(); o2.sum(); o2 = new fsum(); o2.sum(); } }

Output:

INT VALUE = 30 FLOAT VALUE = 30.0

If a class contains atleast one abstract method then it is compulsory to declare it as abstract otherwise we will get compile time error because implementation of the method is not completed and hence we can't create object for that class.

Whenever we inherit 'n' number of abstract methods from abstract base class to derived class, if the derived class defines all 'n' number of abstract methods then the derived class is concrete class. If the derived class is not defining even one abstract method out of 'n' abstract methods then we use a keyword abstract before class name.

Even though a class doesn't contain any abstract method, we can declare it as abstract i.e. abstract class can contain zero number of abstract method. Example : HTTPServlet - this class doesn't contain any abstract method but still it is declared as abstract.

class Algo
{
 public void m1();
} // Compile time error : missing method body, or declare abstract 

class Algo
{
 public abstract void m1()
 {
  
 }
} // Compile time error : abstract methods cannot have a body

class Algo
{
 public abstract void m1();
} // Compile time error :  Algo is not abstract and does not override abstract method m1() in Algo

abstract class Algo
{
 public abstract void m1();
 public abstract void m2();
}
class Example extends Algo
{
 public void m1()
 {
 
 }
} // Compile time error : Example is not abstract and does not override abstract method m2() in Algo
// We can handle this either by declaring class Example as abstract or
by providing implementation for m2().

For abstract methods we have to override it in child classes to provide its implementation where as final methods can't be overridden. Hence, abstract - final combination is illegal for classes.

A final class can't have abstract methods where as abstract class can contain final methods.

final class Algo
{
 public abstract void m1();
} // invalid

abstract class Algo
{
 public final void m1();
} // valid

An abstract base class is one which contains physical representation of abstract methods. An abstract derived class is one which contains logical declaration of abstract methods which are inherited from abstract base class.


Example :

abstract class Algo { abstract void m1(); abstract void m2(); } abstract class C2 extends Algo { void m1() { System.out.println ("m1-C2-ORIGINAL"); } } class C3 extends C2 { void m1() { super.m1(); System.out.println ("m1-C3-OVERRIDDEN"); } void m2() { System.out.println ("m2-C3"); } void m3() { System.out.println ("m3-C3-SPECIALLY DEFINED"); } } class Example { public static void main (String args [ ]) { C3 o3 = new C3(); o3.m1(); o3.m2(); o3.m3(); // C2 o2 = new C2(); invalid C2 o2 = new C3(); o2.m1(); o2.m2(); // o2.m3(); invalid Algo o1 = new C3(); o1.m1(); o1.m2(); // o1.m3(); } }

Output:

m1-C2-ORIGINAL m1-C3-OVERRIDDEN m2-C3 m3-C3-SPECIALLY DEFINED m1-C2-ORIGINAL m1-C3-OVERRIDDEN m2-C3 m1-C2-ORIGINAL m1-C3-OVERRIDDEN m2-C3

An object of either concrete base class or abstract base class contains the details about those methods which are available in that class only but this object (concrete base class or abstract base class) does not contains details of those methods which are specially defined in derived classes.


Example:

import java.awt.GraphicsEnvironment; class Fonts { public static void main (String [ ] args) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String [ ] s = ge.getAvailableFontFamilyNames(); System.out.println("NUMBER OF FONTS = "+s.length); for (int i = 0; i < s.length; i++) { System.out.println (s [i]); } } }

Output:

NUMBER OF FONTS = 272 Agency FB Aharoni Aldhabi Algerian Andale Mono IPA Andalus Angsana New AngsanaUPC Aparajita Arabic Typesetting Arial Arial Black Arial Narrow Arial Rounded MT Bold Arial Unicode MS Baskerville Old Face Batang BatangChe Bauhaus 93 Bell MT..........

Factory method :

A factory method is one whose return type is similar to name of the class where it presents.


Example:

getLocalGraphicsEnvironment();


Rules for factory method :

  • The return type of the factory method must be similar to name of the class where it is present.

  • Every factory method must be static (so that we can call it with respect to name of the class).

  • Every factory method must be public.

  • Factory methods are used for creating an object without using new operator. Every predefined abstract class contains atleast one factory method for creating an object of abstract class.

  • Whenever we define a concrete class then that concrete class also can be made as abstract and it is always further reusable or extendible by further classes.

  • When we define only concrete class we may extend or we may not extend it by derived classes.


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.