Wednesday, 24 February 2016

Java:Providing common features to all sub classes by using abstract classes

 Before going to know how to provide a common feature to all the sub classes using the super class called abstract class, we must know about what is abstract feature of java gives to us and how it works. Lets start by knowing what is it and how to use it.

why abstract classes in java ?

  •  There are situations where we need to give a common feature to all the derived class and the super class declares the structures of a given abstract without providing any implementation in that. 
  • Some times we want to create a super class that only defines a generalized form that will be shared by all of its child classes, leaving it to each child class to fill in the details.


How to Define abstract classes in java ?

 Java provides this functionality by using the abstract keyword. We can define/use abstract methods and classes by using this keyword. like

Syntax:

abstract class class_name;

Example:-

abstract class myClass
{

// abstract class code;

}


In the above example, the abstract class myClass contain nothing , it contain all the method declarations but not implementations.          

                       Abstract classes are classes whose sole purpose is to provide same feature to all sub-classes. Abstract class cannot be directly instanced with new operator, but they can contain class and instance variables and methods. Abstract methods are methods with signature but no implementation.

 So, abstract methods are very useful concepts in programming where common implementation is given to all of child classes.

Abstract classes example programs in java

                                 Let us look at the following block of code and you can defiantly know what are abstract classes are and how to use them in your java program.


 Abstract class mobile
{
public abstract call();    // a method that is declared using abstract keyword is Abstract method.
}

public class nokiaMobile extends mobile
{
    public void call()
      {
       System.out.println(" this is a call method of nokiaMobile ");
      }
  public static void main(String args[])
   {

mobile mob = new nokiaMobile();    // calling abstract methods those are implemented in sub classes.
}

}

          In the above program, we are first defining an abstract classes called mobile which contain abstract method call(). Then, we extend the mobile with our new class called nokiaMobile, where we implement the call() abstract method. Finally, I have called the implementation by using object type of mobile.

          If the extended classes of abstract class are not provided any implementation to the abstract methods, then those classes must be defined as abstract.

 So, when to use abstract classes in java ?

                                                                There so many situations when to use abstract classes in java. Among those the following are the some one.

  • When the super classes need to provided same features to all of the sub classes
  • To prevent it from being instantiated by the subclass 
  • when a class contain one or more abstract methods.

 1.When the super classes need to provided same features to all of the sub classes

                                                      When the super classes need to provided same features to all of the sub classes one can declare the super class as abstract and it contain all the abstract methods. These abstract methods are implemented in the sub classes. For example

   In the above figure the mobile contain three abstract methods, and those all are implemented in three sub classes.

 Why abstract class cant be instantiated

An abstract classes is a class that can be defined using the keyword called "abstract" and we have also see why they are used. Know it is time to know how to crate a abstract class and how to implement it. For example consider the  following code


abstract class A
{
abstract void messageDemo();        // abstract method

public void show()
{
System.out.println(" this is an abstract class");
}
}

                                                   In the above class "A" it contain two  methods  messageDemo() and show() respectively. But the  messageDemo() method is doing nothing here or we don't know to to implement it here. such methods in java can be declared as abstract methods. abstract methods are the methods which consists only method declaration but not definition. That we know what is method but we does not know how it works.

Then how to implement this type of method is by extending the abstract class. In the sub classes we implement abstract methods as 

class sub extends A
{
void  messageDemo()                 // abstract methods implementation in sub class
{
System.out.println(" this a derived class");
}
}

This type of implementation process leads to more useful

are abstract class cant be instantiated?

                                                                        The java  developer develop the JVM when it finds the abstract keyword it just this that there is no need to create object since it is an abstract class. abstract class containers for  abstract methods with no implementation. So if this is the case,we don't know how much memory is allocated to that class and the JVM is unable to allocate memory. "If we declare an abstract class with all concrete methods at time also it is not possible to allocate memory since JVM ignore abstract class to allocate memory". 

                               A another reason is Why abstract class cant be instantiated is if you do this you need constructors for allocating memory. creating a constructor for an abstract class is not possible.

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top