Object Oriented Programming Articles

Page 213 of 589

How to access the fields of an interface in Java?

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

An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface. By default, All the members (methods and fields) of an interface are public.All the methods in an interface are public and abstract (except static and default).All the fields of an interface are public, static and, final by default.If you declare/define fields without public or, static or, final or, all the three modifiers. Java compiler places them on your behalf.ExampleIn the following Java program, ...

Read More

Is it possible to instantiate Type-parameter in Java?

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

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.Exampleclass Student {    T age;    Student(T age){       this.age = age;    }    public void display() {       System.out.println("Value of age: "+this.age);    } } public class GenericsExample { ...

Read More

Can we synchronize abstract methods in Java?

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

An abstract method is the one that does not have a body. It contains only a 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 (body) to it. If a class contains at least one abstract method, you must declare it abstract.Exampleimport java.io.IOException; abstract class MyClass {    public abstract void display(); } public class AbstractClassExample extends MyClass{    public void display(){       System.out.println("subclass implementation of the display method");    }    public static void main(String args[]) ...

Read More

Is it mandatory to use T for type-parameter, while defining Generics classes/methods in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 757 Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.To define a generic class you need to specify the type parameter you are using in the angle brackets “” after the class name and you can treat this as datatype of the instance variable an ...

Read More

Can we change the access specifier from (public) while implementing methods from interface in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 728 Views

An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface.To create an object of this type you need to implement this interface, provide a body for all the abstract methods of the interface and obtain the object of the implementing class.All the methods of the interface are public and abstract and, we will define an interface using the interface keyword as shown below −interface MyInterface{    public void display();    public void setName(String ...

Read More

Can we have generic constructors in Java?

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

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.To define a generic class you need to specify the type parameter you are using in the angle brackets “” after the class name and you can treat this as datatype of the instance variable an ...

Read More

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

How can we restrict Generics (type parameter) to sub classes of a particular class in Java?

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

Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class.You can declare a bound parameter just by extending the required class with the type-parameter, within the angular braces as −class Sample Examplepublic class BoundsExample {    public static void main(String args[]) {       Sample obj1 = new Sample(20);       obj1.display();       Sample obj2 = new Sample(20.22d);       ...

Read More

How to hide unsupported interface methods from class in Java?

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

Actually you can’t, once you implement an interface it is a must to provide implementation to all its methods or, make the class abstract. There is no way to skip methods of an interface without implementing (unless they are default methods). Still, if you try to skip implementing methods of an interface a compile-time error would be generated.Exampleinterface MyInterface{    public static int num = 100;    public void sample();    public void getDetails();    public void setNumber(int num);    public void setString(String data); } public class InterfaceExample implements MyInterface{    public static int num = 10000;    public void ...

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
Showing 2121–2130 of 5,881 articles
« Prev 1 211 212 213 214 215 589 Next »
Advertisements