Interface Try to make Java problem TypeScript 7-3

** Choose the correct description for the interface. (2) **

A. Even if the access modifier is omitted, it is treated as a public method. B. No fields can be defined. C. Class cannot realize multiple interfaces at the same time. D. Interfaces cannot be inherited. E. Abstract classes do not have to implement the methods defined in the interface

The answer is ** AE **

A. Even if the access modifier is omitted, it is treated as a public method. ... Correct, you can't use protected or private for the interface. B. No fields can be defined. ... Incorrect answer, made possible by using final and static in the specifications from JAVASE8. C. Class cannot realize multiple interfaces at the same time. ... Incorrect, the java interface is there to solve inheritance problems such as c ++ diamond inheritance. Instead of the class prohibiting multiple inheritance, the interface allows it. D. Interfaces cannot be inherited. ・ ・ ・ Incorrect answer, it is possible to inherit the interface. E. Abstract classes do not have to implement the methods defined in the interface ... Correct answer, if you inherit them and enter all the instances that are finally realized, there is no problem.

Let's think about this as TypeScript.

** A. Even if the access modifier is omitted, it is treated as a public method. ** ** In js, if the access modifier is omitted, it will be treated as public in java. Since typescript is compatible with js, omitting the access modifier makes it accessible from anywhere as well.

** B. No fields can be defined. ** **

typescript.ts


interface MyInterface{
    name:string
}
class MyClass implements MyInterface{
     name:string;//If this member does not exist, a compile error will occur.
     constructor(name: string) {
          this.name = name; 
     }
}
var obj:MyClass = new MyClass("masao");
var str:string = obj.name;
console.log(str);//Display as masao

After compilation

typescript.ts


var MyClass = (function () {
    function MyClass(name) {
        this.name = name;
    }
    return MyClass;
}());
var obj = new MyClass("masao");
var str = obj.name;
console.log(str); //Display as masao

It is possible to compile normally even if you specify the field.

** C. Class cannot realize multiple interfaces at the same time. ** **

TypeScript.ts


interface MyInterface{
    name:string;
}
interface MyInterface2{
     family:string;
}
class MyClass implements MyInterface,MyInterface2{
     name:string;//If this member does not exist, a compile error will occur.
     family:string;//If this member does not exist, a compile error will occur.
     constructor(name: string,family: string) {
          this.name = name; 
          this.family = family; 
     }
}
var obj:MyClass = new MyClass("masao","yamda");
var str:string = obj.name + obj.family;
console.log(str);//Displayed as masao yamada

After compilation

javascript.js


var MyClass = (function () {
    function MyClass(name, family) {
        this.name = name;
        this.family = family;
    }
    return MyClass;
}());
var obj = new MyClass("masao", "yamda");
var str = obj.name + obj.family;
console.log(str); //Displayed as masao yamada

Even if you realize multiple interfaces, you can compile normally.

** D. Interface cannot be inherited. ** **

typeScript



interface MyInterface{
    name:string;
}
interface MyInterface2 extends MyInterface{
     family:string;
}

class MyClass implements MyInterface2{
     name:string;//If this member does not exist, a compile error will occur.
     family:string;//If this member does not exist, a compile error will occur.
     constructor(name: string,family: string) {
          this.name = name; 
          this.family = family; 
     }
}

var obj:MyClass = new MyClass("masao","yamda");
var str:string = obj.name + obj.family;
console.log(str);//Displayed as masao yamada

After compilation

JavaScript.js



var MyClass = (function () {
    function MyClass(name, family) {
        this.name = name;
        this.family = family;
    }
    return MyClass;
}());
var obj = new MyClass("masao", "yamda");
var str = obj.name + obj.family;
console.log(str); //Displayed as masao yamada

Even if the interface inherits, it will compile without any problem.

** E. The abstract class does not have to implement the methods defined in the interface ** TypeScript has no abstract classes.

Recommended Posts

Interface Try to make Java problem TypeScript 7-3
Increment behavior Try to make Java problem TypeScript 3-4
String operation Try to make Java problem TypeScript 9-3
Initialization of for Try to make Java problem TypeScript 5-4
[Java] Try to solve the Fizz Buzz problem
Java --How to make JTable
Try to solve a restricted FizzBuzz problem in Java
Declaration of multidimensional array Let's try TypeScript for Java problem 4-4
[Beginner] Try to make a simple RPG game with Java ①
Try to make a simple callback
How to make a Java container
Java, interface to start from beginner
[Java] Try to implement using generics
Try to extract java public method
Try to implement Yubaba in Java
CompletableFuture Getting Started 2 (Try to make CompletableFuture)
Try to make a peepable iterator
How to make a Java array
[Java] Try to solve the Fizz Buzz problem using recursive processing
java (interface)
[java] interface
How to make a Java calendar Summary
Try to solve Project Euler in Java
Easy to make Slack Bot in Java
Try to implement n-ary addition in Java
Make something like Java Enum with Typescript
How to make a Discord bot (Java)
[Java] Beginners want to make dating. 1st
Let's migrate to make Java more comfortable
About Java interface
Try to build Java8 environment on Amazon Linux2
I tried to make Basic authentication with Java
Try Java 8 Stream
I did Java to make (a == 1 && a == 2 && a == 3) always true
Try to create a bulletin board in Java
Try to link Ruby and Java with Dapr
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
[Java] Problem No. 2
[Java] Problem No.3
[Java] About interface
[Java] Introduction to Java
[Java] Problem No.1
Try to make a music player using Basic Player
[Java] Functional interface
About interface, java interface
Try to implement TCP / IP + NIO with JAVA
Introduction to java
How to solve an Expression Problem in Java
Easy to make LINE BOT with Java Servlet
Roughly try Java 9
[Java] How to make multiple for loops single
[Java] I tried to solve Paiza's B rank problem
How to make Java unit tests (JUnit & Mockito & PowerMock)
Try to make an addition program in several languages
I tried to make a login function in Java
Try connecting to AzureCosmosDB Emulator for Docker with Java
Try to build a Java development environment using Docker
I used to make nc (netcat) with JAVA normally
About Java functional interface
Make Blackjack in Java
Changes from Java 8 to Java 11