What Is an Abstract Class

What is an abstract class?

  1. most commonly to serve as a base-class or interface (some languages have a separate interface construct, some don't) - it doesn't know the implementation (that is to be provided by the subclasses / implementing classes)
  2. abstraction and re-use
  3. when the base-class can provide no meaningful default-implementation for a method (but allowing subclasses to re-use the non-abstract parts of the implementation; any fields, non-abstract methods, etc)

For example:

public abstract class Stream { /* lots of code, some abstract methods */ }

What the heck is a stream by itself? What kind of stream? a stream to a file? a network? a memory buffer? Each may have different and unrelated ways of reading / writing, but provide a common API. It makes no sense to create just a Stream, but via the abstract class, you can code to the Stream API without knowing the details:

Stream s = CreateStream(...); // I don't *care* what kind of stream
s.Write(new byte[] {1,2,3,4,5});
s.Close();

When and Why to use abstract classes/methods?

I know the basic use of abstract classes
is to create templates for future
classes. But are there any more uses
of them?

Not only can you define a template for children, but Abstract Classes offer the added benefit of letting you define the functionality that your child classes can utilize later.

You could not provide a default method implementation in an Interface prior to Java 8.

When should you prefer them over
interfaces and when not?

Abstract Classes are a good fit if you want to provide implementation details to your children but don't want to allow an instance of your class to be directly instantiated (which allows you to partially define a class).

If you want to simply define a contract for Objects to follow, then use an Interface.

Also when are abstract methods useful?

Abstract methods are useful in the same way that defining methods in an interface is useful. It's a way for the designer of the Abstract class to say "any child of mine MUST implement this method".

What is the point of an abstract class?

I'm not sure you understand what an abstract class is, as none of the classes in your example are abstract, and nothing in there is an interface either. What you are doing is extending an instantiable class. Without the abstract keyword there is nothing to stop me doing:

SuperObject obj = new SuperObject();

I think a better example would be to illustrate how abstract classes are used. What they are commonly used to do is to provide a common method implementation. If a number of classes implement some interface, but all of them implement the same method in the same way using the same code, then what is commonly done is to create an abstract class that contains the common implementation, and get all of the concrete implementations to extend that class. This facilitates code reuse, and decreases the likelihood that one developer will change the common method implementation for one class, but forget the others. For example..

public class ObjectOne extends Thing {
public String objectString()
{
return "objectString() of class ObjectOne\n" ;
}
}

public class ObjectTwo extends Thing {
public String objectString()
{
return "objectString() of class ObjectTwo\n" ;
}
}

public class ObjectThree extends Thing {
public String objectString()
{
return "objectString() of class ObjectThree\n" ;
}
}

public abstract class Thing implements SuperObject {
public String alwaysTheSame() {
return "The same thing";
}
}

public interface SuperObject {
public String objectString();

public String alwaysTheSame();
}

import static java.lang.System.out ;

public class ControlClass {

public static void main(String[] args)
{
SuperObject [] arr = {new ObjectOne(), new ObjectTwo(), new ObjectThree()} ;

for(SuperObject elem : arr)
{
out.println(elem.alwaysTheSame());
out.println(elem.objectString()) ;
}
}
}

What we have done here is introduce an abstract class Thing, which provides a method implementation that is common to all 3 implementations of SuperObject (which is now an interface). This means we don't have to write the same code again in three different places in order to to fully implement the SuperObject interface in each one of our concrete classes.

In addition to this, you can also extend non final classes. You may wish to do this in order to override the default behaviour of one or methods on the concrete class, or to decorate the the class with additional methods. Of course, when you are designing a class hierarchy from scratch you don't stick concrete classes in it that then get extended by other classes, as it's generally considered a bad code smell. However, few of us work with totally new written-from-scratch codebases, and must adapt an exiting codebase to new requirements. Extending a concrete class is one tool in the toolbox to do this.

EDIT: Misunderstood what the OP was asking, but the last paragraph above is relevant.

What are abstract classes and abstract methods?

An abstract class is a class that can't be instantiated. It's only purpose is for other classes to extend.

Abstract methods are methods in the abstract class (have to be declared abstract) which means the extending concrete class must override them as they have no body.

The main purpose of an abstract class is if you have common code to use in sub classes but the abstract class should not have instances of its own.

You can read more about it here: Abstract Methods and Classes

What is an abstract class in PHP?

An abstract class is a class that contains at least one abstract method, which is a method without any actual code in it, just the name and the parameters, and that has been marked as "abstract".

The purpose of this is to provide a kind of template to inherit from and to force the inheriting class to implement the abstract methods.

An abstract class thus is something between a regular class and a pure interface. Also interfaces are a special case of abstract classes where ALL methods are abstract.

See this section of the PHP manual for further reference.

Abstract class and methods, Why?

We do this to say "This class has a complete (ish) API, but it does not have a complete implementation." Among other things, it means that you can do this:

public void doSomething(Dims d) {
System.out.println("The area is " + d.area());
}

// ...using it somewhere:

doSomething(new Triangle(4.0, 6.0));

In doSomething, even though the reference we have to the object is a Dims, not a Triangle, we can call area (and we end up calling Triangle#area) because it's defined in the Dims API (sometimes called a "contract"). It's just that Dims defers the implementation to subclasses. Which is why you can't create instances of abstract classes. The doSomething method doesn't have any idea whether what it was given is a Triangle or Rectangle, just that it's some kind of Dims.

Whereas if Dims didn't define area, doSomething wouldn't compile. We'd have to declare a doSomething accepting a Triangle and another one accepting a Rectangle and so on. We couldn't get the benefit of being able to operate on all Dims things in common code.

There's a lot of crossover between interfaces and abstract classes in Java. Fundamentally, you can think of an abstract class as an interface with a partial implementation (with the caveat that a class can implement more than one interface, but can only derive from a single abstract class). The line's gotten even blurrier now that interfaces can define "default" implementations of methods. (Some argue that now that interfaces can have default methods, they're the "new" abstract classes and make true abstract classes obsolete as a language feature, but there are still arguments, mostly syntactic, around using abstract classes sometimes.)



Related Topics



Leave a reply



Submit