Java Tutorial

Abstract Classes

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.

For example:

class C1
{
    int a,b; void f1 ()
    {
        .........;
        .........;
    }
    void f2 ()
    {
        .........;
        .........;
    }
};

To call the above method:

C1 O1=new C1 ();
O1.f1 ();
O1.f2 ();

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.

Syntax for Abstract Class:

abstract return_type method_name (method parameters if any);

For example:

Abstract void sum ();

The abstract methods make us to understand what a method can do but it does not give how the method can be implemented. Every abstract method belongs to a class under class is known as abstract class, to make the class as abstract we use a keyword called abstract before the class specification.

Syntax for Abstract Class:

abstract class <clsname>
{
    Abstract return_type method_name (method parameters if any);
};

For example:

abstract class Op
{
    abstract void sum ();
};

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

For example:

class CC extends AC
{
    ...........;
    ...........;
};
AC Ao=new AC (); //invalid AC Ao=new CC ();
or
AC Ao;
Ao=new CC ();

Write a JAVA program for computing sum of two integers and floats using abstract classes?

Answer:

abstract class Op
{
    abstract void sum ();
};

class isum extends Op
{
    void sum ()
    {
        int a,b,c;
        a=10;
        b=20;
        c=a+b;
        System.out.println ("INT VALUE = "+c);
    }
};

class fsum extends Op
{
    void sum ()
    {
        float f1,f2,f3;
        f1=10.26f;
        f2=20.32f;
        f3=f1+f2;
        System.out.println ("FLOAT VALUE = "+f3);
    }
};

class AbDemo
{
    public static void main (String k [])
    {
        //	Op o1=new Op (); invalid Op o2;
        o2=new isum ();
        o2.sum ();
        o2=new fsum ();
        o2.sum ();
    }
};

Abstract classes should not be final, since, they are always reusable. Abstract classes are basically used to implement polymorphism; we use the concept of dynamic binding. Hence, abstract classes, polymorphism and dynamic binding will improve the performance of JAVA J2EE applications by reducing amount of memory space.

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 at least one abstract method out of 'n' abstract methods then the derived class is known as abstract derived class and to make that class abstract, we use a keyword called abstract.

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.

Abstract class

Implement the above diagram by using abstract class's polymorphism and dynamic binding.

Answer:

abstract class C1
{
    abstract void f1 ();
    abstract void f2 ();
};

abstract class C2 extends C1
{
    void f1 ()
    {
        System.out.println ("f1-C2-original");
    }
};

class C3 extends C2
{
    void f1 ()
    {
        super.f1 ();
        System.out.println ("f1-C3-OVERRIDDEN");
    }
    void f2 ()
    {
        System.out.println ("f2-C3");
    }
    void f3 ()
    {
        System.out.println ("f3-C3-SPECIALLY DEFINED");
    }
};

class AbDemo1
{
    public static void main (String k [])
    {
        C3 o3=new C3 ();
        o3.f1 ();
        o3.f2 ();
        o3.f3 ();
        //	C2 o2=new C2 (); invalid C2 
        o2=new C3 ();
        o2.f1 ();
        o2.f2 ();
        //	o2.f3 (); invalid C1
        o1=new C3 ();
        // or o2 o1.f1 ();
        o1.f2 ();
        //	o1.f3 ();
    }
};

Output:

With respect to Concrete Class:
C3 o3=new C3 ();
o3.f1 (); // f1 - overridden - C3
o3.f2 (); // f2 - C3
o3.f3 (); // f3 - defined in - C3

With respect to Abstract Derived Class:
C2 o2=new C2 (); // invalid
C2 o2=new C3 ();
o2.f1 (); // f1 - overridden - C3
o2.f2 (); // f2 - C3
o2.f3 (); // invalid

With respect to Abstract Base Class:
C1 o1;
o1=o2; // it mean., new C3 ()
o1.f1 (); // f1 - overridden - C3
o1.f2 (); // f2 - C3
o1.f3 (); // invalid

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 class's.

Write a JAVA program to display the fonts of the system?

Answer

import java.awt.GraphicsEnvironment; class Fonts
{
    public static void main (String k [])
    {
        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]);
        }
    }
};

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

For example:

getLocalGraphicsEnvironment ();

RULES for factory method:

  1. The return type of the factory method must be similar to name of the class where it presents.
  2. Every factory method must be static (so that we can call with respect to name of the class).
  3. Every factory method must be public.

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

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

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