Key Concepts

Review core concepts you need to learn to master this subject

Universal (Generic) Algorithms

When using generics in Java, universal (generic) algorithms can be created for different types.

Generics: Lesson
Lesson 1 of 2
  1. 1
    When using Java, we often write classes and algorithms that work around certain data types. Let’s look at the following class as an example: public class StringBox { public String myString; } I…
  2. 2
    Previously we saw how generics can help make our code more manageable and flexible to future needs. We created the following generic class: public class Box { private T data; public Box(T da…
  3. 3
    Generics allow our programs to adapt to different data type needs but one issue with them is that we cannot use primitive types (int, double, boolean, etc) as arguments to generic type parameters. …
  4. 4
    We’ve seen how to create a generic class but we can also create a generic interface. Let’s look at an example: public interface Replacer { void replace(T data); } The generic interface Messenge…
  5. 5
    We’ve covered generic classes and interfaces but what can we do if we simply want one of our methods to be generic and not the whole class or interface? We can create generic methods to do just tha…
  6. 6
    We’ve done great work learning about generic classes, interfaces, and methods. Let’s discuss some benefits of using generics besides making our code more scalable. We can actually get away with not…
  7. 7
    As of now, our generic classes, interfaces, and methods have all taken a single parameter type but what if our program needed to specify two or more parameter types? Java generics allow us to do th…
  8. 8
    We’ve seen how generics make our code scalable by allowing us to provide type arguments to classes, interfaces, and methods. What if we needed to restrict what class or interface could be used as a…
  9. 9
    We’ve seen that generic code can take type arguments to help generalize our code. We can make our code even more general when we don’t need the more strict type checking of using type parameters by…
  10. 10
    Recall that generic code can have an upper bound when defined using a type parameter or a wildcard. We can also provide a lower bound when working with wildcards. A lower bound wildcard restricts…
  11. 11
    Great job learning about generics! In this lesson we’ve covered how: - Generics help us make generic, scalable code. - The diamond operator (<>) is used to define generic classes, interfaces, or me…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo