Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

An abstract class, although well defined, is one that is never instantiated. For

ID: 3680415 • Letter: A

Question

An abstract class, although well defined, is one that is never instantiated. For example, a pet would never be instantiated as such, but could exist in the form of a dog or a cat. The pet class would be at the top of an inheritance hierarchy where derived classes like the dog or cat class inherit their attributes and behavior from the pet class. Can you give other examples of inheritance hierarchies where the top level is an abstract class, and furthermore, what are the advantages of using such hierarchies?

Explanation / Answer

Abstract class is a special class in Java, it can not be instantiated and that's why can not be used directly. At first concept of abstraction, abstract class and interface all look useless to many developers, because we can not implement any method in interface, we can not create object of abstract class, so why do we need them. Once they face biggest constant of software development, yes that is CHANGE, they understand how abstraction at top level can help in writing flexible software. A key challenge while writing software (Java Programs, C++ programs) is not just to cater today's requirement but also to ensure that nurture requirement can be handled without any architectural or design change in our code. In short, our software must be flexible enough to support future changes. Abstract class and inheritance collectively ensures that most of the code are written using abstract and higher level classes, so that it can leverage Inheritance and Polymorphism to support future changes. This is actually one of the most useful design principle, also known as "Programming for interfaces rather than implementation". Abstract class and abstract method are two ways through which Java assist you on coding at certain level of abstraction. Capability and limitation of abstract class at later half of article, but we must understand why abstract class is important. More importantly, why we, a Java programmer, must understand motive and use of abstract class. I can say that making a class at first an interface or abstract class is a good practice, it will not only help you to test your code quickly by supplying mock objects, but also help you to write code at certain level of abstraction. In this, we would understand why we need abstract class and abstract method and learn how to use them by writing a simple example.

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class AbstractClassDemo
{
public static void main(String args[])
{
Fruit mango = new Mango(Color.YELLOW, true);
Fruit banana = new Banana(Color.YELLOW, false);

List<Fruit>platter = new ArrayList<Fruit>();
platter.add(mango);
platter.add(banana);
serve(platter);
}

public static void serve(Collection<Fruit> fruits)
{
System.out.println("Preparing fruits to serve");
   for (Fruit f : fruits) { f.prepare();
    }
    }
   }

abstract class Fruit
{
private Color color;
private boolean seasonal;
public Fruit(Color color, boolean seasonal)
{
this.color = color; this.seasonal = seasonal;
}

public abstract void prepare();
public Color getColor()
{
return color;
}
public boolean isSeasonal()
{
return seasonal;
}
}

class Mango extends Fruit
{
public Mango(Color color, boolean seasonal)
{
super(color, seasonal);
}
public void prepare()
{
System.out.println("Cut the Mango");
}

}

class Banana extends Fruit
{
public Banana(Color color, boolean seasonal)
{
super(color, seasonal);
}

public void prepare()
{
System.out.println("Peal the Banana");
}
}

Output:

Preparing fruits to serve
Cut the Mango
Peal the Banana



Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote