Object Oriented Programming Articles

Page 256 of 589

What are defender methods or virtual methods in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 779 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

Can interfaces have Static methods in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 8K+ 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

Can we create an object for an interface in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 17K+ 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.Exampleinterface MyInterface{    public static final int num = ...

Read More

Can abstract method declaration include throws clause in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 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 with name MyClass which have an abstract method (display()) and, another class that extends the abstract class.Here, the display() method abstract class throws an IOException.Exampleimport java.io.IOException; abstract class MyClass {    public abstract void display() throws IOException; } public class AbstractClassExample extends MyClass{    public void display() throws IOException { ...

Read More

What happens if the subclass does not override abstract methods in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 6K+ 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();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.A class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.Extending an abstract classOnce you extend an abstract class in Java you need to override all the abstractmethods in it or, declare it abstract. If you don’t, a ...

Read More

How to write a class inside an interface in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 399 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 and this interface has two methods that accepts an object of the class Car as an argument. Within this interface we have the class Car.interface CarRentalServices {    void lendCar(Car c);    void collectCar(Car c);    public class Car{       int carId;       String carModel;   ...

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 11-Mar-2026 633 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 the abstract methods of an interface throw an exception in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 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 below while implementing such method −If the abstract method in the interface throws certain exception. The implemented method can throw the same exception as shown below −Example 1import java.io.IOException; abstract interface MyInterface {    public abstract void display()throws IOException ; } public class InterfaceExample implements MyInterface{    public void display()throws ...

Read More

Can we write an interface without any methods in java?

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

Yes, you can write an interface without any methods. These are known as marking interfaces or, tagging interfaces.A marker interface i.e. it does not contain any methods or fields by implementing these interfaces a class will exhibit a special behavior with respect to the interface implemented.ExampleConsider the following example, here we have class with name Student which implements the marking interface Cloneable. In the main method we are trying to create an object of the Student class and clone it using the clone() method.import java.util.Scanner; public class Student implements Cloneable {    int age;    String name;    public Student ...

Read More

How to convert a Date object to LocalDate object in java?

Narasimha Murthi
Narasimha Murthi
Updated on 11-Mar-2026 1K+ Views

To convert a Date object to LocalDate object in Java −Convert the obtained date object to an Instant object using the toInstant() method.Instant instant = date.toInstant();Create the ZonedDateTime object using the atZone() method of the Instant class.ZonedDateTime zone = instant.atZone(ZoneId.systemDefault());Finally, convert the ZonedDateTime object to the LocalDate object using the toLocalDate() method.LocalDate givenDate = zone.toLocalDate();ExampleFollowing example accepts a name and Date of birth from the user in String formats and converts it to LocalDate object and prints it.import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Date; import java.util.Scanner; public class DateToLocalDate {    public static void main(String ...

Read More
Showing 2551–2560 of 5,881 articles
« Prev 1 254 255 256 257 258 589 Next »
Advertisements