I have interface with two methods and abstract class, which implement this interface and override one method from it. Can I instance this abstract class, without overriding other method of interface? Or can I replace this method with method which have another signature?
UPD: Thanks for answers, I really make mistakes in my question. Can I use anonymus class which will be extend my abstract class, without overriding all methods from implements inrerface? How I understand by answer from DragonK, no, and I need create class, which will be extends abstract class and override others methods?
Can I instance this abstract class
You can never instantiate an abstract class.
You could implement any methods of the interface but you still won't be able to instantiate an abstract class.
Unless you are dealing with Java 8's Default Methods, your interface will have no implementations and thus no methods to override.
In Java, abstract classes cannot be instantiated either, unless you construct an anonymous class.
Can I instance this abstract class, without overriding other method of
interface?
You can't instantiate an abstract class
can I replace this method with method which have another signature?
You can replace this method with method which have another signature. But Classes extend your abstract class will override all abstract method.
You can't instantiate an abstract class.
or you can have Anonymous implementation and instantiate the abstract class.
while providing overriding abstract method we can can change the "covariant return types" but you cant change the method signature.
Related
When we use this new A() {};, if A is concrete and not abstract, do we have to include the methods of A? What if A was an interface? Do we have to include the methods of A? Why?
Whether a class is anonymous has nothing to do with whether you must include the methods of the extended class or of the implemented interface.
If A is concrete, then you don't have to override any methods. You can override them of course, and you should override at least one of them, or else having an anonymous subclass is useless; you could use A directly. You can supply your own new methods, although they won't be callable directly.
If A is abstract or an interface, then you must override all abstract methods, just as with any other named concrete class.
One should use the #Override annotation on all methods intended to override a superclass or implement an interface, to ensure that typos don't result in a method that doesn't override anything.
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.
Actually, i have attended the interview. They ask me,
There is a three functions in interface. overridden class only need to override only one function. how can you achieve this?
Anyone know how to do this?
To expand on the other answer, an abstract method that implements an interface can choose to implement only some of the methods of that interface, leaving the implementation of the rest of the methods to its concrete sub-classes.
In Java 8 you can do it even with concrete classes, as interfaces can have default implementations for some of their methods. Therefore, only the methods that don't have default implementations must be implemented by concrete classes that implement the interface.
You can use abstract class and implement some of the methods
You can declare the class implementing that interface as abstract.
But the class extending that abstract class has to implement all the methods declared in that interface that have not been overridden in the abstract class.
I have two questions really. I'm trying to get a handle on how inheritance works.
If I have an abstract class to inherit from, and it has a method that is not labelled abstract does this method still need to be implemented in the subclass?
If I have a subclass that is inheriting from another subclass, which is then inheriting from an abstract class, does the lowest subclass need to implement the methods in the abstract class? Or because the methods have been implemented in the middle subclass, they don't need to be implemented again?
Thank you!
An abstract class is a class that is declared abstract. It may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
If a class includes abstract methods, the class itself must be declared abstract, as in:
public abstract class GraphicObject {
// declare fields
// declare non-abstract methods
abstract void draw();
}
When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract
If the method is not abstract it has been implemented already, when you subclass the abstract class you inherit the method implementation, re-implementing it would be overriding it. If the method was declared abstract you must implement or get compile-time error if the subclass is also not declared abstract.
If you are inheriting from a class A extends AbstractClass that is not abstract then A must have implemented any abstract methods or again compileerror. If it hasn't implemented any abstract classes then A must also be abstract and responsibility of implementing the abstract methods fall on subclassers of A. Any sublcassers that do not implement the method must also be declared abstract untill finally a subclass implements it.
I wonder 3 things.
1: If I have implemented an interface (with a method) in a superclass where im declaring that method, and then I extend that superclass in another class. Then I don't have to redeclare that method right?
2: But if I don't declare that method in the superclass but in the child class then I instantiate the superclass. What happens then? It didn't contain any method from the instance.
3: Could you use implement in a class and then not declaring that method? Maybe it will be used as a superclass only for other classes to extend. And then just declare that method in the child classes or do you have to declare it in the current class you are implementing the interface?
Assuming you meant "reimplement" instead of "redeclare", that's correct.
Depends whether the class is declared abstract or not. If the superclass is abstract, then the child class either needs to implement it or to be declared abstract as well. If the superclass is not abstract, then it won't compile.
Declare the class abstract if you don't want to implement it.
More on this subject on Sun tutorial about interfaces and inheritance.
This is tagged for both PHP and Java - I'll answer for Java.
Right, it will inherit the implementation of the method from the superclass.
If the superclass still is declared as implementing the interface then it will cause a compile-time error, so this is not possible.
No, as I said before, if you implement an interface then you must implement or inherit the methods that that interface declares. The only exception is if the class is abstract, in which case you do not need to provide an implementation for the interface methods (which are implicitly abstract). Abstract classes cannot be instantiated.
Just adding to what has been already answered for the 2nd question (for Java):
As mentioned in other answer (by #BalusC), if your superclass tells that it implements an interface and doesn't provide implementation of method(s) in the interface then you have to mark the superclass as an abstract class. But you cannot instantiate the superclass since it is an abstract class.