[java] What are abstract classes and abstract methods?

Possible Duplicate:
Abstract class in Java

I got several explanations but so far I'm not able to understand that what are the abstract classes and methods in Java.

Some said it has to do something with the security of the program, other said it isn't anything like that.

Even from the Dietel & Dietel's book I don't get it's purpose. When,where and why do we use it?

Kindly explain it like you're teaching a beginner, I would really appreciate your help.

This question is related to java oop

The answer is


- Abstract class is one which can't be instantiated, i.e. its object cannot be created.

- Abstract method are method's declaration without its definition.

- A Non-abstract class can only have Non-abstract methods.

- An Abstract class can have both the Non-abstract as well as Abstract methods.

- If the Class has an Abstract method then the class must also be Abstract.

- An Abstract method must be implemented by the very first Non-Abstract sub-class.

- Abstract class in Design patterns are used to encapsulate the behaviors that keeps changing.


ABSTRACT CLASSES AND ABSTARCT METHODS FULL DESCRIPTION GO THROUGH IT

abstract method do not have body.A well defined method can't be declared abstract.

A class which has abstract method must be declared as abstract.

Abstract class can't be instantiated.


Once you get what abstract means in Java, you would ask: why they put this in ? Java may work without abstract stuff, BUT it makes part of a certain OO style or vocabulary. There exists really situations where an abstract class or method is an elegant way to express the program authors intention. Most when you are programming a framework or a library that will be used by others.


With abstract classes you can have some kind of skeleton for other classes to extend.

You can't instantiate them but you can put some common implementation which you can use in the classes that extend them.


abstract method do not have body.A well defined method can't be declared abstract.

A class which has abstract method must be declared as abstract.

Abstract class can't be instantiated.


An abstract method is a method signature declaration with no body. For instance:

public abstract class Shape {
    . . .

    public abstract double getArea();
    public abstract double getPerimeter();
}

The methods getArea() and getPerimeter() are abstract. Because the Shape class has an abstract method, it must be declared abstract as well. A class may also be declared abstract without any abstract methods. When a class is abstract, an instance of it cannot be created; one can only create instances of (concrete) subclasses. A concrete class is a class that is not declared abstract (and therefore has no abstract methods and implements all inherited abstract methods). For instance:

public class Circle extends Shape {
    public double radius;
    . . .

    public double getArea() {
        return Math.PI * radius * radius;
    }

    public double getPerimeter() {
        return 2.0 * Math.PI * radius;
    }
}

There are many reasons to do this. One would be to write a method that would be the same for all shapes but that depends on shape-specific behavior that is unknown at the Shape level. For instance, one could write the method:

public abstract class Shape {
    . . .

    public void printArea(PrintStream out) {
        out.println("The area is " + getArea());
    }
}

Admittedly, this is a contrived example, but it shows the basic idea: define concrete behavior in terms of unspecified behavior.

Another reason for having an abstract class is so you can partially implement an interface. All methods declared in an interface are inherited as abstract methods by any class that implements the interface. Sometimes you want to provide a partial implementation of an interface in a class and leave the details to subclasses; the partial implementation must be declared abstract.


An abstract class is a class that you can't create an object from, so it is mostly used for inheriting from.(I am not sure if you can have static methods in it)

An abstract method is a method that the child class must override, it does not have a body, is marked abstract and only abstract classes can have those methods.