Found 4338 Articles for Java 8

How to find the age when date of birth is known? Using Java?

Narasimha Murthi
Updated on 29-Jun-2020 14:41:59

4K+ Views

Java provides a class named Period in the java.time package. This is used to calculate the time period between two given dates as days, months and, years etc.The between() method of this class accepts two LocalDate objects and finds out the period between the two given dates (number of years, months, and days) and returns as a period object.From this, you can extract the number of days, months and, years of the period in between using the getDays(), getMonths() and, getYears() respectively.Finding the ageIf you already have the date of birth of a person, to find the age −Get the ... Read More

How to format a string to date in as dd-MM-yyyy using java?

Maruthi Krishna
Updated on 29-Jun-2020 14:42:48

15K+ Views

The java.text package provides a class named SimpleDateFormat which is used to format and parse dates in required manner (local).Using the methods of this class you can parse String to Date or, format Date to String.Parsing String to DateYou can parse a given String to Date object using the parse() method of the SimpleDateFormat class. To this method you need to pass the Date in String format. To parse a String to Date object −Instantiate the SimpleDateFormat class by passing the required pattern of the date in String format to its constructor.//Instantiating the SimpleDateFormat class SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");Parse/convert ... Read More

What is a remote interface in java?

Maruthi Krishna
Updated on 29-Jun-2020 14:43:24

3K+ Views

A Remote interface is available in the java.rmi package it is a marking/tagging interface, it is used with remote method invocation(RMI).RMI is a mechanism that allows an object residing in one system (JVM) to access/invoke an object running on another JVM.To it is a marking interface, to mark an object of a class remote, you need to implement this interface.To create a remote interface −Create an interface that extends the predefined interface Remote which belongs to the package or, implement the Remote interface with the class, which you need to make remote.Declare all the business methods that can be invoked ... Read More

Can we write an interface without any methods in java?

Maruthi Krishna
Updated on 29-Jun-2020 14:44:24

2K+ Views

Yes, you can write an interface without any methods. These are known as marking interfaces or, tagging interfaces.A marker interface i.e. it does not contain any methods or fields by implementing these interfaces a class will exhibit a special behavior with respect to the interface implemented.ExampleConsider the following example, here we have class with name Student which implements the marking interface Cloneable. In the main method we are trying to create an object of the Student class and clone it using the clone() method. Live Demoimport java.util.Scanner; public class Student implements Cloneable {    int age;    String name;    public ... Read More

Can we declare an interface with in another interface in java?

Maruthi Krishna
Updated on 22-Nov-2023 12:56:18

904 Views

An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface. To create an object of this type you need to implement this interface, provide body for all the abstract methods of the interface and obtain the object of the implementing class. Nested Interfaces Java allows declaring interfaces within another interface, these are known as nested interfaces. While implementing you need to refer to the nested interface as outerInterface.nestedInterface. Example In the following Java example, we have an interface with ... Read More

How to write/declare an interface inside a class in Java?

Maruthi Krishna
Updated on 29-Jun-2020 14:45:53

738 Views

An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface.To create an object of this type you need to implement this interface, provide body for all the abstract methods of the interface and obtain the object of the implementing class.Nested interfacesJava allows writing/declaring interfaces within another interface or, within a class these are known as nested interfaces.ExampleIn the following Java example, we have a class with name Sample which contains a nested interface ... Read More

How to write a class inside an interface in Java?

Maruthi Krishna
Updated on 29-Jun-2020 14:34:08

283 Views

Defining a class within an interface is allowed in Java. If the methods of an interface accept a class as an argument and the class is not used elsewhere, in such cases we can define a class inside an interface.ExampleIn the following example we have an interface with name CarRentalServices and this interface has two methods that accepts an object of the class Car as an argument. Within this interface we have the class Car. Live Demointerface CarRentalServices {    void lendCar(Car c);    void collectCar(Car c);    public class Car{       int carId;       String carModel; ... Read More

Can we implement one interface from another in java?

Maruthi Krishna
Updated on 29-Jun-2020 14:35:20

337 Views

No, we cannot implement one interface from another you can just extend it using the extends keyword as −interface ArithmeticCalculations{    public abstract int addition(int a, int b);    public abstract int subtraction(int a, int b); } interface MathCalculations implements ArithmeticCalculations{    public abstract double squareRoot(int a);    public abstract double powerOf(int a, int b); }Still, if you try to implement one interface from another using the implements keyword. The compiler does not recognize the implements keyword after the name of the interface and throws a compile time error saying “'{' expected”.ExampleIn the following Java program, we have two interfaces ... Read More

Can we declare the variables of a Java interface private and protected?

Maruthi Krishna
Updated on 29-Jun-2020 14:37:41

3K+ Views

Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Private fields of an interfaceIf the fields of the interface are private, you cannot access them in the implementing class.If you try to declare the fields of an interface private, a compile time error is generated saying “modifier private not allowed here”.ExampleIn the following Java example, we are trying to declare the field and method of an interface private.public interface MyInterface{    private static final int num = 10;    private abstract void demo(); }Compile time errorOn compiling, the above program ... Read More

Explain the importance of import keyword in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

439 Views

In Java classes and interfaces related to each other are grouped under a package. Package is nothing but a directory storing classes and interfaces of a particular concept. For example, all the classes and interfaces related to input and output operations are stored in java.io package.There are two types of packages namely user-defined packages and built-in packages (pre-defined)The import keywordWhenever you need to use the classes from a particular package −First of all, you need to set class path for the JAR file holding the required package.Import the required class from the package using the import keyword. While importing you ... Read More

Advertisements