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
This article has some good concepts (and examples) regarding the title of your question.
Abstract Classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract Classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.
Suppose that you want to define a class with certain methods, but for whatever design purpose of your program/project, you want to make sure that an specific method (Abstract Method) must be implemented when the Abstract Class is extended.
Basically, the purpose of Abstract Classes is to define a Class with methods that:
Additional Comment
@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).