Java Articles - Page 302 of 745

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

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

6K+ 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

405 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

2K+ 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

771 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

579 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

3K+ 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

717 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

How do we close resources automatically in Java?

Alshifa Hasnain
Updated on 02-May-2025 19:26:12

562 Views

In this article, we will learn to close resources automatically in Java. Resource management becomes important in Java programming to prevent memory leaks and system instability. Java provides several options for closing resources automatically: files, database connections, and network sockets. The Problem with Manual Resource Closure Traditionally, developers needed to manually close resources using try-finally blocks. This method is error-prone because it's easy to forget to close resources, and exception handling can be complicated: FileInputStream fis = null; try { fis = new FileInputStream("file.txt"); } finally { if (fis != null) { ... Read More

What are the differences between JTextField and JTextArea in Java?

Alshifa Hasnain
Updated on 11-Apr-2025 22:24:05

5K+ Views

The main difference between JTextField and JTextArea in Java is that a JTextField allows entering a single line of text in a GUI application while the JTextArea allows entering multiple lines of text in a GUI application. JTextField The following are the key characteristics of JTextField in Java: A JTextFeld is one of the most important components that allow the user to an input text value in a single line format. A JTextField will generate an ActionListener interface when we trying to enter some input inside it. The JTextComponent is a superclass of JTextField that provides a common set ... Read More

Advertisements