Results 1 to 7 of 7
  1. #1
    feniger is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default abstract sub-class

    is this possible in java?
    abstract class A extends B () {...}
    have a good one - Day I mean...

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    10

    Default

    Yep,

    Here is an example,

    Java Code:
    public class test extends B{
    
    	public test(){
    		this.printA();
    		this.printB();
    	}
    
    	public static void main(String args[]){
    		new test().printA();
    	}
    }
    
    abstract class B extends A{
    	public void printA(){
    		System.out.println("class B");
    	}		
    }
    
    abstract class A{
    	public void printB(){
    		System.out.println("class A");
    	}
    }
    Guess the output....
    freedom exists in the world of ideas

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    22

    Default

    feniger got a nice example. :)

    As that example, you have keep in mind two things.

    1. Abstract class can be subclassed, as you can see.

    2. Abstract classes cannot instantiate. If you do such a things, in compile time, got an error that abstract class cannot be instantiate.

  4. #4
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    10

    Default

    At any level in the class hierarchy you can declare an abstract class.

    Java Code:
    package jf.acls;
    
    public class Concrete {
        @Override
        public String toString() {
            return "Concrete";
        }
    }
    
    package jf.acls;
    
    public abstract class Abstract extends Concrete {
        @Override
        public String toString() {
            return "Abstract extends Concrete";
        }
    
        public abstract int getTheNumber();
    }
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    22

    Default

    Quote Originally Posted by danielstoner View Post
    At any level in the class hierarchy you can declare an abstract class.

    Java Code:
    package jf.acls;
    
    public class Concrete {
        @Override
        public String toString() {
            return "Concrete";
        }
    }
    
    package jf.acls;
    
    public abstract class Abstract extends Concrete {
        @Override
        public String toString() {
            return "Abstract extends Concrete";
        }
    
        public abstract int getTheNumber();
    }
    Nice explanation though. I have one confusion here. What you mean any level of class hierarchy. I'm confusing it with your code. :)

  6. #6
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    10

    Default

    I think his question was if it is possible to extend a concrete class (non-abstract) with an abstract class. And I said yes :) Abstract extends Concrete. I just wanted to make it clear that an abstract class can exist lower in the hierarchy, not only at the top. I hope it is clear now :) because if I continue to explain it will become really messy and unclear. LOL
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    22

Similar Threads

  1. Can this be done using abstract class??
    By romina in forum New To Java
    Replies: 6
    Last Post: 03-02-2009, 01:46 PM
  2. Interface Vs Abstract Class
    By javarishi in forum New To Java
    Replies: 5
    Last Post: 06-15-2008, 05:43 AM
  3. Replies: 0
    Last Post: 04-02-2008, 07:04 AM
  4. Abstract Class
    By gapper in forum New To Java
    Replies: 4
    Last Post: 01-31-2008, 02:25 PM
  5. Abstract Class question
    By maa11235 in forum New To Java
    Replies: 1
    Last Post: 01-05-2008, 11:30 PM

Posting Permissions

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