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 2 of 2

Thread: Abstract class

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Abstract class

    Well part of one of my assignments is to write an abstract class. I know an abstract class is used for inheritance and cannot be instantiated. I'm aware that all of the real coding is done in the subclasses. I just can't get a grip on what's suppose to be in the abstract class other than abstract methods. This is what I have so far

    public abstract class DataItem {
     
    	//abstract,method will determine if two objects contain the same data
    	//Pre-condition: Object contains data is the object otherItem
    	//Post-condition: Returns true 
    	public abstract boolean equals(DataItem otherItem);
     
    	//Method will compare two objects.
    	//Pre-condition: Returns a value < 0 if this element is less than otherItem
    	//Post-condition: Returns 0 if this element is the same as other item. 
    	//returns a value > 0 if this element is greater than otherItem.
    	public abstract int compareTo(DataItem otheritem);
     
     
    	//Method to return a copy of this object.
    	//Post-condition: A copy of this object is created and reference of the copy is returned.
    	public abstract DataItem getCopy();	
     
    }// End abstract class DataItem.


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

    Default Re: Abstract class

    An abstract class is simply a class that can't be instantiated. It can have anything a normal class can have (fields, non-abstract methods, etc.), as well as can have abstract methods. Most often this is done to provide a class which has some implementation of a class, but not all of it.

    ex.: A car class could implement that you steer by turning the wheel, but how it turns would be implemented by inheriting classes.

    If you want a "class" with only abstract members (aka purely abstract class), you could create an abstract class, or you can create an interface. The interface is an abstract class which has and only abstract methods (static methods/fields are ok, though).

Similar Threads

  1. Replies: 2
    Last Post: February 28th, 2011, 10:51 AM
  2. Replies: 12
    Last Post: January 1st, 2011, 03:47 AM
  3. abstract class
    By robinglow in forum Java Theory & Questions
    Replies: 2
    Last Post: August 20th, 2010, 02:36 AM
  4. abstract class
    By robinglow in forum AWT / Java Swing
    Replies: 2
    Last Post: August 20th, 2010, 02:36 AM
  5. [SOLVED] Abstract Classes Help
    By SweetyStacey in forum Object Oriented Programming
    Replies: 10
    Last Post: May 6th, 2010, 07:15 AM

Tags for this Thread