Maruthi Krishna has Published 951 Articles

How to write a class inside an interface in Java?

Maruthi Krishna

Maruthi Krishna

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

282 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 ... Read More

Can we declare an interface as final in java?

Maruthi Krishna

Maruthi Krishna

Updated on 29-Jun-2020 14:33:25

4K+ 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 ... Read More

Can we define constructor inside an interface in java?

Maruthi Krishna

Maruthi Krishna

Updated on 29-Jun-2020 14:31:53

6K+ Views

No, you cannot have a constructor within an interface in Java.You can have only public, static, final variables and, public, abstract, methods as of Java7.From Java8 onwards interfaces allow default methods and static methods.From Java9 onwards interfaces allow private and private static methods.Moreover, all the methods you define (except above ... Read More

What happens if a class does not implement all the abstract methods of an interface in java?

Maruthi Krishna

Maruthi Krishna

Updated on 29-Jun-2020 14:31:01

2K+ Views

Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.In a separate class you need to implement this interface and provide body for all its abstract methods.Once you implement an interface using a class, you must provide body (implement) to ... Read More

Accessing variables of two interfaces, which are same from an implementing class in java?

Maruthi Krishna

Maruthi Krishna

Updated on 29-Jun-2020 14:29:24

709 Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.You can implement multiple interfaces using a single class in Java. Whenever both interfaces have same name, since all the fields of an interface are static by default, you can ... Read More

Can abstract method declaration include throws clause in java?

Maruthi Krishna

Maruthi Krishna

Updated on 29-Jun-2020 14:28:41

2K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();Yes, you can throw and exception from an abstract class.ExampleIn the following Java program, we have a two classes: one abstract class ... Read More

Can the abstract methods of an interface throw an exception in java?

Maruthi Krishna

Maruthi Krishna

Updated on 29-Jun-2020 14:27:42

2K+ Views

Yes, the abstract methods of an interface can throw an exception.ExampleIn the following example the interface (MyInterface) contains an abstract method with name display, which throws an IOException.import java.io.IOException; abstract interface MyInterface {    public abstract void display()throws IOException ; }Rules to be followedYou need to follow the rules given ... Read More

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

Maruthi Krishna

Maruthi Krishna

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

423 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 ... Read More

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

Maruthi Krishna

Maruthi Krishna

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

531 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 ... Read More

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

Maruthi Krishna

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 ... Read More

Advertisements