Abstract Classes and Interfaces Java
This particular assignment is doing research and programming. I want you to do a research report of the following:
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);
}
}
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...
In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a basic employee schema within a company. The Employee class will be an abstract class that will contain methods applicable to all employees. You will then create 2 classes called SoftwareEngineer and ProductManager. Both are different employee types based on occupation. You will create an interface called Developer which will consist of specific methods that apply to only Developers (e.g. SoftwareEngineer class will implement this,...
I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java Programming Project #4 – Classes and Objects (20 Points) You will create 3 new classes for this project, two will be chosen from the list below and one will be an entirely new class you invent.Here is the list: Shirt Shoe Wine Book Song Bicycle VideoGame Plant Car FootBall Boat Computer WebSite Movie Beer Pants TVShow MotorCycle Design First Create three (3) UML diagrams...
How do i write the pseudocode for this java code? First, write out pseudocode, and then create a program to help you by accomplishing the following tasks: : Use command line interface to ask the user to input the following. ○ How many apples are on hand ○ How many apples should be in stock ○ How many oranges are on hand ○ How many oranges should be in stock Perform an operation to determine how many of...