Java Articles

Page 28 of 450

How many ways are there to initialize the instance variables of a class in java?

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

You can initialize the instance variables of a class using final methods, constructors or, Instance initialization blocks.Final methodsWhenever you make a method final, you cannot override it. i.e. you cannot provide implementation to the superclass’s final method from the subclass.i.e. The purpose of making a method final is to prevent modification of a method from outside (child class). You can also use these final methods to initialize the instance variables.Exampleimport java.util.Scanner; public class FinalMethods {    int age = getAge();    String name = getName();    static Scanner sc = new Scanner(System.in);    public static final int getAge() {   ...

Read More

What is need of introducing Generics, when Arrays already present in Java?

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

Arrays in Java are used to store homogeneous datatypes, where generics allow users to dynamically choose the type (class) that a method, constructor of a class accepts, dynamically.By defining a class generic you are making it type-safe i.e. it can act up on any datatype. To understand generics let us consider an example −Exampleclass Student{    T age;    Student(T age){       this.age = age;    }    public void display() {       System.out.println("Value of age: "+this.age);    } } public class GenericsExample {    public static void main(String args[]) {       Student std1 ...

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

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 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

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

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

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

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

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
Showing 271–280 of 4,498 articles
« Prev 1 26 27 28 29 30 450 Next »
Advertisements