Thursday, 13 October 2011

Abstract class and method


abstract Class

Sometimes, a class that is defined represents an abstract concept and, as such, should not be instantiated. Similarly in object-oriented programming, some times it might be need to model an abstract concept without being able to create an instance of it. For example, the Number class in the java.lang package represents the abstract concept of numbers. It makes sense to model numbers in a program, but it doesn't make sense to create a generic number object. Instead, the Number class makes sense only as a superclass to classes like Integer and Float, both of which implement specific kinds of numbers. A class such as Number, which represents an abstract concept and should not be instantiated, is called an abstract class.

Here the case is that  where need to define superclass but do not provide implementation of each method such a class is known as abstract class.

An abstract class provides a base for someone to extend an actual class. classes cannot be instantiated, but they can be subclassed.

An abstract class provide the facility to  put the common method names in one abstract class without having to write the actual implementation code.

An abstract class can be extended into sub-classes, these sub-classes usually provide implementations for all of the abstract methods.

To create abstract class consider following format:
          abstract class ClassName
                   {
                             // class member
                   }

Here , the class which declared as abstract is preceded by the abstract keyword.
The key idea with an abstract class is useful when there is common functionality that's  like to implement in a superclass and some behavior is unique to specific classes. So you implement the superclass as an abstract class and define methods that have common subclasses. Then you implement each subclass by extending the abstract class and add the methods unique to the class.

     Points  to be remembered about  abstract class :
1.     Abstract class contains abstract methods.
2.     Program can't instantiate an abstract class.
3.     Abstract classes contain mixture of non-abstract and abstract methods.
4.     If any class contains abstract methods then it must implements all the abstract methods of the abstract class.


abstract method

An abstract class may contain abstract methods,abstract method is the method which has no implementation and whose definition is preceded by the abstract keyword. It takes following form:

          abstract return_type methodname(parameter list);

Here, the method has no body and followed by the semicolon.

Abstract methods are sometimes called as Sub classes responsibility because super class only declare abstract method but sub class takes responsibility of defining them.

A class that contains one or more abstract methods is also an abstract class

Ex,

  abstract class A
  {
            abstract void add ();
  }

  class B extends A
  {
            void add ()
            {
                      body of the method
            }
  }

  class AB
  {
            public static void main (string args [])
            {
     B b1=new B ();
     B1.add();
            }
  }

Generally we are not instantiating object of abstract class, because we are not doing anything in abstract class but object can be used to print a subclass object.It possible to create reference variable of that class type.

Posted by : Ruchita Pandya

No comments:

Post a Comment