All Topics

More from Java - 2



Interface:-

Another way to achieve abstraction in Java, is with interfaces.
An interface is a completely "abstract class" that is used to group related methods with empty bodies
To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with the implements keyword (instead of extends)
. The body of the interface method is provided by the "implement" class



Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to create an "Animal" object in the MyMainClass)
Interface methods do not have a body - the body is provided by the "implement" class
On implementation of an interface, you must override all of its methods
Interface methods are by default abstract and public
Interface attributes are by default public, static and final
An interface cannot contain a constructor (as it cannot be used to create objects)
We can even implement multiple interfaces by separating them with comma.

User Input:-

The Scanner class is used to get user input, and it is found in the java.util package.
To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation.
In our example, we will use the nextLine() method, which is used to read Strings



In the example above, we used the nextLine() method, which is used to read Strings.
To read other types, look at the table below

Methods Reads..
nextByte() byte datatype
nextShort() short datatype
nextLong() long datatype
nextInt() int datatype
nextFloat() float datatype
nextDouble() double datatype
nextBoolean() boolean datatype
nextLine() char(string) datatype



Hello