Do Abstract classes need to implement an entire interface in java 7? - java

I need a group of different classes to implement a certain interface. However, a lot of those classes, but not all of them, need a same implementation for some of the methodes defined in the interface. I was wondering if I could make an abstract class implement the interface and only create the methods that are the same for those classes?
For example, I have interface A:
public interface A{
public returnType method1(){};
public returnType method2(){};
}
Can I do this:
public abstract class AbstractPartialA implements A{
#Override
public returnType method1(){
implementation
}
}
and than have the classes extending from this Abstract class implement the remaining methods that are required to fulfill the interface?

Yes, you can, that is the exact purpose of abstract classes.

Do Abstract classes need to implement an entire interface in java 7?
And the answer is " NO ". Abstract class can implement entire interface or it can implement only some methods of an interface.
Case-1
If it implements entire interface and is still declared as 'abstract', it means we don't want other(people who are going to use our class) to create object for our class
Example of such class is HttpServlet in javax.servlet.http . Here HttpServlet class doesn't have any abstract method, but still it is declared as 'abstract'
Case-2
Simple, if the class doesn't implement any one of the method of an interface, then it is declared as 'abstract'. Now it will be the responsibility of the other class which extends the abstract class to provide the implementation of such method which is not implemented by 'abstract class'

You can, and when you try to extend from AbstractPartialA Java will ask you to:
Implement all methods declared in implemented interfaces and not implemented
Implement all methods declared as abstract in superclasses
Remember that a class is considered to implement all interfaces implemented by its superclasses, not just the ones specifically written after the implements keyword in this class' declaration. This applies both to the types of the class (and thus the types of its references) and to the methods it is required to implement.

Related

If I extend an abstract class, that is implementing an interface, do I need to rewrite the methods?

I have an interface with several methods. I then have an abstract class that defines some of those methods, but also leaves some of the methods abstract. Now I have a class that extends the abstract class. Do I need to write all the methods from the original interface and define them, or does java know that some of them have been implemented by the abstract class?
At the point that you write a concrete (non abstract) class that implements an interface and/or is derived from an abstract base class, that concrete class must provide an implementation for every method it is specified as having. Specified means either by being declared abstract in a base class, or being specified by an interface that the class or one of its base classes declares it to implement.
If you write an abstract class that implements an interface and/or is derived from an abstract base class, your new class need not provide an implementation for any of the methods it is specified as having; those remaining methods are either explicitly or implicitly considered as being declared abstract by your new class.
Interfaces methods must be implemented. If the abstract class already implements some of them, you're free to override them in the subclasses or not.
In Interfaces, you declare some methods(), no actual implementation should be there.
And when you implement the interface into a abstract class, the declaration of those methods is present in that abstract class. It is up to you to decide whether to define the methods here or not.
when you extend these methods in concrete class(one which will be used to create objects), every methods needed to be defined and since if you already defined some methods in abstract class you don't need to do it again,but you can do it.

From where Function will be inherit and why?

Suppose we have one class implements Interface extends abstract class with same abstract function in both Interface and abstract class. Then class inherit which function Interface or abstract class and why.
Like:
public class A extends B implements I
{
public void set()
{
// Some code here
}
}
Interface:
public interface I {
public void set();
}
abstract Class:
public abstract class B
{
public abstract void set();
}
Both. As long as the functions signatures match, the compiler will accept this "double"-inheritance. Keep in mind implementing interface's methods is only a "contract" your class has to verify to be compilable. Implementing the interface only means "my concrete class has to have a method set()". Extending the abstract class B means "my concrete class inherits the method set() from its superclass, and as it's defined as abstract, it needs to implement it". When both these propositions match (as per your example), all is fine.
If there is a difference in the signature of the functions between the interface and the abstract class, your concrete class must then implement both versions.
BTW, slightly off-topic, try to avoid abstract classes as much as you could. If an abstract class has only abstract methods, then it should be an interface. If it has some code in some of its method, then you should probably think about refactoring it to use composition rather than inheritance. Inheritance is evil ;)
It doesn't matter to know from which the method will be inherited because the method in both Interface and Abstract class are abstract meaning no implementation is given.
So all you have to ensure in your concrete class that you must implement all the method defined in the interface but not implemented in the abstract class . AND implement all the abstract methods defined in its superclasses (even if they are not defined in an interface) (from Guillaume)

What is difference to extend abstract class and non abstract class?

What is difference between abstract class and non abstract class when extending derived classes? Both class I didn't use the method overriding and abstract methods (i.e. abstract class). Just I inherited the properties. What and why did prefer the class?
Ex:
Code 1:
abstract class a {
protected int empnno;
protected String empname;
}
class b extends a {
...
}
Code 2:
class a {
protected int empnno;
protected String empname;
}
class b extends a {
...
}
what is difference to extend abstract class and non abstract class?
Abstract classes may have abstract methods. Abstract methods are methods without implementations and these must be implemented by your subclass (unless you make your subclass abstract too).
Since your a class have no abstract methods, there is no difference what so ever from a subclass-perspective. (The only difference is that if a is abstract it may no longer be instantiated as is. It may only be instantiated in terms of subclasses.)
Suppose there is a class B, and class A, where A extends be. The following are the possible scenarios:
1. B is abstract
1.1. B doesn't have abstract methods
1.1.1. A is abstract
1.1.1.1. You don't want to instantiate A. Everything is fine.
1.1.1.2. You want to instantiate A. That's not possible, you can't create abstract objects
1.1.2. A is not abstract. Everything is fine
1.2. B has at least an abstract method
1.2.1. A is abstract
1.2.1.1. You don't want to instantiate A. Everything is fine.
1.2.1.2. You want to instantiate A. That's not possible, you can't create abstract objects
1.2.2. A is not abstract
1.2.2.1. A doesn't implement all the abstract methods. You can't run your project until you change this
1.2.2.2. A implements all the abstract methods. Everything is fine.
2. B is not abstract
2.1. A is abstract
2.1.1. You want to instantiate A. Error.
2.1.2. You don't want to instantiate A. No problem
2.2. A is not abstract. No problem.
To have a better understanding, we can compare Abstract Classes with Interfaces, the main differences are:
While abstract classes may contain fields/properties and concrete
methods, interfaces may contain only abstract methods (method
signatures).
One class can implement several interfaces, whereas it can extend just one class, abstract or not.
Therefore, when you create a subclass extended an abstract class, you need to implement the abstract method that was in the abstract class(if any), otherwise, the subclass would be still an abstract class -- which cannot be instantiated!!
Also, you can use interfaces instead of abstract classes if you only want to declare some methods signatures.
Actually they are the same, but you cannot instanciate Abstract classes. So if you want nobody tries to instanciate your class, you would like to make it Abstract.
An abstract class will contain abstract methods that do not have an implementation.
When you extend this class, sometimes you may decide only to provide implementations for some of those abstract methods. In this case you've extended an abstract class and yet the subclass is still abstract.
If you implement all the abstract methods, your subclass is typically not abstract (although there's nothing stopping you from declaring it as such, AFAIK).

abstract classes ,constructor and interface

I'm really confused, and I've read a TON of questions on this topic and I have not been able to pinpoint anything specifically that an interface can do that an abstract class cannot do.
A class can implement multiple interfaces, but it cannot implement multiple abstract classes.
Interface itself cannot do anything. It just defines kind of contract between the class(es) that provide some functionality and the caller.
Abstract class is the class that defined as abstract. If class has at least one abstract method (i.e. method without implementation) it must be defined as abstract. But abstract class can contain implementations as well.
Interface cannot contain implementation. Only abstract methods and constants (static final fields).
Class can implement several interfaces and extend only one class (including abstract class).
I hope this helps.
Abstract class can also contain function implementation rather than just defining the functions that have to be implemented by inheriting classes
Abstract classes are partially implemented classes, that will be extended by concrete classes(non-abstract), to be implemented.
Example:
This example does not mean that the sub classes must implement those methods(as it happens when implementing an interface). You can declare a subclass abstract, and the implementation will be done later by annother sub-sub-class. (For example: Boat can have subclasses "SpeedBoat" and FisshingBoat, and the may implement honk() in different ways)
The interface are the eyes of class to the outside world. What classes can do is declared in the interface, but implemented in the class.
A class can implement many interfaces, but can extend only one class.
See this little example of interfaces:
As you can see when we use interfaces we can have a lot of flexibility. Some Enemies can do things that some Heroes can do too(DarkKnight can throw arrows).
I hope you now feel the difference between the abstract classes and interfaces.
Remember this about interfaces and Abstract classes:
Interfaces dont have variables, just non implemented methods(abstract methods implicitly)
Classes that implement interfaces must have all the methods of the interface in its body
One class can extend only one class but implement more than one interface
If a class has an abstract method, it must bee declared as abstract.
Abstract classes can implement interfaces
Interfaces can extend other interfaces(more than one)
I dont know if i forget something, i hope this information helps.
An abstract class can everything that a Interface can do. However inverse of this is not correct.
Abstract class can contain abstract methods, abstract property as well as other members (just like normal class).
Interface can only contain abstract methods, properties but we don’t need to put abstract and public keyword. All the methods and properties defined in Interface are by default public and abstract.
We can see abstract class contains private members also we can put some methods with implementation also. But in case of interface only methods and properties allowed
Interface and abstract class almost both are same the major difference is using interface we are not able to define the body of the method but using abstract class we can define the body of the method inside the abstract class or while implementing it.
e.g
Interface abc()
{
string xyz();
}
abstract abc()
{
string xyz()
{
// define body
}
}
or
abstract abc()
{
string xyz();
}
An abstract class is a class - it defines all or part of an implementation of some behaviour for a class of objects, but with some extension points for concrete subclasses to provide.
An interface is a type - it defines the set of operations which are provided by any class implementing the interface.
You're almost asking whether there is anything that a candidate can do that the job description can't. Creating an abstract class says 'here is a template for some implementation'. Creating an interface says 'I expect an object to provide these capabilities'. You can use virtual methods in an abstract class to implement some aspects of a type, but the intention is different.

I've found many questions on abstract classes and interfaces but answer me this: Can an abstract class do everything an interface can?

I'm really confused, and I've read a TON of questions on this topic and I have not been able to pinpoint anything specifically that an interface can do that an abstract class cannot do.
Is there anything an interface can do that an abstract class cannot do?
I am asking in the context of my Java class, but feel free to remove the java tag if it applies to other languages as well (possibly C#?).
Edit: I understand that an abstract class can do things an interface cannot, but if an abstract class can do everything an interface can do, then what is the point of an interface? What does "implement multiple interfaces" mean?
Interfaces as such cannot do what abstract classes do.
This is because abstract classes can contain code - interfaces cannot. But any given class can only have one superclass - extends - as opposed to any number of interfaces - implements, so if you use abstract classes you essentially paint yourself in a corner of the inheritance tree, because your class can only extend a single class.
This limitation does not apply to interfaces, allowing a single class to have many purposes depending on how many interfaces it implements.
You can't inherit from multiple abstract classes in c#, but you can implement multiple interfaces.
I think this may apply to java too
Edit:
You can't inherit from multiple classes. If you have an abstract class called Clonable, and an abstract class called Disposable then you can only inherit one of these classes and you're forced to make a decision about which class your class should be a subtype of:
e.g:
public abstract class Clonable
{
public abstract void Clone();
}
public abstract class Disposable
{
public abstract void Dispose();
}
// MyClass cannot be Disposable too, it is not allowed by the language
public class MyClass : Clonable
{
override void Clone()
{
}
}
Note it is a design decision of the language to allow you only to inherit from one class.
If on the other hand you have interfaces, the language allows you to implement both
e.g.
public interface IClonable
{
void Clone();
}
public interface IDisposable
{
void Dispose();
}
public class MyClass : IClonable, IDisposable
{
void IClonable.Clone()
{
}
void IDisposable.Dispose()
{
}
}
Well read:
http://mindprod.com/jgloss/interface.html
http://mindprod.com/jgloss/interfacevsabstract.html
What does "implement multiple interfaces" mean?
Consider:
public static interface Ifun extends Comparable<Ifun>, Serializable {}//or
public static class Cfun2 implements Comparable<Cfun>, Serializable
When class implements Ifun, then:
Comparable interface imposes a total ordering on the objects of each class that implements it. Objects that implement this interface can be sorted automatically by Collections.sort.
The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.
It means object can have more then 1 interface.
Interfaces are non instantiable classes that only contains methods that subclasses can inherit. The only thing that interfaces can do (in java) is that a class can implement many interfaces while a class can only extend 1 abstract class.

Categories