Results 1 to 6 of 6

Thread: abstract class

  1. #1
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

    Default abstract class

    why cant an abstract class be instantiated??
    Last edited by swathi dharmaraj; 07-21-2011 at 09:19 PM.

  2. #2
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

  3. #3
    Join Date
    Jul 2011
    Posts
    11
    Rep Power
    0

    Default

    If u wan't a class whose object can't be created , make it abstract class.

    Example ... shape is an abstract class

    where as Rectangle, Circle and Triangle r the concrete sub classes

    When I declare Shape as an Abstract Class ...(becoz Shape is unique so I make it abstract so that the sub classes can use the abstract methods in the super class)

  4. #4
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default

    Simple...
    You cant create an object for abstract class directly...instead you can create object for the class that extends the abstract class...Your question is "Why?" ..
    When you inherit a class with an abstract class, the compiler implicitly creates an object for both the classes...Any class that contain an abstract method must be declared “abstract” and abstract methods can have definitions only in child classes(Inheriting Classes...)...
    Whenever you need your class to prevent instantiated, create it abstract...
    similarly when you need your class to prevent inherited, create it Final...

  5. #5
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default

    An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed..
    Only Abstract classes can have abstract methods...
    They can contain implemented Interfaces...

    abstract interface get
    {
    public void fun();
    abstract void sun();
    }

    abstract class hello implements get
    {
    public void fun()
    {
    System.out.println("fun...");
    }


    abstract void sun() //reports an error that abstract methods cant have a body
    {
    System.out.println("sun...");
    }


    }


    public class one
    {
    public static void main(String args[])throws Exception
    {
    one o=new one();
    //hello h=new hello(); //Shows Error....
    }
    }

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    In your snippet please use code tags

    [code]
    YOUR CODE HERE
    [/code]

    Also; it's unnecessary to say "abstract interface", an interface is implicitly abstract and the explicit qualifier is unnecessary.

    You should also be using naming conventions in classes and methods. For classes the first letter of each word is capitalized
    Java Code:
    Get
    
    Instead of 
    
    get
    And methods have the first letter of the first word lowercase and the remainig words have the first letter capitalized.

Similar Threads

  1. abstract method-abstract class
    By durgaprasad1407 in forum New To Java
    Replies: 1
    Last Post: 04-29-2011, 07:58 PM
  2. Replies: 4
    Last Post: 04-14-2011, 12:42 PM
  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, 03:33 PM
  4. Replies: 6
    Last Post: 02-11-2009, 11:51 AM
  5. Replies: 0
    Last Post: 04-02-2008, 08:04 AM

Posting Permissions

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