Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Just as all subclasses which extend from an abstract class must provide implementation for all abstract methods, do all methods of an interface must do the same for all methods which implement them?
I think you meant to say that all the classes that implement an interface must provide implementation for all its methods. Yes, that's right.
… Unless it's an abstract class implementing the interface, in that case it can leave some of the methods unimplemented (but some concrete class must implement them, eventually). Also, if the interface extends more interfaces, all the methods in those must be implemented, too.
an abstract class leaves the implementation of one or more of it's methods to it's extending classes, it can do the same with the method contracts it inherits through implementing additional interfaces.
If an abstract class implements an interface, it is not bound to implement any of the the inherited methods. That doesn't mean you can't implement any of them. :D
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
class A{}
class B extends A{}
here is it the case that B is extending A alongwith object class.
We all know, in java, all classes by default inherits Object class.
But it is also there that multiple inheritance is not allowed in java.
Then when we inherit a class, what happens with this rule?
We all know, in java, all classes by default inherits Object class.
Yes. But I suspect that you don't understand what that really means.
What it means is that if a class is not declare (via an explicit extend) to inherit from some class, then it implicitly inherits from Object.
class A {} // implicitly inherits Object
class B extends A {} // explicitly inherits A
To say this in other words:
A has Object as its only direct superclass
B has A as its only direct superclass.
B has Object as an indirect superclass.
This is single inheritance. Multiple inheritance would be if B had A and Object as direct superclasses.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I don't get how can I write a sub class which will extend a superclass and a couple of interfaces at the same time.
A class can only extend one superclass, but can implement multiple interfaces, which is done by separating them by a comma, like so:
public class MyClass extends AnotherClass implements AnInterface, AnotherInterface
Extending a class is simple: unless the class you are extending is abstract, your class does not even need to do anything apart from providing a suitable constructor.
Implementing an interface or extending an abstract class, on the other hand, is simply a promise to implement a specific set of methods, as declared in the corresponding abstract class and/or the interface. Again, all your class needs to do is providing some implementation for these methods.
Of course, the implementations need to make sense in the overall scheme of your design, but technically they just need to be there in order for the code to compile.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have an abstract superclass. Some of the methods have implementation in the abstract class. In my subclass would i need to override the implemented method. The method in the subclass does exactly the same as superclass.
public abstract class vehicle {
public int getNumber() {
return 5;
}
}
public class car {
public int getNumber() {
return super.getNumber();
}
}
So my question is, do i need to override the method and include a super call? Or can i just leave it in the super class and not implement it at all in the subclass?
No, you do not. You can just leave it in the super (parent) class and not implement it at all.
The Reason
Basically the idea behind an abstract class (very briefly) is that you can write down common implementations that all the concrete classes derived from the abstract class once, and then not have to override or rewrite them again.
An example like in your case would be if all the vehicles have a number, it is a common property, then there isn't a point in writing the same getNumber() method for each class that derives from that class. The abstract vehicles class is like a 'framework' of sorts, and the concrete classes (car, bus, van) that derive from the vehicle class is the actual class that you instantiate. In this class you can have more specialized variables and methods, (is the car a 2 wheel or 4 wheel? A truck probably doesn't need that method)
Related readings from this SO question: Purpose of Abstract Classes
In short the beauty of abstract classes is that you don't have to be concrete (lol) about every single detail, yet you can have one method for across all common classes, and override them if they are special cases. This helps in maintaining code too (A bug in a common method shared across all classes only requires change in the abstract class and not all of the concrete classes)
Additional Info
Just as additional info, if you did use an interface and NOT an abstract class, then you would HAVE to override each method there, as an interface does not contain the implementation, only the design. This link helped explain things to me last time.
You might also want to read up on Polymorphism and Inheritance, important concepts in all Object oriented Programming (OOP) languages. :)
Note: Also, since I can't comment on your question (not enough rep) yet but have been lurking on SO long enough, don't be discouraged by the downvotes, many here assume you have the right keywords too search for on StackOverflow to get the answer you want but this isn't always the case. Good luck!
You can leave it in the superclass and not implement it in the subclass.
calling
car BMW = new car();
BMW.getNumber();
should grab the superclass method.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I know that a class which implements an interface can be declared a abstract but not sure if it can be declared as interface? If so,What will be its benefit?
No interface can implement an interface by definition.
An interface is a contract that formalizes a behavior and cannot implement logic.
However, interfaces can extend other interfaces.
An abstract class can implement an interface on the other hand, because either:
Behavior is implemented in the abstract class, or
Behavior implementation is deferred to classes extending the abstract class
Here's a general link to Oracle's OOP documentation you want to have a look at.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Super class being an abstract class creates an overhead for all its sub classes to compulsorily define its abstract methods. I understand that it's very basic but I need to know why do programmers usually make super class as an abstract class, though we can do similar things using a super class as a non abstract class.
An abstract superclass is one way to provide re-usable code.
You can extend the abstract class and inherit the code. This is sometimes more convenient than using static methods or object composition to share code.
The abstract class can "fix" parts of the code (by making it final). This is called the "template method" pattern (and this is not possible with an interface, which cannot provide final methods).
Of course, you can achieve both with a non-abstract superclass as well.
An abstract class has the additional benefit that it does not have to provide a complete implementation (that would make sense to instantiate on its own), some parts can be left specified, but unimplemented (the abstract methods).
imagine you have a common behaviour where only small details are specific to the implementation - then you can put all the common behaviour in a abstract base class and having some abstract methods that the implementing classes need to fill.
For example a abstract repository base class might implement all the details to contact your server, etc. and concrete repositories just need to fill in the details to read the right object from the right table, etc.
Abstarct classes are meant for 'abstracting'. means if some classes are having common behaviour, instead of writing evry time the same thing in each class, write that in one class and ask the other classes to use it [by making the classes as subclasses to the abstract class]. This is nothing but inheritance. To summarise: Use abstract classes when you want default behaviour for some classes Use interfaces when you want different behaviour different classes.
For More explanations Refer below links:
http://www.javacoffeebreak.com/faq/faq0084.html
http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
several usage of abstract class:
act as the protocol when tranfering data between objects, the two customers need not to know other class's structure. ----- just like the interface
define the abstract operation, which hides the detailed implementation of concrete class, for example, I have a class called AbstractPayment, which define the opration of charging money from customer, then the concrete classese of it could be: PaypalPayment, AlipayPayment, BankPayment and others. BUT, for the class customer, it only needs to know the AbstractPayment.
after some time, if you need to add another ConcretePayment, or modify one other payment, the customer class won't change.
Abstraction is largely used in design patterns, I suggest you to read following:
Abstract Factory Pattern
STO