Monday, July 9, 2012

Abstract classes and Abstract Methods in Java

Abstract Methods:
  • They are present in abstract classes
  • The keyword "abstract" is prepended to the methods
  • Abstract methods are implemented only in sub-class / inherited class and not in "Abstract" class
  • Since abstract methods are implemented in sub-class, the return type of abtract methods is either "protected" or "public"

Abstract Classes:
  • A class that contains "abstract" methods is an abstract class
  • Abstract classed can contain "non static" and "non final" member variables
  • They can NEVER be instantiated. They can be ONLY INHERITED
  • One abstract class can inherit another abstract class
  • Abstract classes can contain ordinary methods as well as abstract methods
Sample Implementation:


/**
 * ABSTRACT CLASS
 * /
public abstract class AbstractTest {
    public abstract void testPrint1();
    protected abstract void testPrint2();
}

/**
 * ABSTRACT CLASS IMPLEMENTED

 * NOTE: IMPL CLASS MUST IMPLEMENT ALL ABSTRACT METHODS IN ABSTRACT CLASS
 */

public class AbstractTestImpl extends AbstractTest {
    public void testPrint1() {
        System.out.println("Test Print One!");
    }

    public void testPrint2() {
        System.out.println("Test Print Two!");
    }

    public static void main(String args[]) {
        AbstractTestImpl obj = new AbstractTestImpl();
        obj.testPrint1();
    }
}

12 comments:

  1. Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
    python Training in Pune
    python Training in Chennai
    python Training in Bangalore

    ReplyDelete
  2. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us
    Best Devops Training in pune
    Devops Training in Bangalore
    Power bi training in Chennai

    ReplyDelete
  3. Very well written blog and I always love to read blogs like these because they offer very good information to readers with very less amount of words....thanks for sharing your info with us and keep sharing.

    web designing training in chennai

    web designing training in annanagar

    digital marketing training in chennai

    digital marketing training in annanagar

    rpa training in chennai

    rpa training in annanagar

    tally training in chennai

    tally training in annanagar

    ReplyDelete
  4. I cannot thank you enough for the blog.Thanks Again. Keep writing.
    core java online course
    core java online training

    ReplyDelete
  5. Great tips and very easy to understand. This will definitely be very useful for me when I get a chance to start my blog.
    data science course fee in hyderabad

    ReplyDelete