Why does abstract class have to implement all methods from interface?
You need to re-write all of the members/methods in the interface and add the abstract
keyword to them, so in your case:
interface baseInter {
name: string;
test();
}
abstract class abs implements baseInter {
abstract name: string;
abstract test();
}
(code in playground)
There was a suggestion for it: Missing property declaration in abstract class implementing interfaces but it was declined for this reason:
Although, the convenience of not writing the declaration would be
nice, the possible confusion/complexity arising from this change would
not warrant it. by examine the declaration, it is not clear which
members appear on the type, is it all properties, methods, or
properties with call signatures; would they be considered abstract?
optional?
What is the use of abstract class that implements an interface
The name it self showing that it's abstract
, it need not to implement. Where as the subclasses of that abstract class must and must full fill that definition and needs to implement that methods in that interface.
An abstract class in Java need not implement any methods from its implementing interface. Why?
The abstract
class is not real implementation class. It may contain abstract
methods and doesnot need to implement
the methods from the interface
. It is concern of the REAL implementing class to define the abstract/interface methods.
See this difference between abstract class and interface
Interface:
public interface InterfaceClass {
void interfaceMethod();
//no method definition
}
//Abstract Class implementing InterfaceClass
abstract class AbsClass implements InterfaceClass{
abstract void abstractMethod();
public void doSomethingCommon() {
System.out.println("Abstract class may contain method definition");
}
//no need to implement methods of InterfaceClass because AbsClass is abstract
}
And here is real class that extends AbsClass
: Its duty of RealClass to define all abstract methods and interface methods. Additionally, it may override
the defined methods in abstract
class as well.
public class RealClass extends AbsClass{
@Override
public void interfaceMethod() {
//implement interface method here
}
@Override
void abstractMethod() {
}
// you may override the doSomethingCommon() of AbsClass too
@Override
public void doSomethingCommon() {
// TODO Auto-generated method stub
super.doSomethingCommon();
}
}
Why there is no compile time error on AbsClass
:
We cannot create instances of abstract class. That's why there is no meaning of displaying error at compile time.
Y it's not mendatory to implement all interface methods in Child abstract class But Mendatory to implement all Interface methods in Grand Child class?
That is because a concrete class must have implementation because they can be instantiated. Suppose they allow concrete class not to implement all the methods of an interface, there will arise a problem. If in the code we call the unimplemented method, JVM wont be having the address of the unimplemented method.
But abstract classes can not be instantiated. Thats why it is not mandatory to implement methods of an interface by an abstract class.
C#: Abstract classes need to implement interfaces?
In C#, a class that implements an interface is required to define all members of that interface. In the case of an abstract class, you simply define those members with the abstract
keyword:
interface IFoo
{
void Bar();
}
abstract class Foo : IFoo
{
public abstract void Bar();
}
Or to put it another way: you don't have to "implement" it (which would be a terrible limitation on abstract classes); however, in C#, you do have to tell the compiler that you are deliberately passing the buck to concrete subclasses - and the above line of code shows how to do so.
The comments and downvotes complaining that this is not an answer to the question are missing the point. Someone coming to Stack Overflow, having received this compiler error, but having an abstract class in which it would be a mistake to supply an implementation, are stuck without a good solution - would have to write implementation methods that threw runtime exceptions, a horrendous work-around - until they have the above information. Whether it is good or bad that C# requires this explicitness is outside the scope of Stack Overflow, and not relevant to the question nor this answer.
Java abstract class implements an interface, both have the same method
In an interface, all methods are public
and abstract
.
Knowing this, interface A's doStuff is actually public abstract void doStuff()
. Which should look familiar, as Abstract Class B has the same method signature.
To answer question 1, class B's doStuff()
is the same as interface A's doStuff()
. Since all methods in Java are virtual, calling doStuff()
will be the same regardless of if your C is declared as an A, a B, or a C.
As for question 2, no. B's doStuff()
is redundant code that doesn't actually do anything. C is implementing A's doStuff()
whether or not B declares doStuff()
.
My abstract class implements an interface but doesn't implement some of its methods. How do I make it compile?
The way to do this is to add an abstract void Tick()
method to CanvasTool_BaseDraw
and override it in CanvasTool_Spray
.
Not every programming language does it this way. In Java you do not have to add an abstract method for every method in the interface(s) you implement. In that case your code would compile.
Related Topics
How to Obtain the Start Time and End Time of a Day
How to Pass Console Arguments to Application in Eclipse
@Transactional(Propagation=Propagation.Required)
Transactional Saves Without Calling Update Method
Difference Between an Application Server and a Servlet Container
How to Copy Hashmap (Not Shallow Copy) in Java
How to Mock New Date() in Java Using Mockito
Fixedthreadpool VS Cachedthreadpool: the Lesser of Two Evils
Does Stream.Foreach Respect the Encounter Order of Sequential Streams
How Does Java Garbage Collector Handle Self-Reference
Joda-Time: What's the Difference Between Period, Interval and Duration
Make Hibernate Ignore Instance Variables That Are Not Mapped
How to Copy the Contents of One Arraylist into Another
Maven "Build Path Specifies Execution Environment J2Se-1.5", Even Though I Changed It to 1.7
Access Maven Properties Defined in the Pom
What's the Difference Between a Resource, Uri, Url, Path and File in Java
Create Java Runtime Image on One Platform for Another Using Jlink