Question

Abstract Classes and Interfaces Java This particular assignment is doing research and programming. I want you to do a research report of the following: Ways that abstract classes promotes software re...

Abstract Classes and Interfaces Java

This particular assignment is doing research and programming. I want you to do a research report of the following:

  • Ways that abstract classes promotes software reusability, decreases time programming, and helps reduce the number of errors in software.
  • Research the using abstract classes and interfaces.
  • I would like for you to be creative and come up with your own example of abstract classes and interfaces in Java by writing your own abstract class and interface, demonstrating the abstract class and interface in a test program (let your imagination and creativity go wild!)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

We can use abstract classes when we have some classes that share some(not all) common information (or) lines of code.  

We can use interfaces when we have some classes that share all the common data or that needs to implement all the methods.

Using Abstract classes:

In the below program, Circle and Rectangle classes have the common method moveTo() and therefore this method is implemented/has body.

Note that these two classes have different implementations for area calculations and to draw on the screen. So, these methods are abstracted and needs to be implemented by Circle and Rectangle classes individually.

abstract class Shape

{

    // declare fields

    String name = " ";

      

    Shape(String name)

    {

        this.name = name;

    }

      

    // declare non-abstract methods

    // that is common code

    public void moveTo(int x, int y)

    {

        System.out.println(name + " " + "has been moved to"

                                   + " x = " + x + " and y = " + y);

    }

      

    // abstract methods which will be

    // implemented by its subclass(es)

    abstract public double area();

    abstract public void draw();

}

  

class Rectangle extends Shape

{

      

    int length, width;

      

    // constructor

    Rectangle(int length, int width, String name)

    {

          

        super(name);

        this.length = length;

        this.width = width;

    }

      

    @Override

    public void draw()

    {

        System.out.println("Rectangle has been drawn ");

    }

      

    @Override

    public double area()

    {

        return (double)(length*width);

    }

}

  

class Circle extends Shape

{

      

    double pi = 3.14;

    int radius;

      

    //constructor

    Circle(int radius, String name)

    {

          

        super(name);

        this.radius = radius;

    }

      

    @Override

    public void draw()

    {

          

        System.out.println("Circle has been drawn ");

    }

      

    @Override

    public double area()

    {

        return (double)((pi*radius*radius)/2);

    }

}

Using Interfaces:

In case you don't have common code between Cicle and Rectangle, you can use interfaces.

interface Shape

{

    // abstract method

    void draw();

    double area();

}

  

class Rectangle implements Shape

{

    int length, width;

      

    // constructor

    Rectangle(int length, int width)

    {

        this.length = length;

        this.width = width;

    }

      

    @Override

    public void draw()

    {

        System.out.println("Rectangle has been drawn ");

    }

      

    @Override

    public double area()

    {

        return (double)(length*width);

    }

}

  

class Circle implements Shape

{

      

    double pi = 3.14;

    int radius;

      

    //constructor

    Circle(int radius)

    {

          

        this.radius = radius;

    }

      

    @Override

    public void draw()

    {

        System.out.println("Circle has been drawn ");

    }

      

    @Override

    public double area()

    {

          

        return (double)((pi*radius*radius)/2);

    }

      

}

Add a comment
Know the answer?
Add Answer to:
Abstract Classes and Interfaces Java This particular assignment is doing research and programming. I want you to do a research report of the following: Ways that abstract classes promotes software re...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT