Java Articles

Page 28 of 450

What happens if we overload default methods of an interface in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ Views

An interface in Java is similar to a 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 that can have a default implementation. If you have a 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; ...

Read More

Can we overload methods of an interface in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 8K+ Views

Polymorphism is the ability of an object to perform different actions (or, exhibit different behaviors) based on the context.Overloading is one of the mechanisms to achieve polymorphism where a class contains two methods with the same name and different parameters.Whenever you call this method the method body will be bound with the method call based on the parameters.ExampleIn the following Java program, the Calculator class has two methods with name addition the only difference is that one contains 3 parameters and the other contains 2 parameters.Here, we can call the addition method by passing two integers or three integers. Based ...

Read More

How to override only few methods of interface in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 8K+ Views

Once you implement an interface from a concrete class you need to provide implementation to all its methods. If you try to skip implementing methods of an interface at compile time an error would be generated.Exampleinterface MyInterface{    public void sample();    public void display(); } public class InterfaceExample implements MyInterface{    public void sample(){       System.out.println("Implementation of the sample method");    }    public static void main(String args[]) {       InterfaceExample obj = new InterfaceExample();       obj.sample();    } }Compile-time errorInterfaceExample.java:5: error: InterfaceExample is not abstract and does not override abstract method display() ...

Read More

Is it possible to override a Java method of one class in same?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 3K+ Views

When we have two classes where one extends another and if, these two classes have the same method including parameters and return type (say, sample) the method in the subclass overrides the method in the superclass.i.e. Since it is an inheritance. If we instantiate the subclass a copy of superclass’s members is created in the subclass object and, thus both methods are available to the object of the subclass.But if you call the method (sample), the sampling method of the subclass will be executed overriding the super class’s method.Exampleclass Super{    public static void sample(){       System.out.println("Method of ...

Read More

When overriding clone method, why do we need to declare it as public in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ Views

The clone() method belongs to the class named Object of the java.lang package, it accepts an object as a parameter creates and returns a copy of it.In order to use this method, you need to make sure that your class implements the Cloneable (marker) interface.Examplepublic class CloneExample implements Cloneable {    private String name;    private int age;    public CloneExample(String name, int age){       this.name = name;       this.age = age;    }    public void displayData(){       System.out.println("Name : "+this.name);       System.out.println("Age : "+this.age);    }    public static void ...

Read More

What happens when a subclass object is assigned to a superclass object in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 6K+ Views

Converting one data type to others in Java is known as casting.If you convert a higher datatype to lower datatype, it is known as narrowing (assigning higher data type value to the lower data type variable).char ch = (char)5;If you convert a lower data type to a higher data type, it is known as widening (assigning lower data type value to the higher data type variable).Int i = 'c';Similarly, you can also cast/convert an object of one class type to others. But these two classes should be in an inheritance relation. Then, If you convert a Super class to subclass ...

Read More

Why subclass doesn't inherit the private instance variables of superclass in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 6K+ Views

When you declare the instance variables of a class private, you cannot access them in another class if you try to do so a compile-time error will be generated.But, if you inherit a class that has private fields, including all other members of the class the private variables are also inherited and available for the subclass.But, you cannot access them directly, if you do so a compile-time error will be generated.Exampleclass Person{    private String name;    public Person(String name){       this.name = name;    }    public void displayPerson() {       System.out.println("Data of the Person ...

Read More

How to find the Strings within a text file in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 8K+ Views

Following Java program accepts a String value from the user, verifies whether a file contains the given String and prints the number of occurrences of the word too.Exampleimport java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class FindingWordFromFile {    public static void main(String args[]) throws FileNotFoundException {       //Reading the word to be found from the user       Scanner sc1 = new Scanner(System.in);       System.out.println("Enter the word to be found");       String word = sc1.next();       boolean flag = false;       int count = 0;       System.out.println("Contents ...

Read More

Exception Hierarchy in case of multiple catch blocks.

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 6K+ Views

An exception is an issue (run time error) that occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.Multiple exceptions in a codeWhenever we have a code that may generate more than one exception and if you need to handle them specifically you can use multiple catch blocks on a single try.try{    //code } catch(Exception1 ex1) {    // } catch(Exception2 ex2) {    // }Exampleimport java.util.Arrays; import java.util.Scanner; public class MultipleCatchBlocks {    public static void main(String [] args) ...

Read More

How to read/write data from/to .properties file in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 12K+ Views

The .properties is an extension in java which is used to store configurable application. It is represented by the Properties class in Java, you can store a properties file and read from it using the methods of this class. This class inherits the HashTable class.Creating a .properties file −To create a properties file −Instantiate the Properties class.Populate the created Properties object using the put() method.Instantiate the FileOutputStream class by passing the path to store the file, as a parameter.ExampleThe Following Java program creates a properties file in the path D:/ExampleDirectory/import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class CreatingPropertiesFile {   ...

Read More
Showing 271–280 of 4,498 articles
« Prev 1 26 27 28 29 30 450 Next »
Advertisements