Found 4338 Articles for Java 8

How abstraction is achieved using interfaces in Java?

Maruthi Krishna
Updated on 10-Sep-2019 10:28:50

7K+ Views

Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it.Since all the methods of the interface are abstract and the user doesn’t know how a method is written except the method signature/prototype. Using interfaces, you can achieve (complete) abstraction.Abstraction in interfacesAn 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 ... Read More

What are wildcards arguments in Generics In Java?

Maruthi Krishna
Updated on 21-Jan-2020 12:16:39

1K+ 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

Bounded-types in generics in Java?

Maruthi Krishna
Updated on 09-Sep-2019 09:03:42

7K+ 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. These are known as bounded-types in generics in Java.Defining bounded-types for classYou can declare a bound parameter just by extending the required class with the type-parameter, within the angular braces as −class Sample ExampleIn the following Java example the generic class Sample restricts the type parameter to the sub classes of the Number classes using ... Read More

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

Maruthi Krishna
Updated on 09-Sep-2019 08:56:43

2K+ 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 Example Live DemoIn the following Java example the generic class Sample restricts the type parameter to the sub classes of the Number classes using the bounded parameter.class Sample {    T data;    Sample(T data){ ... Read More

Can we have generic constructors in Java?

Maruthi Krishna
Updated on 09-Sep-2019 08:43:58

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 is need of introducing Generics, when Arrays already present in Java?

Maruthi Krishna
Updated on 09-Sep-2019 08:30:28

63 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 −Example Live Democlass 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 ... Read More

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

Maruthi Krishna
Updated on 09-Sep-2019 08:26:15

526 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

Where exactly Type-Parameter need to be specified, while declaring generic method in Java?

Maruthi Krishna
Updated on 09-Sep-2019 08:22:05

207 Views

Similar to generic classes you can also define generic methods in Java. These methods use their own type parameters. Just like local variables, the scope of the type parameters of the methods lies within the method.While defining a generic method you need to specify the type parameter within the angle brackets (< T >). This should be placed before the method's return type.You can have multiple type parameters separated by commas. A type parameter, also known as a type variable, is an identifier that specifies a generic type name.The type parameters can be used to declare the return type and ... Read More

What are raw types in generics in Java?

Maruthi Krishna
Updated on 09-Sep-2019 08:15:30

445 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 have multiple type parameters in generic methods in Java?

Maruthi Krishna
Updated on 09-Sep-2019 08:05:18

9K+ 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

Advertisements