Few Questions over abstract class and interface in java?
- What is use of an abstract class implementing an interface? In which scenario would we implement.开发者_如何转开发
- Why would you choose Abstract class over interface? If an class extends an Abstract class, should we implement all the methods in the Abstract Class. If an class implements an Interface should we implement all the methods too.
- Why was abstract class or interface pattern introduced? What is the use of them? Does it deal anything in the way object is getting instantiated or the way it behaves?
- If an abstract class has a static method declared, then can we instantiate that class?
I was asked these questions, though my answers were not clear... i would like to know from people here?
1&2) You can use interfaces and abstract classes together -- the interface can provide a definition of behavior, and the abstract class provides partial implementation, the subclasses provide full implementation.
3) Interfaces and abstract classes are concepts. The java language designers decided to provide language features to realize the concepts.
4) An instance of an abstract class can never be instantiated. A static method on an abstract class can be invoked normally, remember static methods are invoked by referencing the class, not an instance of the class.
some psuedocode
abstract class Base {
abstract void doSomething(); // subclass provides implementation
protected helper() {
// doSomething implementation in subclass can use helper
}
}
On Abstract classes and Interfaces
Abstract classes defines a partial representation of some entity, that is common to every extending class but that must be completed by inheritance. So, every inheriting class comprehends all the properties defined in every parent classes.
Interfaces provide a behavior to a given class, and allow to simulate something similar to multiple inheritance. Since you can implement multiple interfaces in a class, you can make that class adopt the behavior of every implementing interface, and all together.
Classical example: Abstract class: Animal Represents an animal in an abstract way. Each animal should extend this class to adopt all the implications of "being" an animal.
Abstract class: Mammal extends Animal The mammal is an Animal and inherits all the properties that are common to every mammal.
Some mammals are carnivorous and some herbivorous. These are different behaviors for the same type of animal, so here they come the interfaces.
Interface: Carnivorous Defines the properties a carnivore animal should have.
Interface: Herbivorous Defines the properties a herbivore animal should have.
Every mammal should breath air. This is another behavior, that is common to every mammal.
Interface AirBreathing Defines the properties for air-breathing animal
So. You have these two mammals: Wolves and Manatees. Both have to breath air, but wolves are carnivorous and manatees herbivorous.
Class Wolf extends Mammals implements AirBreathing, Carnivorous
Class Wolf extends Mammals implements AirBreathing, Herbivorous
Implementation details
1) Every abstract method defined in an abstract class, must be implemented somewhere "in the road" of inheritance until a final class is reached. This means that when a final class is implemented it must implement or inherit implementations of every abstract method inherited from parent classes.
2) Yes, you can define static methods in abstract classes. You can call static method from the abstract class as AbstractClass.staticMethod(), but you can't ever instantiate an abstract class.
3) Classes implementing interfaces, must implement every method defined by the interface.
Hope this helps.
What is use of an abstract class implementing an interface? In which scenario would we implement?.
One important use is to provide a skeletal implementation reducing the effort needed to implement the interface. An example of this is AbstractCollection<E>
which implements the Collection<E>
interface in the Java Collections Framework.
Why would you choose Abstract class over interface?
When I want to provide an implementation for some parts of the class's behaviour.
If a class extends an abstract class, should we implement all the methods in the abstract Class. If an class implements an interface should we implement all the methods too.
Concrete classes must completely implement the abstract interface. You have no choice but to implement all methods. This is because whenever you implement an interface or extend an abstract class with a concrete implementation, you're declaring that the concrete class fully describes the interface's behavior.
Of course abstract extensions don't have to implement the interface completely i.e. abstract extensions of interfaces / abstract classes aren't forced to implement the interface in entirety.
Why was abstract class or interface pattern introduced? What is the use of them? Does it deal anything in the way object is getting instantiated or the way it behaves?
Interfaces describe the behavior of types. Since all implementations must completely describe the behavior enforced by the interface, they allow for polymorphism.
Canonical Collections Framework example:
- The
List<E>
interface describes how a list of objects may behave. It describes theadd
,remove
,get
andset
operations to name a few.
2.ArrayList<E>
and LinkedList<E>
are two concrete implementations of the List<E>
interface.
3.Thus it's guaranteed that both of them behave like a List<E>
and more specifically have concrete implementations of the add
, remove
, get
and set
operations to name a few.
4.Also since both of them implement the same interface, more specifically, both of them behave like a List<E>
, I can use them, wherever I need to use a List<E>
.
5.This allows for statements like these :
List<String> names = new ArrayList<String>();
List<String> queue = new LinkedList<String>();
showList(names);
showList(queue);
void showList(List<String> list) {
for(String s : list)
System.out.println(s);
}
This is a very simple example of polymorphism in action. The showList(List<String> list)
methods accepts and happily iterates over names
and queues
because both of them implement the same interface and it can be sure of it's behaviour.
I have just completed my studies and joined a firm. So might be possible i may not be able to give you good example but yes in broad sense they gonna work and they are effective... the most important reason for using interface is to create a common behavior in related classes. Wonderfull example is collection framework. Collection is a interface that ensure whatever the classes that represents collection of some objects then it should compulsory implement all the methods of collection(else they wont be a collection, also note that collection is a behavior) and now this interface is further implemented by other interface list so all the collection which are of type list implement this(here we have specialize the behavior of collection(by adding some method), note that list is also a behavior). Now we want a class which represents nothing but a collection of array as linked list so we implemented all the methods of list(which ultimately have collection method). Now come to abstract class, consider the example of Number class in java its a abstract class which implements serializable interface since it has to show serialized behavior. Also note that Number represents some physical entity, so it should be a abstract class. Abstract because from it we cant conclude to exact object. While serializable is a behavior hence interface. Thats what i could deliver to you and thats what i learned from my basic knowledge. But always remember oops differ people to people. See your encapsulation implement abstraction example as well. So everything depends on you.
精彩评论