Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Why and where abstract methods & classes and static methods are used?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Location
    Chandigarh, India
    Posts
    6
    My Mood
    Cool
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Why and where abstract methods & classes and static methods are used?

    Hello to all ,
    Actually I know how to declare and implement abstract method's and classes and static method's,
    But the confusion is that where is exactly used ?? as i got this question in interview also,
    just wanna know where it is exactly used and why is needed .
    as we declare abstract method in super class and implement in subclass ,but we can do this in subclass also without declaring in super class why and where is needed,
    please reply me soon??


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Why and where abstract methods & classes and static methods are used?

    By declaring an abstract method in an abstract class and then inheriting the abstract method, you are able to create many different implementations of one method and invoke that method in your code without having to know what subclass you are invoking it on. Alternatively, you can use a non-abstract method to call an abstract method, which when you implement it will have a proper algorithm.

    For example, look at the code below (will not compile as is since classes were not created properly, but the rest of it would work):
    public abstract class A {
    	public int getValue() {
    		return calculate(5);
    	}
    	public abstract int calculate(int n);
    }
     
    public class B extends A{
    	@Override
    	public int calculate(int n) {
    		return n+5;
    	}
    }
     
    public class C extends A{
    	@Override
    	public int calculate(int n) {
    		return n+10;
    	}
    }
     
    public class Runner {
    	public static void main(String args[]) {
    		A obj1 = new B();
    		A obj2 = new C();
     
    		//Call obj1's implementation
    		System.out.println(obj1.calculate(7)+" equals 12");
    		System.out.println(obj1.getValue()+" equals 10");
    		//Call obj2's implementation
    		System.out.println(obj2.calculate(7)+" equals 17");
    		System.out.println(obj2.getValue()+" equals 15");
     
    		//Add both objects to an array
    		A[] objects = new A[2];
    		objects[0] = obj1;
    		objects[1] = obj2;
     
    		//Print the getValue() method of each object in the array
    		for(int i=0;i<objects.length;i++) {
    			System.out.println(objects[i].getValue());
    		}
    	}
    }

    That code will produce the output:
    12 equals 12
    10 equals 10
    17 equals 17
    15 equals 15
    10
    15
    I'll let you read over that. If you need anything clarified, feel free to ask.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    ajaysharma (July 14th, 2012)

  4. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why and where abstract methods & classes and static methods are used?

    I'd start by defining each.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why and where abstract methods & classes and static methods are used?

    Please read the forum rules. I have merged your duplicate thread. You've got two people replying to your same question in different locations - not very considerate of the volunteers time.

  6. The Following User Says Thank You to copeg For This Useful Post:

    ajaysharma (July 14th, 2012)

  7. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why and where abstract methods & classes and static methods are used?

    And post #4 applies not just to double posts in a single forum, but to cross posts as well.

    This thread has been cross posted here:

    http://www.java-forums.org/advanced-java/61365-why-where-abstract-methods-classes-static-methods-used.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  8. The Following User Says Thank You to copeg For This Useful Post:

    ajaysharma (July 14th, 2012)

  9. #6
    Junior Member
    Join Date
    Jul 2012
    Location
    KPK
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Red face Re: Why and where abstract methods & classes and static methods are used?

    an abstract method have no body while the body of the abstract method is in some other class.
    abstract class provide default functionality for the subclass.
    Last edited by captain kashif; July 13th, 2012 at 02:37 PM.

  10. The Following User Says Thank You to captain kashif For This Useful Post:

    ajaysharma (July 14th, 2012)

  11. #7
    Junior Member
    Join Date
    Jul 2012
    Location
    Chandigarh, India
    Posts
    6
    My Mood
    Cool
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Why and where abstract methods & classes and static methods are used?

    Hi aussiemcgr ..Thank you for your instant help..
    I'm getting exactly, what u said..but can you give me some real world example of abstract method and classes that where it is used,
    I mean like where it has to be needed to get abstract method & classes,..??

  12. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Why and where abstract methods & classes and static methods are used?

    I provide an example of why and where for abstraction here.
    Last edited by helloworld922; July 14th, 2012 at 01:48 AM.

  13. The Following User Says Thank You to helloworld922 For This Useful Post:

    ajaysharma (July 14th, 2012)

Similar Threads

  1. Replies: 12
    Last Post: January 1st, 2011, 03:47 AM
  2. newbie question about Abstract methods
    By FailMouse in forum Java Theory & Questions
    Replies: 3
    Last Post: August 10th, 2010, 11:51 PM
  3. Graphic Environment Abstract Methods
    By striko_514 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 5th, 2010, 01:01 AM