I am having an interface called I. This interface is implemented by an abstract class AClazz.
AClazz implements I
Now this abstract class is extended by several concrete classes.
ConcreteClazz extends AClazz
AnotherConcreteClazz extends AClazz
Now when I add a new method contract to the interface I why does eclipse and hence Java language complain about a missing implementation inside the concrete classes (ConcreteClazz and AnotherConcreteClazz) and not inside the abstract class AClazz?
If AClazz was a concrete class (& not abstract) and the concrete classes extended from the non abstract AClazz will Java complain about missing implementation inside concrete classes again?
So say your interface I has a method m:
public interface I {
public void m (int someParameter);
}
Now AClazz implements I, and ConcreteClazz extends AClazz. That means that ConcreteClazz also implements I, whether or not you say so explicitly in the class declaration of ConcreteClazz. That means you can use a ConcreteClazz object anywhere an I is expected. So say somewhere, in some other class, you have a method
void takeSomeAction(Something x, I y) {
....
y.m (x.getSomeProperty()); // call the interface method
....
}
You can call it with a ConcreteClazz object:
ConcreteClazz z = new ConcreteClazz(...);
Something st;
foo.takeSomeAction(st, z);
You're giving takeSomeAction a ConcreteClazz to use as its I parameter. Then takeSomeAction will call the m method. How can this work unless you've provided an implementation for m, somewhere?
That's why you need to implement m, either in AClazz or ConcreteClazz. You could implement it in AClazz, if that's the right thing to do, and then you don't need to provide another implementation in ConcreteClazz. But if you don't implement it in AClazz, it won't cause any errors, since, at runtime, you can't actually have an object of type AClazz. But since you can have objects of type ConcreteClazz, that means that somewhere, for ConcreteClazz, there has to be an implementation of m.
An Abstract class means, this class contains abstract methods (i.e. no implementation, only definition of method), the classes that are extending it must implement the unimplemented methods.
Since an Interface contains 100% pure public abstract methods, which must be implemented by the classes that are implementing them unless it's marked as abstract. Again follow the above rule.
An abstract class remains abstract no matter how many new abstract methods you add to it. So compiler does not complain when you add a new method in your I interface which is implemented by your abstract class.
A concrete class cannot have any abstract methods, as your subclasses are extending your abstract class and that class does not implement the newly added method to the interface, hence the compiler complains. Because newly added abstract method is inherited in all your child classes and a concrete class cannot have an abstract method.
Related
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.
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.
public abstract class A {
abstract void polo();
}
class B extends A {
}
My doubt is if I give abstract keyword in class B means it wont shows anything
abstract class B extends A {
}
Why its not showing the message here to implement methods in class A - this is my doubt.
If I didn't give abstract keyword means class B indicates like this
The type B must implement the inherited abstract method A.polo()
What's wrong with that one. Can abstract class extend another abstract class or not.
If a non-abstract class extends an abstract class, it must implement all abstract methods, in your case class B must implement polo()
If you make class B abstract, then you don't have to. But in this case you can't create an instance of B and the classes that extends B should implement polo
You can extend an abstract class, but you must decide what the new one will do about the abstract methods. You can choose to implement all those methods, so the abstract keyword won't be required. You can also implement some or none of the methods, but you must mark the class as abstract.
Depending of the business logic, you may put the abstract methods into an Interface that required classes will implement.
if B is abstract
not mandatory to implement the method
but you can implement the method or left implementation for child class
but if B is not abstractit is mandatory you implement the abstract method
One handy trick I use in this situation is to make class B non abstract to start with. I then use the IDE to generate the over-rides for all the abstract methods and then afterwards I make class B abstract and then go through the overridden methods and decide for each one whether to implement them or leave them abstract at this level too.
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.
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).