what must be implemented from an abstract class in java? - java

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.

Related

Is it possible and meaningful to make the superclass abstract?

I am told that: a class can extend a concrete superclass and override an implemented method to make it abstract.
If the method becomes abstract, then the corresponding superclass should become abstract too.
Is this correct? If so, is it meaningful to make the superclass or the method in superclass to become abstract?
To contain abstract methods the class itself must also be declared abstract.
Abstract methods can be used where behaviour is specific to each subclassing object. Although this can also be achieved through interfaces, you may prefer to use an abstract class as they can also contain already implemented methods, or you may wish to declare fields which are not static and final. Beware that when a class becomes abstract it can no longer be instantiated.
Read more about abstract classes here.
Whether or not you make your class abstract if it contains abstract methods is not a choice, it is a must. You cannot have a non-abstract class that has abstract methods.
Whether or not you should declare a method abstract or implement it in your super class comes down to your concrete use case. Is there a default behavior that makes sense for all possible concrete implementations of the super class? Then you might want to implement it. If that's not the case, declare it abstract and let the sub classes take over.
I am told that: a class can extend a concrete superclass and override an implemented method to make it abstract.
If the method becomes abstract, then the corresponding superclass should become abstract too.
Is this correct?
I think it's incorrect.
Take the following code for example.
public class Father {
public void print() {
System.out.println("Father");
}
}
public abstract class Son extends Father {
#Override
public abstract void print(); /* no need to abstract the superclass */
// and other abstract or concrete methods
}
public class Grandson extends Son {
#Override
public void print() {
System.out.println("Grandson");
}
}

Normal Class extend to another abstract class

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.

What are abstract classes and abstract methods? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Abstract class in Java
I got several explanations but so far I'm not able to understand that what are the abstract classes and methods in Java.
Some said it has to do something with the security of the program, other said it isn't anything like that.
Even from the Dietel & Dietel's book I don't get it's purpose.
When,where and why do we use it?
Kindly explain it like you're teaching a beginner, I would really appreciate your help.
- Abstract class is one which can't be instantiated, i.e. its object cannot be created.
- Abstract method are method's declaration without its definition.
- A Non-abstract class can only have Non-abstract methods.
- An Abstract class can have both the Non-abstract as well as Abstract methods.
- If the Class has an Abstract method then the class must also be Abstract.
- An Abstract method must be implemented by the very first Non-Abstract sub-class.
- Abstract class in Design patterns are used to encapsulate the behaviors that keeps changing.
An abstract class is a class that can't be instantiated. It's only purpose is for other classes to extend.
Abstract methods are methods in the abstract class (have to be declared abstract) which means the extending concrete class must override them as they have no body.
The main purpose of an abstract class is if you have common code to use in sub classes but the abstract class should not have instances of its own.
You can read more about it here: Abstract Methods and Classes
An abstract method is a method signature declaration with no body. For instance:
public abstract class Shape {
. . .
public abstract double getArea();
public abstract double getPerimeter();
}
The methods getArea() and getPerimeter() are abstract. Because the Shape class has an abstract method, it must be declared abstract as well. A class may also be declared abstract without any abstract methods. When a class is abstract, an instance of it cannot be created; one can only create instances of (concrete) subclasses. A concrete class is a class that is not declared abstract (and therefore has no abstract methods and implements all inherited abstract methods). For instance:
public class Circle extends Shape {
public double radius;
. . .
public double getArea() {
return Math.PI * radius * radius;
}
public double getPerimeter() {
return 2.0 * Math.PI * radius;
}
}
There are many reasons to do this. One would be to write a method that would be the same for all shapes but that depends on shape-specific behavior that is unknown at the Shape level. For instance, one could write the method:
public abstract class Shape {
. . .
public void printArea(PrintStream out) {
out.println("The area is " + getArea());
}
}
Admittedly, this is a contrived example, but it shows the basic idea: define concrete behavior in terms of unspecified behavior.
Another reason for having an abstract class is so you can partially implement an interface. All methods declared in an interface are inherited as abstract methods by any class that implements the interface. Sometimes you want to provide a partial implementation of an interface in a class and leave the details to subclasses; the partial implementation must be declared abstract.
abstract method do not have body.A well defined method can't be declared abstract.
A class which has abstract method must be declared as abstract.
Abstract class can't be instantiated.
An abstract class is a class that you can't create an object from, so it is mostly used for inheriting from.(I am not sure if you can have static methods in it)
An abstract method is a method that the child class must override, it does not have a body, is marked abstract and only abstract classes can have those methods.
With abstract classes you can have some kind of skeleton for other classes to extend.
You can't instantiate them but you can put some common implementation which you can use in the classes that extend them.
Once you get what abstract means in Java, you would ask: why they put this in ? Java may work without abstract stuff, BUT it makes part of a certain OO style or vocabulary. There exists really situations where an abstract class or method is an elegant way to express the program authors intention. Most when you are programming a framework or a library that will be used by others.
ABSTRACT CLASSES AND ABSTARCT METHODS FULL DESCRIPTION GO THROUGH IT
abstract method do not have body.A well defined method can't be declared abstract.
A class which has abstract method must be declared as abstract.
Abstract class can't be instantiated.

What is difference to extend abstract class and non abstract class?

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).

What is the difference between an abstract class and a class that has all its methods abstract?

I wonder what is the difference in Java between an abstract class and a class that has all its methods abstract? I mean, is an abstract class just a class whose methods automatically get abstract?
Absolutely not. Indeed, a class can be abstract without any methods being abstract, although that's relatively rare (see Mark's comment below for an example). On the other hand, if a class has any abstract methods, then it must be declared abstract.
Generally speaking, the purpose of an abstract class is to provide a skeleton with some non-abstract behaviour, but other bits still to be filled in by subclasses. This can be used with the template method pattern, for example.
Any class that contains one or more abstract methods must also be declared abstract. To declare a class abstract, you simply use the abstract keyword in front of the class keyword at the beginning of the class declaration. There can be no objects of an abstract class. That is, an abstract class cannot be directly instantiated with the new operator. Such objects would be useless, because an abstract class is not fully defined. Also, you cannot declare abstract constructors, or abstract static methods. Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or be itself declared abstract.
The Only difference between abstract class and interface is that abstract class can be inherited and whereas interfaces can't, thus interfaces don't have any constructors conversely with an abstract class.
Whenever you make a abstract method in the class then you explicitly mention the abstract keyword before the class name, just like this
public abstract class Test {
abstract void show();
}
Here are the points related Abstract class in Java
-->Abstract class is the one of class that cannot be instantiated.
-->If you want to get implementation of any method from child class(other person) then abstract method can use in this sense.
-->Abstract class are incomplete ,subclass must declare missing piece to become concrete class(Class whose object can be instantiated ) , otherwise these subclass also become abstract class.
-->You can achieve abstraction that is main pillar of OOP through by "abstract classes".
Abstraction hide the irrelevant detail of an object.
-->Abstract use for IS A Relationship (Inheritance).
-->Abstraction use for to achieve Polymorphic behavior (Another main pillar of OOP)
-->abstract class should not be private and not contained private method.
-->You extends single abstract class not multiple because Java is Single supported Inheritance
--> Abstract class must contain 1 or more than 1 abstract method
-->If any class contain abstract method then it should explicitly declare abstract class even if it contain concrete method.
--> Constructor and static method cannot be declared as abstract because constructor are not inherited.
--> If child class have not implement the abstract method of super class then it become also abstract class.
-->Attempting to instantiate the object of abstract class is an Compilation error.
-->Abstract super class variable can hold the reference of child concrete object.
A class which contains the abstract keyword in its declaration is known as abstract class.
Abstract classes may or may not contain abstract methods ie., methods with out body ( public void get(); )
But, if a class have at least one abstract method, then the class must be declared abstract.
If a class is declared abstract ,it cannot be instantiated.
To use an abstract class you have to inherit it from another class, provide implementations to the abstract methods in it.
If you inherit an abstract class you have to provide implementations to all the abstract methods in it.
If you don't want to provide implementation for all abstract methods then there is a concept of adapter class:
Example:
abstract class A{
public void m1();
public void m2();
public void m3();
}
class B extends A{
public void m1(){}
public void m2(){}
public void m3(){}
}
class C extends B{
public void m2(){
System.out.println("Hello m2");
}
public static void main(String args){
C obj=new C();
C.m2();
}
}

Categories