Found 7442 Articles for Java

Default method vs static method in an interface in Java?

Maruthi Krishna
Updated on 02-Jul-2020 10:22:11

9K+ Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Since Java8 static methods and default methods are introduced in interfaces.Default Methods - Unlike other abstract methods these are the methods can have a default implementation. If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface.In short, you can access the default methods of an interface using the objects of the implementing classes.Exampleinterface MyInterface{    public static int num = 100;    public default void ... Read More

Does java support hybrid inheritance?

Maruthi Krishna
Updated on 02-Jul-2020 10:01:48

2K+ Views

Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −public class A extends B{ }The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.In inheritance a copy of super class members are created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.Exampleclass Super{    int a =100;    int b = 200;    public void ... Read More

How to make an object completely encapsulated in java?

Maruthi Krishna
Updated on 02-Jul-2020 10:02:51

355 Views

The process of wrapping up the data and, the code acting on the data together is known as encapsulation. This is a protective mechanism where we hide the data of one class from (an object of) another.Since, variables hold the data of a class to encapsulate a class you need to declare the required variables (that you want to hide) private and provide public methods to access (read/write) them.By doing so, you can access the variables only in the current class, they will be hidden from other classes, and can be accessed only through the provided methods. Therefore, it is ... Read More

Is it mandatory to override the default methods of an interface in Java?

Maruthi Krishna
Updated on 01-Aug-2019 11:12:37

3K+ Views

The default methods are introduced in an interface since Java8. Unlike other abstract methods these are the methods can have a default implementation. If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface.In short, you can access the default methods of an interface using the objects of the implementing classes.Exampleinterface MyInterface{    public static int num = 100;    public default void display() {       System.out.println("display method of MyInterface");    } } public class InterfaceExample implements MyInterface{    public static void main(String ... Read More

What is diamond problem in case of multiple inheritance in java?

Maruthi Krishna
Updated on 02-Jul-2020 10:04:18

13K+ Views

Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −public class A extends B{ }The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.In inheritance a copy of super class members is created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.Multiple inheritanceThere are various types of inheritance available namely single, multilevel, hierarchical, multiple ... Read More

Can Enum implements an interface in Java?

Vivek Verma
Updated on 29-May-2025 17:58:20

13K+ Views

Yes, an Enum implements an Interface in Java. It can be useful when we need to implement business logic that is tightly coupled with a specific property of a given object or class. Before implementing the interface with an enum, let's discuss enums and interfaces in Java with a code snippet for better understanding. Enum in Java In Java, an Enum (i.e., an enumeration) is a special data type added in Java version 1.5. Enums are constants by default; the names of an enum type's fields are in uppercase letters. In the Java programming language, you can define an Enum ... Read More

How can we implement a moving text using a JLabel in Java?

raja
Updated on 10-Feb-2020 13:34:34

2K+ Views

A JLabel is a subclass of JComponent class and an object of JLabel provides text instructions or information on a GUI. A JLabel can display a single line of read-only text, an image or both text and an image. A JLabel can explicitly generate a PropertyChangeListener interface. We can also implement a moving text in a JLabel by using a Timer class, it can set a timer with speed(in milliseconds) and this as an argument.Exampleimport java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.Timer; public class MovingTextLabel extends JFrame implements ActionListener {    private JLabel label;    public MovingTextLabel() {       setTitle("MovingTextLabel");       label= new JLabel(" ... Read More

What can cause the \"cannot find symbol\" error in Java?

Vivek Verma
Updated on 29-May-2025 05:39:49

12K+ Views

In Java, the cannot find symbol error occurs when we try to reference a variable that is not declared in the program or when we try to access a class that is not declared or imported.  Possible Causes of 'Cannot Find Symbol' Error Below is a list of some possible causes of the 'cannot find symbol' error in Java - Using a variable that is not declared or outside the code. Using wrong cases ("tutorials" and "Tutorials" are different) or making spelling mistakes. The packaged class has ... Read More

When can we call wait() and wait(long) methods of a Thread in Java?

Vivek Verma
Updated on 29-May-2025 17:52:43

637 Views

This article will discuss the wait() and wait(long timeout) methods, along with a suitable example that explains when to call these methods in a thread in Java. The wait() Method In Java, the wait() method is used in multithreading and causes the current thread to "wait" until another thread calls notify() or notifyAll() on the same object. When the wait() method is called, the thread moves from the running to the waiting state and resumes only when notified, if not a deadlock will be formed. Note: Call the wait() method only when the thread should wait without any time limit. Following ... Read More

What is the importance of Runtime class in Java?

raja
Updated on 22-Nov-2023 09:53:24

1K+ Views

The java.lang.Runtime class is a subclass of Object class, can provide access to various information about the environment in which a program is running. The Java run-time environment creates a single instance of this class that is associated with a program. The Runtime class does not have any public constructors, so a program cannot create its own instances of the class. A program must call the getRuntime() method to get a reference to the current Runtime object. The important methods of Runtime class are addShutdownHook(), exec(), exit(), freeMemory(), gc(), halt() and load(). Syntax public class Runtime extends Object Example public class RuntimeTest { ... Read More

Advertisements