Found 4338 Articles for Java 8

Can we create an object for an interface in java?

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

16K+ Views

No, you cannot instantiate an interface. Generally, it contains abstract methods (except default and static methods introduced in Java8), which are incomplete.Still if you try to instantiate an interface, a compile time error will be generated saying “MyInterface is abstract; cannot be instantiated”.In the following example we an interface with name MyInterface and a class with name InterfaceExample.In the interface we have an integer filed (public, static and, final) num and abstract method demo().From the class we are trying to − create an object of the interface and print the num value.Example Live Demointerface MyInterface{    public static final int num ... Read More

Can we declare interface members as private or protected in java8?

Maruthi Krishna
Updated on 04-Feb-2022 06:05:47

4K+ Views

Interface in Java is similar to a class but, it contains only abstract methods and fields which are final and static.Since all the methods are abstract you cannot instantiate it. To use it, you need to implement this interface using a class and provide body to all the abstract methods in it.Private members of an interfaceIf the members of the interface are private you cannot provide implementation to the methods or, cannot access the fields of it in the implementing class.Therefore, the members of an interface cannot be private. If you try to declare the members of an interface private, ... Read More

What are Transient variables in Java? Explain.

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

285 Views

In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI).While serializing an object of a class, if you want JVM to neglect a particular instance variable you need can declare it transient.public transient int limit = 55; // will not persist public int b; // will persistIn the following java program, the class Student has two instance variables name and age where, age is declared transient. In another class named EampleSerialize we are trying to ... Read More

What happens if we does not initialize variables of an interface in java?

Maruthi Krishna
Updated on 29-Jun-2020 14:16:56

1K+ Views

In Java final is the access modifier which can be used with a filed class and a method.When a method if final it cannot be overridden.When a variable is final its value cannot be modified further.When a class is finale it cannot be extended.Declaring final variable without initializationIf you declare a variable as final, it is mandatory to initialize it before the end of the constructor. If you don’t you will get a compilation error.ExampleIn the following java program, we a have an interface with a public, static, final variable with name num and, a public, abstract method with name ... Read More

Guidelines to follow in while overriding a method that throws an exception in java?

Maruthi Krishna
Updated on 29-Jun-2020 14:22:28

533 Views

While a superclass method throws an exception while overriding it you need to follow the certain rules.Should throw Same exception or, sub typeIf the super-class method throws certain exception, the method in the sub-class should throw the same exception or its sub type.ExampleIn the following example, the readFile() method of the super-class throws an IOEXception and, the readFile() method of the sub-class throws FileNotFoundException exception.Since the FileNotFoundException exception is the sub type of the IOException this program gets compiled and executed without any errors.import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super{    public String readFile(String path) throws ... Read More

Can we change an exception of a method with throws clause from unchecked to checked while overriding it in java?

Maruthi Krishna
Updated on 29-Jun-2020 14:23:57

424 Views

A checked exception is an exception that occurs at the compile time, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation; the programmer should take care of (handle) these exceptions.An unchecked exception is an exception that occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.Unchecked to checkedWhen a method in superclass throws an unchecked exception the method the subclass method overriding cannot throw ... Read More

Can we create non static variables in an interface using java?

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

2K+ Views

Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Since all the methods are abstract you cannot instantiate it. To use it, you need to implement this interface using a class and provide body to all the abstract methods int it.Non static variables in an interfaceNo you cannot have non-static variables in an interface. By default, All the members (methods and fields) of an interface are publicAll the methods in an interface are public and abstract (except static and default).All the fields of an interface are public, static and, final ... Read More

Can interfaces have Static methods in Java?

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

7K+ Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.A static method is declared using the static keyword and it will be loaded into the memory along with the class. You can access static methods using class name without instantiation.Static methods in an interface since java8Since Java8 you can have static methods in an interface (with body). You need to call them using the name of the interface, just like static methods of a class.ExampleIn the following example, we are defining a static method in an interface and accessing ... Read More

What are defender methods or virtual methods in Java?

Maruthi Krishna
Updated on 29-Jun-2020 14:10:49

500 Views

The default methods in an interface in Java are also known as defender methods or, virtual methods.The defender/virtual methods are those that will have a default implementation in an interface. You can define defender/virtual methods using the default keyword as −default void display() {    System.out.println("This is a default method"); }There is no need to implement these defender/virtual methods in the implementing classes you can call them directly.If you have an interface which is implemented by some classes and if you want to add a new method int it.Then, you need to implement this newly added method in all the ... Read More

What happens if we define a concrete method in an interface in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:44:28

2K+ Views

Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Since all the methods are abstract you cannot instantiate it. To use it, you need to implement this interface using a class and provide body to all the abstract methods int it.Concrete methods in an interfaceAll the methods in an interface must be abstract, you cannot have a concrete method (the one which has body) if you try to do so, it gives you a compile time error saying “interface abstract methods cannot have body”.ExampleIn the following Java program, we are ... Read More

Advertisements