Results 1 to 8 of 8
  1. #1
    sid123 is offline Member
    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    Default interface and abstract class

    when to use interface and abstract class.give me a real time example.

    thnks
    sid123

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: interface and abstract class

    What do you think?
    Do you know the difference between them?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    sid123 is offline Member
    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    Default Re: interface and abstract class

    i know the difference but i need a real time example such that i can use them in any application

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: interface and abstract class

    Quote Originally Posted by sid123 View Post
    i know the difference but i need a real time example such that i can use them in any application
    Look at the List interface, the abstract class AbstractList and the LinkedList and ArrayList classes for a fine example.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    sid123 is offline Member
    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    Default Re: interface and abstract class

    still i am not getting can u give any other example.

    thnks

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: interface and abstract class

    If I gave you another example, you wouldn't understand it either. The example I gave you is prime example; what more do you want?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Kkenjoy is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: interface and abstract class

    I can give u some code but what JosAH said is better to check.

    If you know the differences then you will understand:

    Interface:
    Java Code:
    public interface Calculator {
    
    	public double Sum(int x,int y);
    }
    Java Code:
    public class CalculatorWithTax implements Calculator{
    
    	private final double tax = 0.03; 
    	
    	@Override
    	public double Sum(int x, int y) {
    		int sum = x+y;
    		double taxation = sum*tax;
    		return sum-taxation;
    	}
    
    }
    Abstract:

    Java Code:
    public abstract class Calculator {
    
    	public int sum(int x, int y){
    		return x+y;
    	}
    	
    }
    Java Code:
    public class CalculatorWithTax extends Calculator {
    
    	private final double tax = 0.03;
    	
    	
    	public double sumWithTax(int x,int y){
    		double sum = sum(x,y);
    		double taxation = sum*tax;
    		return sum-taxation;
    	}
    }

  8. #8
    Mephisto Pheles is offline Time, stop!
    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Default Re: interface and abstract class

    An abstract class is used when you want to have other classes extend into it, but you do not want it to be called on its self. Let me explain more with code posted.


    Java Code:
    public abstract class Calculator {
     
        public int sum(int x, int y){
            return x+y;
        }
         
    }
    This class is abstract, meaning that while you can not directly call on it, if another class were to extend into it, it would be able to use its variables/etc.

    Java Code:
    public class CalculatorWithTax extends Calculator {
     
        private final double tax = 0.03;
         
         
        public double sumWithTax(int x,int y){
            double sum = sum(x,y);
            double taxation = sum*tax;
            return sum-taxation;
        }
    }
    This class extends Calculator , meaning it can use the things whiten calculator, as we see here: double sum = sum(x,y);.



    So let me quickly surmise this: abstract classes are used when you want to create a superclass that will have multiple classes extend into it, but you do not want that class directly used.

Similar Threads

  1. Why Interface when we have Abstract Class
    By b4viral in forum New To Java
    Replies: 4
    Last Post: 07-04-2012, 09:42 AM
  2. When to use Abstract class and Interface?
    By vivekjan17 in forum New To Java
    Replies: 1
    Last Post: 03-05-2012, 10:37 AM
  3. Abstract Class & interface
    By suresh.sa in forum New To Java
    Replies: 1
    Last Post: 10-18-2010, 08:25 AM
  4. Replies: 6
    Last Post: 02-11-2009, 10:51 AM
  5. Interface and Abstract Class
    By kian_hong2000 in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 02:22 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
  •