What is Abstract class

Hello what is Abstract class

An abstract class is a class that can have one or more abstract methods in it.
An abstract method is a method that does not have a body. Only the signature is declared, leaving it up to a subclass to implement the method.
An often used example of this would be the abstract class shape, representing a two dimensional shape (for instance a circle or a square). This class has an abstract method getArea(), which gets you the size of the surface.
The implementation of this method would be something like returning Math.PI * radius * radius for the circle, while returning side * side for the square.
So while you can't do something like Shape s = new Shape(), you can do Shape s = new Square() and int x = s.getArea() after that.

Similar Messages

  • What are abstract classes/methods and what are they for?

    Hi,
    I've just heard about abstract classes and methods and I'm just wondering what exactly they're used for, and why are they there for the Graphics class for example?
    Cheers.

    raggy wrote:
    bastones_ wrote:
    Hi,
    I've just heard about abstract classes and methods and I'm just wondering what exactly they're used for, and why are they there for the Graphics class for example?
    Cheers.Hey bro, I'll try to solve your problemYou have to know two important concepts for this part. 1 is Abstract classes and the other is Interface classes. Depends on the nature of the project, you need to set certain level of standards and rules that the other developers must follow. This is where Abstract classes and Interface classes come into picture.
    Abstract classes are usually used on small time projects, where it can have code implementation like general classes and also declare Abstract methods (empty methods that require implementation from the sub-classes).Wrong, they are used equally among big and small projects alike.
    Here are the rules of an Abstract class and method:
    1. Abstract classes cannot be instantiatedRight.
    2. Abstract class can extend an abstract class and implement several interface classesRight, but the same is true for non-abstract classes, so nothing special here.
    3. Abstract class cannot extend a general class or an interfaceWrong. Abstract classes can extend non-abstract ones. Best example: Object is non-abstract. How would you write an abstract class that doesn't extend Object (directly or indirectly)?
    4. If a class contains Abstract method, the class has to be declared Abstract classRight.
    5. An Abstract class may or may not contain an Abstract methodRight, and an important point to realize. A class need not have abstract methods to be an abstract class, although usually it will.
    6. Abstract method should not have any code implementations, the sub-classes must override it (sub-class must give the code implementations). An abstract method must not have any implementation code code. It's more than a suggestion.
    7. If a sub-class of an Abstract class does not override the Abstract methods of its super-class, than the sub-class should be declared Abstract also.This follows from point 4.
    9. Abstract classes can only be declared with public and default access modifiers.That's the same for abstract and non-abstract classes.

  • What is the advantage of abstract class and method???

    hi,
    * Why a class is declared as abstract???
    * What is the use of declaring a class as abstract???
    * At what situation abstract class will be used???Thanks
    JavaImran

    To save you from the wrath of the Java experts on this forum, allow me as a relatively new Java user to advise you: do NOT post homework problems here; you're just going to get told to go google the answer. Which would be a good move on your part. Especially since I found the answer to your questions by googling them myself.

  • Why use an Abstract Class ?

    I am new to Java and for some reason I can't get my head around why to use an abstract class. I understand that an abstract class is something like:
    public abstract class Food{ // abstract class
    public void eat(){
    // stub
    public class Apple extends Food{
    public void eat(){
    // Eat an apple code
    }So basically the idea above is that you can eat an "apple" but you can't eat "food" because you can't instantiate an abstract class.
    I understand what an abstract class is and how to write one. What I don't understand is why you would use it? It looks to me like I could have just created a normal class called "Food" and just not instantiated it. What are the benefits of using an abstract class?

    807479 wrote:
    I am new to Java and for some reason I can't get my head around why to use an abstract class.One of the first books I ever read about Object-Oriented design contained the following quote from [url http://en.wikipedia.org/wiki/Lucius_Cary,_2nd_Viscount_Falkland]Lord Falkland:
    "When it is not necessary to make a decision, it is necessary +not+ to make a decision."
    It took me quite a while to understand, but it's all about flexibility: As soon as you cast something in stone, you lose the ability to change it later on if something better/more appropriate comes along. Interfaces and abstract classes are all about delaying that decision.
    As jverd said, interfaces allow you to specify what is required without defining the how; and as ErasP said, abstract classes are usually incomplete: ie, they define some of the 'how', but not all of it.
    What is most important about abstract classes though is that they cannot exist on their own: They must be extended by a concrete class that completes the 'how' before they can be instantiated and, as such, they declare the intent of the designer.
    One of the most important uses of abstract classes is as "skeleton implementations" of interfaces, and there are a lot of examples of these in the Java Collections hierarchy. My favourite is probably AbstractList, which contains a skeleton implementation of a List. Because it exists, I can create a class that wraps an array as a List with very little code, viz:public final class ArrayAsList<T>()
       extends AbstractList<T>
       private final T[] values;
       public ArrayAsList(T... values) {
          this.values = values;
       @Override
       public T get(int index) {
          return values[index];
       @Override
       public T set(int index, T element) {
          T value = get(index);
          values[index] = element;
          return value;
       @Override
       public int size() {
          return values.length;
    };and somewhere else, I can use it:   List<String> letters =
          new ArrayAsList<String>("a", "b", "c");or perhaps, more practically:   List<String> words = new ArrayAsList<String>(
          bigTextString.split(" +") );Now that may not seem like a big deal to you, but given all that Lists can do, it's actually a very powerful bit of code. The above example is from "Effective Java" (p.95).
    HIH
    Winston

  • Having trouble understanding Abstract class. Help!!!!!!

    Having trouble understanding Abstract class. when is Abstract class used and for what.

    Having trouble understanding Abstract class. when is
    Abstract class used and for what.An abstract class is used to force the developer to provide a subclass, to implement the abstract methods, while still keeping the methods that were provided.
    � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • What is the diff b/w Abstract class and an interface ?

    Hey
    I am always confused as with this issue : diff b/w Abstract class and an interface ?
    Which is more powerful in what situation.
    Regards
    Vinay

    Hi, Don't worry I am teach you
    Abstract class and Interface
    An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.
    Edited by SASIKUMARA
    SIT INNOVATIONS- Chennai
    Message was edited by:
    sasikumara
    Message was edited by:
    sasikumara

  • What is the point of an abstract class?

    ok lets say there was a subclass that inhereited another subclass...like a class Person, a class Student, and a class GraduateStudent; why would i want to make the class person abstract and have a abstract void display(); in it if i want to override that method in the subclasses lower on the inheritance tree? Wouldn't it do the same thing to just make a normal class Person and just not have a void display...and the other 2 over ride it anyways so i seriously dont see what the point of abstract classes are

    ok lets say there was a subclass that inhereited
    another subclass...like a class Person, a class
    Student, and a class GraduateStudent; why would i
    want to make the class person abstract and have a
    abstract void display(); Because all Persons are required to be able to display(), but there's no reasonable common default behavior for display() that you can put in the base class.
    Abstract methods say, "Everything of this type must be able to do this, but it's up to the individual implementations to figure out *how* to do it."
    in it if i want to override
    that method in the subclasses lower on the
    inheritance tree? Wouldn't it do the same thing to
    just make a normal class Person and just not have a
    void display..Then you couldn't do this: Person p = getAPersonThatMightBeAStudentOrWhateverWeDoNotKnowWhat();
    p.display();or this:
    void doPersonStuff(Person p) {
      p.display();
    Person s1 = new Student();
    Student s2 = new Student();
    Person t1 = new Teacher();
    Teacher t2 = new Teacher();
    doPersonStuff(s1);
    doPersonStuff(s2);
    doPersonStuff(t1);
    doPersonStuf(t2);
    .and the other 2 over ride it anyways
    so i seriously dont see what the point of abstract
    classes areI hope you do now.
    Message was edited by:
    jverd

  • What is an abstract class?

    What is an abstract class and what does it do compare to other classes?
    I'm having some trouble trying to understand it.
    Message was edited by:
    xc100

    s it not obvious that the OP is a worthless troll?Looking at his/her recent posts, I wouldn't say so.
    S/he looks like a newbie who is asking basic
    questions
    and is rather reluctant to follow links or read
    tutorial pages,
    but I haven't seen actual troll behaviour.My first impression was that this was another Charles_Bronson, due to the countless threads that ask basic, open-ended questions.
    Then again, CB never posted any actual code, so who knows?
    One can never know really and it's unfair to judge someone unheard. That's >why I usually start by giving a very brief reply to invite to a dialog.
    And it's not just about replying to the OP. Sometimes a discussion of >general interest develops even from a seemingly trivial question.I see your point. However, the discussions that seem to stem from these types of threads seem to be irrelevant to Java, if you know what I mean.

  • What is the benefit of abstract classes?

    I am little bit confuse regarding the benefits of the abstract classes. Can anybody helps me in this regard. I just want to know when and what situation we may decide that particular class needs an abstract method.
    Thanx

    I am little bit confuse regarding the benefits of the
    abstract classes. Can anybody helps me in this
    regard. I just want to know when and what situation
    we may decide that particular class needs an abstract
    method.Sorry if I sound weary of this question, but it is asked about 3-4 times a week. Rather than have us go through this tired exercise again, please search the forum for previous threads on this subject. You'll find about a gazillion. In fact, it's often a good idea to search the forum before asking a question as very often it has been asked and answered before.

  • What happens to abstract classes now?

    As we can write default implementations to the methods in a interface, what happens to abstract classes now?

    just because you can doesn't mean you should. And this is a clear case of you should not.
    Allowing implementation in interfaces is a dirty hack they needed to put in to be able to bolt on some of the other stuff without breaking forward compatibility with existing code.
    That doesn't make it a good idea, in fact it's a pretty good indication that the stuff they hacked in this way is itself not a good idea.

  • What's the difference between Abstract Class and Interface?

    Dear all,
    Can anyone give me some hints about this topic?
    Thanks.
    Leo

    an abstract class may have some methods already implemented in the abstract class but an interface has no methods implemented
    I think it's just that simple.
    For your design needs, you just choose what you need : )
    Cheers
    Stephen

  • What is the difference between Abstract class and Interface ?

    Hi,
    Could u plz tell me the difference between Abstract class and Interface?
    Thanks in advance.
    Gopi

    Lots.
    An abstract class can contain some method implementations, or indeed all the method implementations. It may contain methods with all the various access modifiers. It cannot be instantiated. A class may inherit from only a single abstract class.
    An interface contains only public method stubs and constants. A class may implement multiple interfaces. An interface cannot (obviously) be instantiated.
    Abstract classes are particularly useful when you need to provide a semi-complete implementation for reuse. Interfaces are used more like types.
    Look at java.util.* for some good examples of the use of both.

  • Abstract method called in an abstract class

    Hello,
    I am writing some code that I'd like to be as generic as possible.
    I created an abstract class called Chromozome. This abstract class has a protected abstract method called initialize().
    I also created an abstract class called Algorithm which contains a protected ArrayList<Chromozome>.
    I would like to create a non abstract method (called initializePopulation()) which would create instances of Chromozome, call their method initialize() and full the ArrayList with them.
    In a practical matter, only subclass of Algorithm will be used, using an ArrayList of a subclass of Chromozome implementing their own version of initialize.
    I have been thinking of that and concluded it was impossible to do. But I'd like to ask more talented peaple before forgetting it !
    Thanks,
    Vincent

    Ok, let's it is not impossible, juste that I had no idea of how doing it :-)
    The difficulty is that Algorithm will never have to deal with Chromozome itself, but always with subclass of Chromozome. This is usually not an issue, but in that case, Algorithm is required to create instances of the desired subclass of Chromozome, but without knowing in advance wich subclass will be used (I hope what I say makes any sense).
    Actually I may have found a way in the meantime, but maybe not the best one.
    I created in Algorithm an abstract method :
    protected abstract Chromozome createChromozome()The method initializePopulation will call createChromozome instead of calling directly the constructor and the initialize() method of Chromozome.
    Then subclass of Algorithm will implement the method createChromozome using the desired subclass of Chromozome.

  • Casting & abstract class & final method

    what is casting abstract class & final method  in ABAP Objects  give   some scenario where  actually  use these.

    Hi Sri,
    I'm not sure we can be any more clear.
    An Abstract class can not be instantiated. It can only be used as the superclass for it's subclasses. In other words it <b>can only be inherited</b>.
    A Final class cannot be the superclass for a subclass. In other words <b>it cannot be inherited.</b>
    I recommend the book <a href="http://www.sappress.com/product.cfm?account=&product=H1934">ABAP Objects: ABAP Programming in SAP NetWeaver</a>
    Cheers
    Graham

  • "Abstract" method in a non-abstract class

    Hi all.
    I have a class "SuperClass" from which other class are extended...
    I'd like to "force" some methods (method1(), method2, ...) to be implemented in the inherited classes.
    I know I can accomplish this just implementing the superclass method body in order to throw an exception when it's directly called:
    void method1(){
    throw new UnsupportedOperationException();
    }...but I was wondering if there's another (better) way...
    It's like I would like to declare some abstract methods in a non-abstract class...
    Any ideas?

    The superclass just models the information held by
    the subclasses.
    The information is taken from the database, by
    accessing the proper table (one for each subclass).??
    What do you mean by "models the information"?
    You should use inheritance (of implementation) only when the class satisfies the following criteria:
    1) "Is a special kind of," not "is a role played by a";
    2) Never needs to transmute to be an object in some other class;
    3) Extends rather than overrides or nullifies superclass;
    4) Does not subclass what is merely a utility class (useful functionality you'd like to reuse); and
    5) Within PD: expresses special kinds of roles, transactions, or things.
    Why are you trying to force these mystery methodsfrom the superclass?
    It's not mandatory for me to do it... I 'd see it
    just like a further way to check that the subclasses
    implements these methods, as they have to do.That's not a good idea. If the superclass has no relation to the database, it shouldn't contain methods (abstract or otherwise) related to database transactions.
    The subclasses are the classes that handle db
    transaction.
    They are designed as a binding to a db table.And how is the superclass designed to handle db transactions? My guess (based on your description) is that it isn't. That should tell you right away that the subclasses should not extend your superclass.

Maybe you are looking for

  • My apple tv is not working on my panasonic projector even when I changed the resolution

    My apple tv is not working on my Panasonic projector even when I have changed the resolution to all of the different settings

  • Firefox Distribution

    Hiya all- I'm working on a test environment where the user logs in to NDS and is then auto-logged into Win2K as "user" and not administrator. I'm hoping to move to this sort of environment in an effort to cut down on Ad/Spyware, and to keep rogue app

  • Tally up a column only on rows that have been checked

    Hi I'm driving myself to drink with this, and I'm sure it should be pretty easy. I have a subtotal column (Column D) which I calculate the total of via (SUM(D2:D20), however I have a column G, that is a "Paid" column, that has a checkbox, now what I

  • Com.sap.portal.htmlb in SSL

    Hello, We have implemented SSL on the portal platform (on the J2EE) and it seems to be working for the most part.  We are using it with Kerberos authentication.  We have noted that there are some issues and they mostly revolve around the com.sap.port

  • Online chat, Nov. 12, on Regular Expressions

    One of the new packages added in J2SE v 1.4 is java.util.regex , which provides classes for handling regular exprssions. A regular expression is a string pattern that can be used to perform sophisticated string searching and replacement. Learn more a