Tasks studies - laboratory
An abstract class is a class from which an object instance cannot be created. It has the following properties:
• We mark it with the abstract modifier. • The class may (but does not have to) contain both regular Java methods and those marked with the abstract modifier. • An abstract class can be extended by other Java classes - both regular and abstract. In Java, we can extend only one class at a time and the same is true for abstract classes.
Example 1:
abstract class className {
// ...
}
An abstract class can contain abstract method declarations, regular methods, constants and
final members.
abstract class Example
{
public static final double PI = 3.14; // declaration of constant
abstract String Something(); // abstract method (without body)
public void Message() // regular method
{
System.out.println("Hello World!");
}
}
To better define the meaning of abstraction in programming, let’s use an example from real life. An abstract class can be a geometric figure. We don’t know what a “figure” looks like, for us it’s a general concept, an abstract entity that we can’t imagine. However, we can define properties of this figure, e.g.: surface area, perimeter, which can be inherited by specific descendant classes (and which we can imagine, e.g.: Square, Rectangle, Triangle, etc.). An abstract class is a kind of pattern for descendant classes.
package AbstractClasses;
public abstract class Figure {
abstract double Area();
abstract double Perimeter(); }
The Figure class contains two abstract methods that return floating-point values – double. Unlike interfaces, abstract methods must specify the type of the returned values.
Descendant classes that inherit the abstract class must implement all of its abstract methods. In addition, the abstract class can also have regular methods that do not have to be implemented in descendant classes.
From the above example, we already know that descendant classes will calculate the area and perimeter of a given figure. The abstract class does not provide specifics for the calculation methods, because each figure has a different mathematical formula for calculating these unknowns. As you can see, this is a flexible approach that gives us a lot of freedom in design. Examples of descendant classes implementing the abstract Figure class:
package AbstractClasses;
public class Square extends
Figure{
public double a; public double Field()
{
return a*a;
}
public double Perimeter()
{
return 4*a;
}
}
package AbstractClasses;
public class Rectangle extends
Figure{
public double a, b;
public double Field()
{
return a*b;
}
public double Perimeter()
{
return 2*a + 2*b;
}
}
Using an abstract class and classes inheriting its members:
package AbstractClasses;
public class FigureTest {
public static void main(String[] args) {
Square kw1 = new Square();
kw1.a = 10;
System.out.println("Area square = " + kw1.Field());
System.out.println("Perimeter square = " + square1.Perimeter());
Rectangle rectangle1 = new Rectangle();
rectangle1.a = 5;
rectangle1.b = 2;
System.out.println("Area rectangle = " + rectangle1.Area());
System.out.println("Perimeter rectangle = " + rectangle1.Perimeter());
}
}
An interface is a “template” containing elements that must be used in classes that implement it.
Interfaces can only contain constants and method declarations. An interface is implemented in a given class using the implements keyword.
Interface construction:
access_modifier interface Interface_name {
// declaration of constants and/or methods
}
Interface implementation:
access_modifier class Class_name implements Interface_name {
// declaration of members and/or methods
}
Unlike inheritance, a given class can implement multiple interfaces:
access_modifier class Class_name implements Interface1, Interface2 //
etc... {
3
// class code
}
Interfaces can also inherit from multiple interfaces:
access_modifier interface Interface_name extends Interface1,
Interface2 //... {
}
Example of an interface:
package AbstractClasses;
public interface GeometricFigure {
double Area();
double Perimeter();
}
Notice that the GeometricFigure interface tells the class “what to do”, but it doesn’t tell it how to do it. We only have declarations of method names: Area() and Perimeter(). All constants and methods of the interface are public and abstract by default, so it is not necessary to write keywords before them, e.g.: public. Analyzing this simple interface, we can easily guess that it will tell the class to calculate the area and perimeter of a given geometric figure. So let’s write a Square class and introduce the appropriate formulas to it:
public class Square implements GeometricFigure {
private double a; // length of side / base
public void setA(double a) {
this.a = a;
}
public double getA() {
return a;
}
public double Pole() {
return a*a;
}
public double Perimeter() {
return 4*a;
}
}
Both methods (Area and Perimeter) return the result of the operation. Let’s add one more figure - a rectangle. The Rectangle class, similarly to Square, implements the GeometricFigure interface.
public class Rectangle implements GeometricFigure {
private double a; // length
private double h; // height
public void setA(double a) {
this.a = a;
}
public double getA() {
return a;
}
public void setH(double h) {
this.h = h;
}
public double getH() {
return h;
}
public double Field() {
return a*h;
}
public double Perimeter() {
return 2*a+2*h;
}
}
The Square and Rectangle classes must implement all the methods of the Geometric Figure interface, but there is nothing stopping them from having additional methods inside their own classes, e.g. the formula for the diagonal. Finally, let’s create a class to test our program - Geometric FigureTest:
public class Geometric FigureTest {
public static void main(String[] args)
{
Square kw1 = new Square();
kw1.setA(10);
System.out.println("Area of the square = " + kw1.Area());
System.out.println("Perimeter of the square = " + kw1.Perimeter());
Rectangle rectangle1 = new Rectangle();
rectangle1.setA(5);
rectangle1.setH(2);
System.out.println("Area of rectangle = " + rectangle1.Area());
System.out.println("Perimeter of rectangle = " + rectangle1.Perimeter());
// Will print:
// Area of square = 100.0
// Perimeter of square = 40.0
// Area of rectangle = 10.0
// Perimeter of rectangle = 14.0
}
}
Default methods in interfaces In Java, it is possible to add methods containing a body to interfaces. These methods use the keyword default and are called default methods. Since they are already implemented (they are not abstract), it is not necessary to provide their implementation in inheriting classes.
They can be overridden in those classes if necessary.
package AbstractClasses;
public interface GeometricFigure {
double Field();
double Perimeter();
package AbstractClasses;
public class GeometricFigureTest {
public static void main(String[]
args)
{
Square kw1 = new Square();
5
default String getFullName()
{
return "Interface GeometricFigure";
}
}
kw1.setA(10);
System.out.println("Area
of
square = " + kw1.Area());
System.out.println("Perimeter
of
square = " + kw1.Perimeter());
System.out.println(kw1.getFullName());
Rectangle rectangle1 = new Rectangle();
rectangle1.setA(5);
rectangle1.setH(2);
System.out.println("Area
of
rectangle = " + rectangle1.Area());
System.out.println("Perimeter
of
rectangle = " + rectangle1.Perimeter());
/*Displays
Area of a square = 100.0
Perimeter of a square = 40.0
Interface Geometric figure
Area of a rectangle = 10.0
Perimeter of a rectangle = 14.0*/
}
}
Constant values in interfaces In Java, in addition to variables, we can also introduce constant values into the code. We will talk about this in the future, but for now it is important to know that a constant is defined using a combination of two static final keywords. Additionally, the name of the constant should be written in capital letters (in the case of a name consisting of several elements - they are separated by the underscore character _):
public class MovieItem {
public static final String LABEL = "Default item name";
}
A constant value can be initialized with a value only during creation. We can do this either
immediately in the field or in the constructor. We cannot change such a value later. Assigning a new value
will result in a compilation error. public class MovieItem {
public static final String LABEL = "Default item name";
public void updateLabel() {
this.LABEL = "Default item name created by JavAPPa"; //will not
compile
}
}
What does all this have to do with interfaces? It turns out that we can also create constants in interfaces, but with the difference that they are declared automatically and implicitly. So we do not enter the words static final in the code:
package AbstractClasses;
public interface GeometricFigure {
double Pole();
double Perimeter();
public String LABEL = "My geometric figures";
default String getFullName() {
return "Interface Geometric Figure"; }
}
Implement the application using interfaces and abstract classes.
Please implement an application modeling existing means of transport used to
move both on land, water and in the air. Hint: You should create
the Fly
and Lata
interfaces (methods plyn()
and lec()
respectively). You should also define classes
implementing each of the interfaces (e.g. Ship
, Airplane
) as well as a class creating sample
objects along with calling the implemented methods.
Please implement an application that shows Animals
and their basic activities (swims
, surfaces
, submerges
, excretes
, flies
, lands
). Hint: You should create two interfaces: Swimming with the methods swim()
, surface()
, submerges()
and the Flying interface with the methods fly()
, land()
. The Animal class should be modified to be an abstract class. The Fish
class should also be abstract. The Fish
class should contain the methods eat()
and excretes()
.
The finished solution should be tested by creating a new Whale Pet and checking its operation.
all of them in the new project.
running three sample functions.
Replace the Figura
class with an abstract class
Add the abstract method String description()
to the Figure class, returning information about the object, e.g.
“Object of the Rectangle class”.
Modify the remaining classes so that the program compiles correctly.
In the main program, create an array named tabelaFigur with size 10 of the Figure type. To each element of the array create a new object, or assign an existing one.
For each object of the array call the opis()
method. From which class was the method opis() called
and why?
In the Figure class define additional abstract methods and implement them in the descendant classes:
a) void skaluj(float skala) //scales the size of the figure
```a) float getPowierzchnia();
b) boolean wPolu(Punkt p); ```
Implement the IFigury
interface in all the Rectangle
, Triangle
, Square
classes.
In the main program declare a list of IFigury type objects, then assign different
figures to it: Rectangle
, Square
, Triangle
.
For each object of the IFigur
array, call the getPowierzchnia()
and w_polu()
methods.
Create a class Okręchnia (Circle) that inherits from the Figure class
Define an interface called RuchFigury (MoveFigury), containing the following method:
void move(int x, int y);
Implement the RuchFigury interface in the Okrąg (Circle) class.
Check the interface’s operation.