Can you give an example of when we would use an abstract cla
Can you give an example of when we would use an abstract class with a class name, attributes and behaviors?
An abstract class defines a common set of public methods and properties for the various members of a class hierarchy. An abstract class typically contains one or more abstract methods and properties that derived classes will override. All classes in the hierarchy can use this common set of public methods and properties.
Question: Is it mandatory for abstract classes to contain abstract methods? If not, why would you create an abstract class with no abstract methods?
Solution
Shape.java
public abstract class Shape {
//Declaring variable
private int sides;
//Setter and getter methods
public int getSides() {
return sides;
}
public void setSides(int sides) {
this.sides = sides;
}
public abstract void calcArea();
//toString() method which is used to display the contents of an object
@Override
public String toString() {
System.out.println(\"___________________________\");
return \"This Shape has \"+ sides +\" sides \";
}
}
_____________________________________________
Square.java
public class Square extends Shape{
//Declaring instance variable
private double sidelen=0.0;
double area;
//parameterized Constructor
public Square(double sidelen,int sides) {
super();
setSides(sides);
this.sidelen=sidelen;
}
/* Here in this class as we are extends an Abstract class
* Shape we must have to over ride the calcArea()
* method in its subclasses
*/
@Override
public void calcArea() {
//Calculating the area of the square
area=sidelen*sidelen;
}
//toString() method is used to display the contents of an object
@Override
public String toString() {
System.out.println(super.toString());
this.calcArea();
return \"The Shape is Square\ Length of Side=\" +sidelen+\"\ Area=\"+area;
}
}
______________________________________________
Triangle.java
public class Triangle extends Shape {
// Declaring instance variables
private double base = 0.0;
private double height = 0.0;
double area;
// parameterized constructor
public Triangle(double base, double height, int sides) {
super();
setSides(sides);
this.base = base;
this.height = height;
}
/*
* Here in this class as we are extends an Abstract class Shape we must have
* to over ride the calcArea() method in its subclasses
*/
@Override
public void calcArea() {
//Calculating the area of the Triangle
area = 0.5 * base * height;
}
//toString() method is used to display the contents of an object
@Override
public String toString() {
System.out.println(super.toString());
this.calcArea();
return \"The Shape is Triangle\ Base=\" + base + \"\ Height=\" + height
+ \"\ Area=\" + area;
}
}
_____________________________________________
Demo.java
public class Demo {
public static void main(String[] args) {
//Creating an Triangle class Object by passing arguments
Triangle t=new Triangle(5, 6, 3);
//Calling the toString() method on Triangle Object
System.out.println(t.toString());
//Creating an Square class Object by passing arguments
Square sq=new Square(5,2);
//Calling the toString() method on Square Object
System.out.println(sq.toString());
}
}
________________________________________________
Output:
___________________________
This Shape has 3 sides
The Shape is Triangle
Base=5.0
Height=6.0
Area=15.0
___________________________
This Shape has 2 sides
The Shape is Square
Length of Side=5.0
Area=25.0
________________________________________________
Explanation:
Here in this example Shapes is an Abstract class.which has some properties and methods which are concrete(Having method body) and has area() method which is abstract(Does\'nt have any method body).
If we want to group the common propeties and methods which were used by all other class in our project then we have to create an abstract class and provide common properties and common methods which were shared by all other classes in our project.And If we want a method must have different body in different classes but must have that method in every other classes in our project then we have to declare that method as abstract and must be place in abstract class.here in this manner we are declaring abstract method area() and provided in Shapes class.
If a class is having an abstract method then we must have to declare that class as abstract.
We have to provide the body of that abstract method in its subclasses by extending the Abstract class.
here we are providing the body of abstract method area() in Traingle and Square classes by extending the Abstract class Shape.
____________________________________________
Answer for the Question:
We can declare a class as abstract even if it is not having any abstract methods.Because we want the remaining other classes in our project must extends this Abstract class.The purpose of this is ,in future if we want to add a new common feature to all other classes in our project and that common feature is having different method body then simplywe can declare an abstract method inside abstract class.Or In future if we want to share any properties information to all other classes in our project ,instead of writing the code in each and every class.Simply we can write in one place which is in abstract class.That that common information will be shared by all other classes. As all other classes are extending the abstract class.
If we wan to add any concrete method which will be used my all other classes in our project.Then instead of writing the same method with same method body in all classes ,simply we can write that method in abstract class.
These are the main advantages of abstract class which is not having any abstract methods in it.
_________________________________Thank You



