Java for beginners - Chapter 28 : Generics in Java | Generic method and generic class

preview_player
Показать описание
Generics in Java | Generic method and generic class

What is Generics ?
Generic class and Generic Method.

Generics allow you to customize a "generic" method or class to whatever type we are working with.
A class that can refer to any type is known as generic class.
Example :
Suppose we have a method that adds two numbers together.
But if we need to add two numbers of different types then we need to create ,separate add methods for different types.

public int Add(int a, int b)

public double Add(double a, double b)

public float Add(float a, float b)

//Generics allow you to create a single method that is customized for the type that invokes it.

public T Add lessthanTgreaterthan(T a, T b)

T is subsituted for whatever type you use.

A generic class declaration looks like a non-generic class declaration, except that the class name is followed by a type parameter section.

ankpro
ankpro training
C#
C sharp
Bangalore
Rajajinagar
Selenium
Coded UI
Mobile automation testing
Mobile testing
JQuery
JavaScript
.Net
C
C++
Components of the .Net framework
Hello World
Literal
Keywords
Variable
Data types
Operators
Branching
Loops
Arrays
Strings
Structures
Enums
Functions
Рекомендации по теме