Results 1 to 6 of 6
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    6

    Default Creating objects of Abstract Classes ?

    Java Code:
    abstract class E
    {
    	int age;
    }
    
    class Z
    {
    	E makeE()
    	{
    		return new E() {};
    	}
    }
    public class TestingTest 
    {
    	public static void main(String[] args)
    	{
    		Z z = new Z();
    		E hey = z.makeE();
    		hey.age = 15;
    		System.out.println(hey.age);
    	}
    }
    I thought you were not allowed to create objects from Abstract classes, but this code works just fine.

    Help please ?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,414
    Blog Entries
    7
    Rep Power
    25

    Default Re: Creating objects of Abstract Classes ?

    new E() { } is not an object of an abstract class (e.g. there are no abstract methods).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    6

    Default Re: Creating objects of Abstract Classes ?

    Thanks, this made me understand as well:
    Java Code:
    package myPackage;
    
    abstract class E
    {
    	int age;
    	abstract void hello();
    }
    
    class Z
    {
    	E makeE()
    	{
    		return new E() {
    
    			@Override
    			void hello() {
    				System.out.println("Hello");
    			}};
    	}
    }
    
    public class TestingTest 
    {
    	public static void main(String[] args)
    	{
    		Z z = new Z();
    		E hey = z.makeE();
    		hey.age = 15;
    		System.out.println(hey.age);
    	}
    I am creating an object that extends the Abstract class.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    23

    Default Re: Creating objects of Abstract Classes ?

    Regardless of whether the class you extend is abstract or concrete, adding a pair of braces after the constructor call means you are defining an anonymous inner class. Plug this into a main(...) method and see what it prints:
    Java Code:
    System.out.println(new Object() {}.getClass().getName());
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    6

    Default Re: Creating objects of Abstract Classes ?

    Why is the last post deleted ?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    23

    Default Re: Creating objects of Abstract Classes ?

    Quote Originally Posted by fatabass View Post
    Why is the last post deleted ?
    http://www.java-forums.org/new-java/...onymously.html

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Abstract Classes.
    By maknib in forum New To Java
    Replies: 3
    Last Post: 05-12-2011, 02:30 PM
  2. Replies: 0
    Last Post: 04-02-2011, 02:55 AM
  3. Abstract Classes??? Creating a new object????
    By jonytek in forum New To Java
    Replies: 2
    Last Post: 03-07-2011, 04:34 AM
  4. classes and creating objects
    By Latanyar in forum New To Java
    Replies: 4
    Last Post: 11-01-2010, 08:14 PM
  5. abstract classes
    By renju krishnan in forum New To Java
    Replies: 1
    Last Post: 09-29-2010, 08:31 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
  •