Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2014
    Posts
    2
    Rep Power
    0

    Default what is abstract class?

    I am just started to learn java and i am facing trouble learning abstract class.can anyone tell me with an example?

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: what is abstract class?

    An abstract class is an class that cannot be instantiated.

    The important thing with an abstract class is: It is marked as abstract:
    Java Code:
    public abstract class MyAbstractClass {
    }
    A class is normally made abstract, when it has abstract methods. An abstract method could be compared with an Interface. It just defines what kind of methods classes should have, when they extend the abstract class and are not abstract themself.

    Java Code:
    public abstract class MyAbstractClass {
       public abstract void myMethod();
    }
    
    public class MyNoLongerAbstractClass extends MyAbstractClass {
      public void myMethod() {
        // Some implementation
      }
    }
    So the second class must implement myMethod() if it is no longer abstract.

    And the usage is like the usage of interfaces: You can split up dependencies. So one part of the code wants to work with an abstract class or an interface. These parts do not need to know details about the exact implementation.
    So you can have something like
    Java Code:
       public MyAbstractClass getAnAbstractClass() {
         return new MyNoLongerAbstractClass();
      }
    
      // And somewehere else
      MyAbstractClass myClass = getAnAbstractClass();
      myClass.myMethod();
    Konrad

  3. #3
    Join Date
    Jun 2014
    Posts
    2
    Rep Power
    0

    Default Re: what is abstract class?

    Quote Originally Posted by kneitzel View Post
    An abstract class is an class that cannot be instantiated.

    The important thing with an abstract class is: It is marked as abstract:
    Java Code:
    public abstract class MyAbstractClass {
    }
    A class is normally made abstract, when it has abstract methods. An abstract method could be compared with an Interface. It just defines what kind of methods classes should have, when they extend the abstract class and are not abstract themself.

    Java Code:
    public abstract class MyAbstractClass {
       public abstract void myMethod();
    }
    
    public class MyNoLongerAbstractClass extends MyAbstractClass {
      public void myMethod() {
        // Some implementation
      }
    }
    So the second class must implement myMethod() if it is no longer abstract.

    And the usage is like the usage of interfaces: You can split up dependencies. So one part of the code wants to work with an abstract class or an interface. These parts do not need to know details about the exact implementation.
    So you can have something like
    Java Code:
       public MyAbstractClass getAnAbstractClass() {
         return new MyNoLongerAbstractClass();
      }
    
      // And somewehere else
      MyAbstractClass myClass = getAnAbstractClass();
      myClass.myMethod();
    Konrad
    THANK YOU ,kneitzel

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: what is abstract class?

    I might add that an abstract class also allows you to add both abstract method declarations and fully implemented methods and instance fields. These implemented methods would be sufficiently useful to most subclasses but can be overridden as required (as long as they are not declared final).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. abstract method-abstract class
    By durgaprasad1407 in forum New To Java
    Replies: 1
    Last Post: 04-29-2011, 06:58 PM
  2. Replies: 4
    Last Post: 04-14-2011, 11:42 AM
  3. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM
  4. Replies: 6
    Last Post: 02-11-2009, 10:51 AM
  5. Replies: 0
    Last Post: 04-02-2008, 07:04 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •