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)
Related
I am told that: a class can extend a concrete superclass and override an implemented method to make it abstract.
If the method becomes abstract, then the corresponding superclass should become abstract too.
Is this correct? If so, is it meaningful to make the superclass or the method in superclass to become abstract?
To contain abstract methods the class itself must also be declared abstract.
Abstract methods can be used where behaviour is specific to each subclassing object. Although this can also be achieved through interfaces, you may prefer to use an abstract class as they can also contain already implemented methods, or you may wish to declare fields which are not static and final. Beware that when a class becomes abstract it can no longer be instantiated.
Read more about abstract classes here.
Whether or not you make your class abstract if it contains abstract methods is not a choice, it is a must. You cannot have a non-abstract class that has abstract methods.
Whether or not you should declare a method abstract or implement it in your super class comes down to your concrete use case. Is there a default behavior that makes sense for all possible concrete implementations of the super class? Then you might want to implement it. If that's not the case, declare it abstract and let the sub classes take over.
I am told that: a class can extend a concrete superclass and override an implemented method to make it abstract.
If the method becomes abstract, then the corresponding superclass should become abstract too.
Is this correct?
I think it's incorrect.
Take the following code for example.
public class Father {
public void print() {
System.out.println("Father");
}
}
public abstract class Son extends Father {
#Override
public abstract void print(); /* no need to abstract the superclass */
// and other abstract or concrete methods
}
public class Grandson extends Son {
#Override
public void print() {
System.out.println("Grandson");
}
}
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.
I have two methods in an interface:
public interface MyInterface {
public void methodOne();
public void methodTwo();
}
public class MyClass implements MyInterface{
public void methodOne(){
//implementation code
}
public void methodTwo(){
//implementation code
}
}
Can I restrict one of them while implementing the interface?
You must implement all methods of your interface, unless the class implementing the interface is abstract.
If by restrict you mean that you want to predefine one or more of the methods, then you can use an abstract class instead of the interface. Abstract methods in an abstract class are methods that must be implemented by any class that extends the abstract class. Non-abstract methods are actually implemented in the abstract class, itself.
For example,
public abstract class MyClass
{
abstract void methodOne();
void methodTwo()
{
//implementation code
}
}
public class MyOtherClass extends MyClass
{
void methodOne()
{
//implementation code
}
}
Here's a reference for Abstract Classes and Methods.
EDIT 1 (in response to comment):
I'm not really sure what you mean by a burden. All I'm saying is that if you want all methods to be implemented by the class, then use an interface.
If you only want some of the methods implemented by a class, then you can either use an abstract class instead of the interface
or
If it makes sense, have an abstract class implement the interface (partially) and then have the remainder of the methods implemented by whatever extends the abstract class.
Both approaches are reasonable. It depends on what you really need to do.
EDIT 2 (in response to additional comments):
Providing one user class with additional features seems like the perfect application for just extending the "normal user class" with a "super user" class that has the additional features. If you need an interface for the "super user" class, you can create an interface that extends the interface implemented by the "normal user" class.
No you have to implement all methods if your class implements an interface.
Unless it is abstract.
Check this discussion for more details.
You can use an abstract class; they do not need to implement all the entire interface. However, they cannot be instantiated. You must implement all methods in order to instantiate an implementation of an interface.
Except the abstract class, you are bound to implements all methods of interface.
You can not put restriction on Interface, but YES you can achieve In Abstract class and NO in concrete class.
Couple of solutions :
Either mark the class as abstract. But in that case you cannot instantiate your implementing class.
Simply add black implementation in your class.
Not sure what you mean by "restrict". You have to implement the method. However, you could simply do
public void method1(someargs) {
throw new UnsupportedOperationException();
}
as is done by many Collection implementations.
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.
I wonder what is the difference in Java between an abstract class and a class that has all its methods abstract? I mean, is an abstract class just a class whose methods automatically get abstract?
Absolutely not. Indeed, a class can be abstract without any methods being abstract, although that's relatively rare (see Mark's comment below for an example). On the other hand, if a class has any abstract methods, then it must be declared abstract.
Generally speaking, the purpose of an abstract class is to provide a skeleton with some non-abstract behaviour, but other bits still to be filled in by subclasses. This can be used with the template method pattern, for example.
Any class that contains one or more abstract methods must also be declared abstract. To declare a class abstract, you simply use the abstract keyword in front of the class keyword at the beginning of the class declaration. There can be no objects of an abstract class. That is, an abstract class cannot be directly instantiated with the new operator. Such objects would be useless, because an abstract class is not fully defined. Also, you cannot declare abstract constructors, or abstract static methods. Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or be itself declared abstract.
The Only difference between abstract class and interface is that abstract class can be inherited and whereas interfaces can't, thus interfaces don't have any constructors conversely with an abstract class.
Whenever you make a abstract method in the class then you explicitly mention the abstract keyword before the class name, just like this
public abstract class Test {
abstract void show();
}
Here are the points related Abstract class in Java
-->Abstract class is the one of class that cannot be instantiated.
-->If you want to get implementation of any method from child class(other person) then abstract method can use in this sense.
-->Abstract class are incomplete ,subclass must declare missing piece to become concrete class(Class whose object can be instantiated ) , otherwise these subclass also become abstract class.
-->You can achieve abstraction that is main pillar of OOP through by "abstract classes".
Abstraction hide the irrelevant detail of an object.
-->Abstract use for IS A Relationship (Inheritance).
-->Abstraction use for to achieve Polymorphic behavior (Another main pillar of OOP)
-->abstract class should not be private and not contained private method.
-->You extends single abstract class not multiple because Java is Single supported Inheritance
--> Abstract class must contain 1 or more than 1 abstract method
-->If any class contain abstract method then it should explicitly declare abstract class even if it contain concrete method.
--> Constructor and static method cannot be declared as abstract because constructor are not inherited.
--> If child class have not implement the abstract method of super class then it become also abstract class.
-->Attempting to instantiate the object of abstract class is an Compilation error.
-->Abstract super class variable can hold the reference of child concrete object.
A class which contains the abstract keyword in its declaration is known as abstract class.
Abstract classes may or may not contain abstract methods ie., methods with out body ( public void get(); )
But, if a class have at least one abstract method, then the class must be declared abstract.
If a class is declared abstract ,it cannot be instantiated.
To use an abstract class you have to inherit it from another class, provide implementations to the abstract methods in it.
If you inherit an abstract class you have to provide implementations to all the abstract methods in it.
If you don't want to provide implementation for all abstract methods then there is a concept of adapter class:
Example:
abstract class A{
public void m1();
public void m2();
public void m3();
}
class B extends A{
public void m1(){}
public void m2(){}
public void m3(){}
}
class C extends B{
public void m2(){
System.out.println("Hello m2");
}
public static void main(String args){
C obj=new C();
C.m2();
}
}