Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Overloading Abstract Class Methods ??

  1. #1
    sreeram is offline Member
    Join Date
    Dec 2012
    Location
    Hyderabad
    Posts
    4
    Rep Power
    0

    Unhappy Overloading Abstract Class Methods ??

    Java Code:
    import java.util.Scanner;
    abstract class vol{
    abstract volume(){}
    class cuboid extends vol
    {
    	
    	void volume(int l,int b,float h) /// is this possible?? if not how can implement the concept??
    	{
    		System.out.println();
    	}
    }
    public class inheritance {
    public static void main (String args[]){
    	System.out.println("Hello World 1");
    	Scanner sc=new Scanner(System.in);
    	System.out.println("Enter an integer");
    	int s=sc.nextInt();
    	System.out.println(s+2);
    }}
    i have a abstract class shape
    i have 3 classes deriving from it..
    cuboid sphere and cube..

    how to implement them..

    how can i modify this particular programm

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Overloading Abstract Class Methods ??

    Hi,

    what is your question really?

    Yes, you can overload abstract classes. It's about the same thing as overloading other non-abstract classes, though in your abstract class case you will have to provide the implementation in a subclass.
    You can not do it the way you have coded it. You need to declare the method in the abstract class the way that you want it to be implemented. You cannot declare it without parameters and then implement it with parameters.

    You could for example have it like this if you want to calculate volumes:

    Java Code:
    public abstract class Shape {
    
    	abstract void volume(int x, int y, int z);
    	abstract void volume(int radius);
    
    }
    And you could implement it with subclasses that are of certain shapes, such as..

    Java Code:
    public class Sphere extends Shape{
    
    	@Override
    	void volume(int x, int y, int z) {
    		System.out.println("A sphere's volume isn't calculated this way!");	
    	}
    
    	@Override
    	void volume(int radius) {
    		System.out.println("The volume of the sphere is " + (4*Math.PI*Math.pow(radius, 3))/4);
    	}
    
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Overloading Abstract Class Methods ??

    I would assume that the volume method should return something, probably a double. Your initial abstract class (and note all class name should begin with an upper case letter) shouldn't even compile because you don't give volume a return type.

    Next, if your class will be holding fields that define its shape and size, then the volume method would likely need no parameters at all. Also, since the volume method would be inherited from a parent abstract class, indeed it should have no parameters, else it would be impossible to use this method for diverse shapes that all would require different numbers and types of parameters.
    Zyril likes this.

  4. #4
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Overloading Abstract Class Methods ??

    Ah, I never thought of that, Fubar! That is a nice way of using it! =)

  5. #5
    sreeram is offline Member
    Join Date
    Dec 2012
    Location
    Hyderabad
    Posts
    4
    Rep Power
    0

    Default Re: Overloading Abstract Class Methods ??

    thanks FUbar.. thats what i wanted to know...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 1
    Last Post: 01-27-2012, 07:26 AM
  2. Replies: 3
    Last Post: 11-22-2011, 05:51 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. Overloading abstract methods??!?
    By tfitz666 in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 09:45 AM
  5. Abstract Class with Static Methods
    By bugger in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 12:20 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
  •