Home> Java> javaTutorial> body text

Why do interfaces not have constructors in Java, but abstract classes have constructors?

PHPz
Release: 2023-09-13 18:09:03
forward
1473 people have browsed it

Why do interfaces not have constructors in Java, but abstract classes have constructors?

Constructoris used to initialize non-static members of a specific class relative to the object.

Constructor in the interface

  • Interfacein Java does not have aconstructor, because all data members in the interface are by defaultpublic static final, they are constants (assigned at declaration time).
  • There are no data members in the interface that can be initialized through the constructor.
  • In order to call a method, we need an object. Because the method in the interface has no body, there is no need to call the method in the interface.
  • Since we cannot call methods in the interface, there is no need to create an object for the interface and there is no need to include a constructor in it.

Example 1

interface Addition { int add(int i, int j); } public class Test implements Addition { public int add(int i, int j) { int k = i+j; return k; } public static void main(String args[]) { Test t = new Test(); System.out.println("k value is:" + t.add(10,20)); } }
Copy after login

Output

k value is:30
Copy after login

Constructor in class

  • ClassThe purpose of the constructoris to initialize fields, but not to build objects.
  • When we try to create a new instance of abstract super class, the compiler gives an error.
  • However, we can inherit an abstract class and use its constructor to control it by setting its variables.

Example 2

abstract class Employee { public String empName; abstract double calcSalary(); Employee(String name) { this.empName = name; // Constructor of abstract class } } class Manager extends Employee { Manager(String name) { super(name); // setting the name in the constructor of subclass } double calcSalary() { return 50000; } } public class Test { public static void main(String args[]) { Employee e = new Manager("Adithya"); System.out.println("Manager Name is:" + e.empName); System.out.println("Salary is:" + e.calcSalary()); } }
Copy after login

Output

Manager Name is:Adithya Salary is:50000.0
Copy after login

The above is the detailed content of Why do interfaces not have constructors in Java, but abstract classes have constructors?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!