Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the purpose of Abstract Classes in Java

Suppose I have two Classes, A and B. The A class is defined as abstract, while B extends this abstract class, and finally i test the result and both classes are part of same package.

public abstract class A {

    protected abstract void method1(); 

    protected void method2() { 
        System.out.println("This is Class A's method"); 
    } 
}

public class B extends A {

    @Override
    protected void method1() {
        System.out.println("This is B's implementaiton of A's method");
    } 
}  

and now when i test them:

B b = new B();
b.method1();
b.method2();  

I get expected output:

This is B's implementaiton of A's method
This is Class A's method

QUESTIONS:

  • What is the purpose of @Override keyword, because if I omit it, it still works the same.
  • If I don't implement the abstract method, I get a compilation error. So what is the difference from implementing an interface?
  • Also, I can implement method2() in B as well. Then the output changes to what is use in B. Isn't this also overriding the parent class method? Then what is the purpose of explicitly defining a method as abstract in Class A?
like image 566
911TurboS Avatar asked Feb 08 '12 16:02

911TurboS


People also ask

What is the main purpose of abstract class in java?

Java Abstract class can implement interfaces without even providing the implementation of interface methods. Java Abstract class is used to provide common method implementation to all the subclasses or to provide default implementation. We can run abstract class in java like any other class if it has main() method.

What is the purpose of abstract class abstract class?

An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.

What is the importance of abstract classes in programming?

The main benefit of using an Abstraction in Programming is that it allows you to group several related classes as siblings. Abstraction in Object Oriented Programming helps to reduce the complexity of the design and implementation process of software.


2 Answers

@Override

@Override was introduced in Java 5 (and extended a little bit in Java 6). It's only informative. It says "I'm suppose to override something that already exist in parent class or interface.

IDE's like Eclipse can warn you in case there's no such parent method (by example if you mispell the name). In that case your method will not be invoked (because of the mispelling).

But don't worry too much about it.

Abstract class vs interface

An abstract class allows you define a basic functionality leaving undefined parts. An interface doesn't allow you to implement anything. You can program everything except the part that really changes in each case. So when you need it, you inherit and implement the missing part.

Override two methods

Yes. In Java you can override all methods not explicity declared as final in parent class. It's ok. If you want to make it unmodifiable you can declare it final. By example, if you want to declare an ordering you could:

public abstract class Ordering<X>
{
abstract boolean isLower(X a, X b);
abstract boolean isEquals(X a, X b);
   final boolean isGreater(X a, X b) {
      return !isLower(a, b) && !isEquals(a, b);
   }
}

Of course it may have sense to override isGreater to implement it another more efficient way (imagine it's costly to compare). But there are scenarios when you want to provide basic already implemented functionality (and then's when abstract classes are better than interfaces) or when you want to force some implementation (then's when final keyword show useful).

like image 50
helios Avatar answered Sep 21 '22 06:09

helios


  1. @Override is not a keyword, it is an optional annotation that helps the compiler check that you indeed are overriding a method. If you say @Override but there is no method to override, the compiler will tell you that you've probably made a typo. Rename method1 to method12 to see the effect.
  2. Interface cannot have any implementations, while abstract class may optionally provide implementations for some of its methods. In addition, interfaces cannot have data members.
  3. Defining a method as abstract means that the derived class must provide an implementation. Not declaring it abstract says that derived classes simply can provide their own implementation, but they do not need to.
like image 39
Sergey Kalinichenko Avatar answered Sep 20 '22 06:09

Sergey Kalinichenko