I'm sure those who use it often forget it when they try to use it. This time, let's understand the difference between abstract classes and interfaces.
First, the abstract class starts with nothing. An abstract class is simply an [unfinished class]. → I have an [Unfinished Method].
Abstract class is Alone does not play the role of Class, Since it creates a new class, it has an important meaning as a parent class (Super Class) that becomes a Basic Class.
I prepared an example. The size of the figure is an example of what you want. An abstract class called Shape defines only the abstract function of Finding the size of a shape. From now on, subclasses define how to implement them.
abstract class Shape {
abstract void draw(double a, double b);
}
class Circle extends Shape {
void draw(double r1, double r2) {
System.out.println("Circle : " + (3.14 * r1 * r2));
}
class Rectangle extends Shape {
void draw(double h, double v) {
System.out.println("Rectangle : " + (h * v));
}
class Triangle extends Shape {
void draw(double a, double h) {
System.out.println("Triangle : " + (a * h / 2));
}
public class AbstractTest
{
public static void main(String args[]) {
Circle c = new Circle();
c.draw(5.0, 5.0);
Rectangle r = new Rectangle();
r.draw(5.0, 10.0);
Triangle t = new Triangle();
t.draw(5.0, 10.0);
Shape s = new Circle();
s.draw(5.0, 5.0);
}
}
Interfaces are friends of abstract classes. It's a kind of abstract class, but it's more abstract than an abstract class. Use implements to use the interface in the class.
Also, although it is troublesome, there are various restrictions on the interface. --All member variables must be public static final. (Can be omitted) --All methods must be public abstract. (Can be omitted)
TV.java
public interface TV{
public int MAX_VOLUME = 100;
public int MIN_VOLUME = 0;
public void turnOn();
public void turnOff();
public void changeVolume(int volume);
public void changeChannel(int channel);
}
I made TV.java. You can define abstract methods and constants.
LedTV.java
public class LedTV implements TV
{
public void on(){
System.out.println("TV ON");
}
public void off(){
System.out.println("TV OFF");
}
public void volume(int value){
System.out.println(value + "volume Setting.");
}
public void channel(int number){
System.out.println(number + "channel Setting.");
}
}
Test.java
public class Test{
public static void main(String args[]){
TV tv = new LedTV();
tv.on();
tv.volume(50);
tv.channel(6);
tv.off();
}
}
If you have created an LcdTV that implements a TV interface, Even if only the new LedTV part is changed to new LcdTV, the program can operate in the same way.
--Abstract classes and interfaces have only declarations and no implementation contents. --You cannot use new to create an object yourself, only inherited children can create an object. --Use when the inherited class should ensure implementation.
interface | Abstract class |
---|---|
To guarantee the same behavior for implementation objects | To use and extend the function |
Multiple inheritance is possible | Multiple inheritance is not possible |
Only abstract methods are possible | General method+Abstract method possible |
Recommended Posts