In java, we give body to an abstract method of parent class inside the child class and then call that function via child class object like:
//let the abstract function be fun then,
child c= new child();
c.fun();
then fun executes the body given by child but what's new in this as same being done in function overriding?
The difference is that subclasses that won't also be abstract must implement all inherited abstract methods. Also, the concrete implementation can't call super.method() as there's no implementation in the abstract class.
As to the why, or perhaps more explicitly, why choose the abstract superclass rather than an interface: often it's useful to provide implementations of only some methods of an interface - for instance for code-sharing - and require concrete subclasses to provide specific implementations for other parts of the interface.
The abstraction means that the developer has (explicit) to implement the method. Overriding means that the developer implement a method that's already implement. This is by definition of the languaje.
Related
In Java inheritance, can the expression "overriding an abstract method" be used interchangeably with the expression "implementing an abstract method"?
I came across the following question in the book "OCA Java SE 8 Programmer I Study Guide" in Chapter 5, page 296, question #15:
Q: Which of the following is true about a concrete subclass? (Choose all that apply)
A. A concrete subclass can be declared as abstract.
B. A concrete subclass must implement all inherited abstract methods.
C. A concrete subclass must implement all methods defined in an inherited interface.
D. A concrete subclass cannot be marked as final.
E. Abstract methods cannot be overridden by a concrete subclass.
My answer was B & E. But the book says, the correct answer is only B.
My question is specifically about the option E. The book says, option E is incorrect because abstract methods must be overridden by a concrete subclass. My initial thought was abstract methods must be implemented before being overridden by a subclass. So why is option be incorrect?
Let me explain each points one at a time.
A. A concrete subclass can be declared as abstract.
Ans: Incorrect.
By the definition, concrete classes are those which can be instantiated and this class can also extends the base abstract class.
B. A concrete subclass must implement all inherited abstract methods.
Ans: Correct
If the methods are marked as abstracts in the base class, then the child class extending base class should make sure that all the abstract methods are implemented. You will otherwise get a compile time error.
C. A concrete subclass must implement all methods defined in an inherited interface.
Ans: Incorrect
this is only the case when the concrete subclass has directly implemented the interface by this class itself.
But in this case, base class may have implemented an inherited interface .Hence, its not mandatory for concrete sub-class to implement all methods that are there in base class as a result of inheritance from the interface.
D. A concrete subclass cannot be marked as final.
Ans: Incorrect
If a subclass is marked as final, then this class can no longer be extended by any other available sub-classes. Marking a class with a final keyword is particularly used to achieve Immutability.
With Immutable classes, We can call accessor methods (e.g. getter methods), copy the objects, or pass the objects around — but no method should allow modifying the state of the object. Examples: String, wrapper classes like Float, Integer.etc.
E. Abstract methods cannot be overridden by a concrete subclass.
Ans: Incorrect
By the definition, Abstract methods are methods that are only declared but contains no body or implementation.And since we cannot instantiate abstract class, it is up-to sub classes to provide the implementations for these defined abstract methods.Hence abstract methods are not optional but mandatorily be overridden by a inheriting subclass.
An abstract method is a method that is declared, but contains no implementation.
you can override both abstract and normal methods inside an abstract class.
only methods declared as final cannot be overridden.
An abstract class can have both abstract and non abstract methods. What is the point of having non abstract methods in the abstract class if a new object of the abstract class can't be created?
I know you can override the non-abstract method in a child class and then use it through the object of the child class. But if you are doing that, what is the need of having the non-abstract method with an implementation in the first place?
Think more or google more.
If your child classes have the common functionality then why you will override the method in every class? you can use the base class(which is abstract in this case) method.There comes the need of non-abtract(concrete as they called mostly) methods.
while having abstract method there(as you know already i think), we can override according to our requirement.
If you need all methods should be override in every child classes according to their requirement, then you can go for Interface.
Simple answer: Reuse and maintainability.
Suppose there are 4 concrete classes extending your abstract class which are all going to share some behaviors.
In this case it is better to implement the method in abstract class rather than defining it separately in all your concrete classes.
Concrete subclasses can use methods in the abstract superclass. So all shared functionality between subclasses can go into the base abstract class.
Code reusage. If you don't override non-abstract methods in your inheriting class(es) you inherit them from the abstract class.
To instead have them in the subclass violates the principle of DRY (don't repeat yourself): if all the subclasses have the same function, why write it repeatedly in every class?
As I know the subclass constructor calls the super class constructor by using super();.
But since interface doesn't have any constructor, how can inheritance take place?
But since interface doesn't have any constructor how can inheritance take place??
Easy, an interface cannot have any instance fields so there is nothing to construct. You cannot place in code in an interface (up to Java 7 anyway) so there is nothing which needs to be called.
The interface is a contract, defining what methods mush be offered by the implementation. A class doesn't inherit an interface but implements it.
From the specification :
This type has no implementation, but otherwise unrelated classes can
implement it by providing implementations for its abstract methods.
Interfaces (also known as Service Contracts) are implemented, not constructed. They define a set of methods (Services) that a class provides, so a client knows what can he expect of the implementing class regardless of the actual type implementing the interface. The constructor is related to this particular instance of a given type, implementing the interface.
IYourObject yourObject = new YourObject();
On the other hand, interface inheritance is also by extension. It "adds" the methods of an interface to another one and allow the possibility of switching the interface type of an object amongst the different interfaces in the "hierarchy".
Interface is not inherited - it is rather implemented
Interface doesn't contain constructor because of the following reasons:
The data member of the interface are already initialized .
Constructors of the special defined methods which are not permitted to defined and moreover interface data member are static.
What is the difference between abstract methodand method overriding in java? Because same result can be found by using method overriding. so what is the necessity of abstract method.
An abstract method forces a programmer who inherits the method to define an implementation for it, i.e. the class says "I will need an implementation of this function, which the concrete implementation of this class must provide".
This is in contrast to overriden methods, which allow (rather than require) the inheriting class to change the implementation of the method for objects of that class.
In particular, overridable methods (or "virtual" methods) have a basic implementation (which can itself be empty) that the overriding method can call.
The abstract method needs to be implemented in order for the subclass to be utilized. You must also implement non-abstract methods, for instance, in the case of interface method signatures.
I think it is correct to say that you implement abstract methods in the same way that you implement method stubs of an interface. Overriding is not the same as implementing because you can override methods which are not stubs. Subclasses often override methods where the superclass is neither an interface nor an abstract class. So the term overriding has a broader meaning than implementation.
You may find this tutorial helpful as it has quite a clear definition.
http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
Hope that helps!
The two have different usages which can't be compared.
An Abstract Class is created only to be sub classed by some other class. The implementing class necessarily has to implement all the abstract methods declared inside it. However Method Overriding is the ability of a subclass to override a method from an inherited superclass whose behavior is "close enough" and then to modify behavior as needed. An Abstract Method is one that is declared without an implementation and needs to be necessarily implemented in the subclass.
Is there any keyword or design pattern for doing this?
Please check the update
public abstract class Root
{
public abstract void foo();
}
public abstract class SubClass extends Root
{
public void foo()
{
// Do something
//---------------- Update -------------------//
// This method contains important code
// that is needed when I'm using a instance
// of SubClass and it is no instance of any
// other class extending SubClass
}
}
public class SubberClass extends SubClass
{
// Here is it not necessary to override foo()
// So is there a way to make this necessary?
// A way to obligate the developer make again the override
}
Thanks
If you are doing this, then you are probably abusing inheritance; inheritance, contrary to popular myth, is not intended for making custom hooks/handlers, but rather to enable alternative implementations.
If you want your user to provide some sort of function/hook/callback, then you should define an interface that provides just those methods that you need your user to define. Then you should require the user to pass in an instance of that interface to your object's constructor or passed into the function that needs it.
Aggregation, delegation, and composition are frequently better and safer design patterns than inheritance; forcing other users to inherit from your class, is incredibly risky, as it provides the user with many opportunities to violate the contract of your class or to invalidate the invariant of your base class.
If every class subclassing SubClass has to override foo() then why provide an implementation at all in SubClass? You can simply remove the method definition from SubClass and then all subclasses will be forced to provide an implementation.
If you really want to, you can re-declare foo as abstract.
public abstract class SubberClass extends SubClass
{
public abstract void foo();
}
Instead of overriding foo() in SubClass, create a new method fooImpl() and leave foo() abstract. This way, all classes must implement foo() but you can simply implement it by calling fooImpl() if that is already enough.
Yeah it is not necessary to override foo() in SubberClass.
You can't have it both ways. You can't provide a method with a default implementation AND require child classes override it. Instead of declaring the method as abstract in Root, you could define an interface (IFoo) with the method declared and then provide an abstract class that implements the interface. That would still require a concrete child class but would not require a method override.
Most of the time you see this type of pattern, an interface is used to define a set of methods and an abstract base class provides some default implementations for some but not all methods from the interface. This requires the concrete child class to provide code for the remaining methods and the option to override the default behaviors.
In any case, you can't provide a default behavior for a single method and require child classes to override that same method.