Understand the difference between abstract classes and interfaces!

Understand the difference between abstract classes and interfaces!

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.

Abstract class ???

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);  
   }
}

interface???

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.

What abstract classes and interfaces have in common

--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.

Differences between abstract classes and interfaces

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

Understand the difference between abstract classes and interfaces!
Understand the difference between each_with_index and each.with_index
[Java] Understand the difference between List and Set
[iOS] Understand the difference between frame and bounds
About the difference between classes and instances in Ruby
[JAVA] What is the difference between interface and abstract? ?? ??
Difference between interface and abstract class
[JAVA] Difference between abstract and interface
The difference between programming with Ruby classes and programming without it
About the difference between irb and pry
[Details] Let's master abstract classes and interfaces! !!
Easy to understand the difference between Ruby instance method and class method.
[Rails / ActiveRecord] About the difference between create and create!
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
Difference between vh and%
Understand the difference between int and Integer and BigInteger in java and float and double
Difference between i ++ and ++ i
[Ruby] I thought about the difference between each_with_index and each.with_index
[Rails] I learned about the difference between resources and resources
What is the difference between a class and a struct? ?? ??
What is the difference between System Spec and Feature Spec?
[Rails] What is the difference between redirect and render?
Compare the difference between dockerfile before and after docker-slim
Java classes and instances to understand in the figure
What is the difference between skip and pending? [RSpec]
[Rails] I investigated the difference between redirect_to and render.
What is the difference between Java EE and Jakarta EE?
[Swift] UITextField taught me the difference between nil and ""
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Java abstract methods and classes
Difference between render and redirect_to
[Java] Note about the difference between equivalence judgment and equality judgment when comparing String classes
Understand in 3 minutes! A very rough explanation of the difference between session and cookie
[Rails] What is the difference between bundle install and bundle update?
Difference between Java and JavaScript (how to find the average)
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby] About the difference between 2 dots and 3 dots of range object.
What is the difference between an action and an instance method?
[Java] Check the difference between orElse and orElseGet with IntStream
Let's override the difference between == (identity) and equals method (equivalence)
[Ruby] Difference between get and post
Difference between instance method and class method
Difference between render method and redirect_to
Difference between == operator and equals method
[Terminal] Difference between irb and pry