class and interface - java

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.

Related

why it is required to add abstract keyword in front of abstract methods

This could be a useless question, Just out of my curiosity.
Why are we forced to add the keyword abstract in front of abstract method of abstract class, when we don't need to add
the keyword in the case of interface?
The possible answer could be:
As an abstract class may be a mix of concrete and abstract methods,
the 'abstract' keyword is used to identify which method(s) are abstract.
But
When we don't give any body to any methods, and use ; at the end of a declaration, then what could go wrong if it is considered as an abstract method automatically?
Why we are forced to add the keyword abstract in front of abstract method of abstract class, when we don't need to add the keyword in case of interface.
Within interfaces, all method definitions are implicitly abstract. You can provide the keyword, though, but there won't be any difference.
Within abstract classes, however, when you want to denote a method as abstract, you're required to type the abstract keyword. This actually makes the code more readable and easy to be understood.
Not all methods in an abstract class need be abstract. In fact, you can have an abstract class without any abstract methods.
On the other hand, all methods defined in an interface are implicitly public abstract, and so you can drop the qualifier.
You mention it explicitly so that anyone extending the class will know that he should implement those methods compulsorily and need not bother about the methods without the abstract keyword if he doesn't need to.
Two answers
Thats just the way language is designed
Abstract methods are meant to be overriden in child classes and that means some VM's can do various optimizations when they see abstract keyword
Btw. In java 4 there was actually abstract keyword in interfaces. Now it is considered as obsolete

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.

Java instance abstract class, which implements interface

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.

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.

Defining an abstract class without any abstract methods

Can I define an abstract class without adding an abstract method?
Of course.
Declaring a class abstract only means that you don't allow it to be instantiated on its own.
Declaring a method abstract means that subclasses have to provide an implementation for that method.
The two are separate concepts, though obviously you can't have an abstract method in a non-abstract class. You can even have abstract classes with final methods but never the other way around.
Yes you can. The abstract class used in java signifies that you can't create an object of the class. And an abstract method the subclasses have to provide an implementation for that method.
So you can easily define an abstract class without any abstract method.
As for Example :
public abstract class AbstractClass{
public String nonAbstractMethodOne(String param1,String param2){
String param = param1 + param2;
return param;
}
public static void nonAbstractMethodTwo(String param){
System.out.println("Value of param is "+param);
}
}
This is fine.
Yes you can do it. Why don't you just try doing that?
YES You can create abstract class with out any abstract method the best example of abstract class without abstract method is HttpServlet
Abstract Method is a method which have no body, If you declared at least one method into the class, the class must be declared as an abstract its mandatory BUT if you declared the abstract class its not mandatory to declared the abstract method inside the class.
You cannot create objects of abstract class, which means that it cannot be instantiated.
Yes we can have an abstract class without Abstract Methods as both are independent concepts. Declaring a class abstract means that it can not be instantiated on its own and can only be sub classed. Declaring a method abstract means that Method will be defined in the subclass.
Yes, you can declare a class you cannot instantiate by itself with only methods that already have implementations. This would be useful if you wanted to add abstract methods in the future, or if you did not want the class to be directly instantiated even though it has no abstract properties.
yes, we can declare an abstract class without any abstract method. the purpose of declaring a class as abstract is not to instantiate the class.
so two cases
1) abstract class with abstract methods.
these type of classes, we must inherit a class from this abstract class and must override the abstract methods in our class,
ex: GenricServlet class
2) abstract class without abstract methods.
these type of classes, we must inherit a class from this abstract class,
ex: HttpServlet class
purpose of doing is although you if you don't implement your logic in child class you can get the parent logic
please check the HttpServlet source code
You can, the question in my mind is more should you. Right from the beginning, I'll say that there is no hard and fast answer. Do the right thing for your current situation.
To me inheritance implies an 'is-a' relationship. Imagine a dog class, which can be extended by more specialized sub types (Alsatian, Poodle, etc). In this case making the dog class abstract may be the right thing to do since sub-types are dogs. Now let's imagine that dogs need a collar. In this case inheritance doesn't make sense: it's nonsense to have a 'is-a' relationship between dogs and collars. This is definitely a 'has-a' relationship, collar is a collaborating object. Making collar abstract just so that dogs can have one doesn't make sense.
I often find that abstract classes with no abstract methods are really expressing a 'has-a' relationship. In these cases I usually find that the code can be better factored without using inheritance. I also find that abstract classes with no abstract method are often a code smell and at the very least should lead to questions being raised in a code review.
Again, this is entirely subjective. There may well be situations when an abstract class with no abstract methods makes sense, it's entirely up to interpretation and justification. Make the best decision for whatever you're working on.
yes you can do that.
declaring class abstract means that class will not be instantiated by any other class.
and there should be at least one abstract method inside that and meaning of that you can declare abstract method in that class if you are not declaring method than its ok.
example:
public abstract class abs {
protected int cx = 0, cy = 0;
public void p() {
System.out.print("hello");
}
}
this will work for sure.
Yes you can. Sometimes you may get asked this question that what is the purpose doing this?
The answer is: sometimes we have to restrict the class from instantiating by its own. In that case, we want user to extend our Abstract class and instantiate child class
Actually there is no mean if an abstract class doesnt have any abstract method . An abstract class is like a father. This father have some properties and behaviors,when you as a child want to be a child of the father, father says the child(you)that must be this way, its our MOTO, and if you don`t want to do, you are not my child.
Yes, you can define an abstract class without an abstract method. However, if there is no method inside you might better go with an interface

Categories