Wednesday, May 29, 2013

Classes and use of "abstract" keyword in Java



Types of classes in Java:

 There are two types of classes in Java. They are:
1)      Concrete classes
2)      Abstract classes

Concrete Class:
A class is said to be concrete class if and only if it contains fully defined methods. Fully defined methods are known as implemented or concrete methods.
Once a class is concrete than we can create object of that class directly.

class B
{
            int x,y;
            void add()
            {
                        …………
                        …………        
}
void sub()
{
                        …………
            …………
}
}

Abstract Class:
·         A class is said to be an abstract class if and only if contains some defined and some undefined methods.
·         With respect to an abstract class we can’t create an object of that class directly but we can create an object of abstract class indirectly.
·         Undefined methods in java are known as unimplemented or abstract methods.
·         An abstract method is one which does not contain any definition.
·         To make a method abstract we must use the keyword called “abstract” before the method declaration.
·         Syntax:
[access specifier] abstract returnType methodName(param, if any);
            e.g. public abstract void add(int a, int b);

·         Abstract method provides security.

NOTE: Abstract method always makes us to understand what a method can do but we can’t understand how a method can be defined.

·         A special case we have a class in which all methods are defined but we put abstract before the class to make the class secure because now we can’t create object of that class direct.

·         Once a class contains an abstract method then that class is known as an abstract class. To make a class as an abstract class we must use a keyword “abstract” before class specification.

·         Syntax of abstract class:
abstract class ClassName
{
abstract methods;
} 
·         An abstract class is one in which we have at least one abstract method.
·         Abstract classes are basically used to implement polymorphic application. To execute polymorphic application we must use dynamic binding hence abstract classes with polymorphic application along with dynamic binding improves the performance of the java program by reducing amount of memory space.

 Sample Program

abstract class Operation
{
            abstract void add();
};

class IntSum extends Operation
{
            int i1,i2,i3;
            void add()
            {
i1 =10;
i2 = 20;
i3 = i1 + i2;
                        System.out.println(“Sum of the integers”, +i3);
            }
};

class FloatSum extends Operation
{
            float f1,f2,f3;
            void add()
            {
f1 =1.5f;
f2 = 2.5f;
f3 = f1 + f2;
                        System.Out.println(“Sum of the floats”, +f3);
            }
};

Class AbstractDemo
{
            public static void main(String args[ ])
            {
                        Operation o;
                       
o = new Intsum();
                        o.add();
                       
o = new Floatsum();
                        o.add();
            }
};

·         An object of the abstract class but not create directly but it can be created indirectly. It means that no allocation of memory by using new keyword.
·         An object of an abstract class is nothing but an object of that class which is extending this abstract class.
·         Every abstract class if further extendable / reusable.
·         Abstract class could not be final.

Note: - If we have four classes in the 4 different files then we are not using semicolon (;) mark at the end of each class but if we have 4 classes in the same java file then we have to terminate classes one after another.
·         Abstract base class: An abstract base class is one which contains physical representation of abstract methods.
·         Abstract derived class: An abstract derived class is one which contains logical representation of abstract methods which are inherited from abstract base class.
·         We can create an object of an abstract class indirectly in 2 ways: -
1)      We can create the object of that class (Derived) which is extending the abstract class and the object of that derived class will become the object of the abstract class.     
2)      We can use the “factory methods”.

Factory Methods:
·         A factory method is that whose return type is similar to the name of the class where it presented.
·         Factory methods are used to create an object of the class without using the “new” keyword.

Rules for the factory method: -
1)      Return type of the factory method must be similar to the name of the class.
2)      Every factory method must be “static”.
3)      Every factory method must be “public”.        

Sample Example:

import java.awt.GraphicsEnvironment;
class FactoryFonts
{
            public static void main(String args[ ])
            {
GraphicEnvironment ge =  GraphicEnvironment.getLocalGraphicsEnvironment(); //Factory Method
                        String fonts = ge.getAvailableFontFamilyNames();
                        Syatem.out.println (“Total available fonts: ”, +fonts.lenght);
                        Syatem.out.println (“Available fonts names: “);
                        for(int i = 0;i< font.lenght; i++)
                        {
                                    System.out.println (“Font Name: “, +fonts[i]);
                        }
}
}

In the above program ,”awt” is a package, which is used here to import a class “GraphicsEnviorment” now this “GraphicsEnviorment”  is an abstract class and it has an abstarct method getLocalGraphicsEnviorment().

The above program collects and prints all the system fonts.

No comments:

Post a Comment