Core java - Advance Topics
  • Welcome
  • Schedule
  • 1) Exception Handling
    • 1) Introduction to Exception Handling
    • 2) Categories of Exceptions
    • 3) Creating a method that throws an exception
    • 4) Creating Custom Exception Classes
    • 5)What happens when an exception is thrown?
      • 5.1) Creating try-catch-finally blocks
      • 5.2) Using a method that throws a checked exception
      • 5.3) Using a method that throws a runtime exception
      • 5.4) Using a method that throws an error
      • 5.5) Will a finally block execute even if the catch block defines a return statement?
      • 5.6) What happens if both a catch and a finally block define return statement?
      • 5.7) What happens if a finally block modifies the value returned from a catch block?
      • 5.8) Can a try block be followed only by a finally block?
      • 5.9) Does the order of the exceptions caught in the catch blocks matter?
      • 5.10) Can I rethrow an exception or the error I catch?
      • 5.11) Can I declare my methods to throw a checked exception instead of handling it?
      • 5.12) I can create nested loops, so can I create nested try-catch blocks too?
      • 5.13) Should I handle errors?
    • 6) Best Practices
    • 7) Cheat Sheet
    • 8) Problems
  • 2) Wrapper Classes and Enums
    • 2.1) Creating objects of the wrapper classes
    • Enums
  • 3) Inner Classes
    • 3.1) Static nested class (also called static inner class)
    • 3.2) Inner class (also called member class)
    • 3.3) Anonymous inner class
    • 3.4) Method local inner classes
    • CheatSheet
  • 4) Generics
    • Multiple Type parameters in Generic classes
    • Inheritance using Generics
    • Generic interfaces
    • Generic Methods
    • Bounded type parameters
    • Applications
  • 5) Equals and Hashcode
    • Problems
  • CompareTo method overview
  • Basic DS
    • 1) Simple Array List
    • 2) Simple HashMap
  • 5) Collections Framework - Part 1
    • Introducing the collections framework
    • Working with the Collection interface
      • The core Collection interface
      • Methods of the Collection interface
    • Creating and using List, Set, and Deque implementations
      • List interface and its implementations
      • Iterators
      • Sorting List using custom sorting technique
      • Comparable Interface
      • Custom Sorting using comparator
      • ArrayList - Examples and practice problems
    • Stack
    • Linked List
    • LinkedList Operations
  • 6) Collections Framework - Part 2
    • Sets
      • Set Types
      • Array to Set (vice versa)
    • Maps
    • TreeMap
    • Autoboxing And Unboxing
  • Collections Framework - Part 3
    • Basics : DS , Number System
    • Internal Working
      • HashMap
      • HashSet
  • 7) Reflection API
  • 8) Annotations
  • 9) Reading Input From Various Sources
    • File Handling
    • Reading From Xml
    • Reading From JSON
  • 10) Multi-threading (Concurrency)
    • Protect shared data
    • Thread-safe access to shared data
  • 11) Design Patterns
    • Singleton
    • DI
  • 12) Internal Working of JVM
  • 13) Garbage Collection
  • 14) More on Strings (Buffer and Builder)
  • 15) Cloning and Immutable Class
    • 16) Serialization And Deserialization
    • Untitled
  • JAVA 8
    • Interface Changes
    • Lambda
    • Method Ref
    • Optional
    • Streams
    • Predicates
  • Practice Tests
    • Test - Collections
    • OOPS
    • S-OOPS
Powered by GitBook
On this page
  • DEFINING A GENERIC INTERFACE:
  • NONGENERIC CLASS IMPLEMENTING A GENERIC INTERFACE:
  • GENERIC CLASS IMPLEMENTING A GENERIC INTERFACE:

Was this helpful?

  1. 4) Generics

Generic interfaces

PreviousInheritance using GenericsNextGeneric Methods

Last updated 5 years ago

Was this helpful?

A generic interface enables you to abstract over types. In this section, you’ll see how to define and implement generic interfaces.

DEFINING A GENERIC INTERFACE:

The declaration of a generic interface includes one or more type parameters. Let’s look at an example of a generic interface that can accept multiple type parameters: the MyMap interface accepts two type parameters and defines methods put() and get(). You can compare the MyMap interface to a simplified version of the Map inter- face, defined in the java.util package:

NONGENERIC CLASS IMPLEMENTING A GENERIC INTERFACE:

When a nongeneric class implements a generic interface, the type parameters don’t follow the class name. For the implemented interface, the type parameters are replaced by actual types:

In the preceding example, MapLegendNonGeneric is a nongeneric class which implements generic interface MyMap (defined in the previous section).

When implementing a generic interface, take note of the type parameters and how they are used in method declarations (method parameters and return types). The methods of an implementing class must implement or override all the interface methods. In the following example, class MapLegendNonGeneric won’t compile because it doesn’t override the abstract method get(String) in MyMap (the return type of get() is declared to be String, not Integer):

A nongeneric class can implement a generic interface by replacing its type parameters with actual types.

Code :

package com.gs.corejava.generics;

interface MyMap<K,V>{
	void put(K key, V value);
    V get(K key);
}

class MapLegendNonGeneric implements MyMap<String,Integer>{

	public void put(String key, Integer value) {		
	}
	public Integer get(String key) {
		return null;
	}
	
}

public class NonGenericClassWithGenericInterface {

}

GENERIC CLASS IMPLEMENTING A GENERIC INTERFACE:

Here’s an example of declaring a generic class that implements a generic MyMap inter- face. To pass the type parameter information to a class, the type parameters must follow both the name of the class and the interface implemented by the class:

You might also choose a combination. In the following examples, the classes define only one parameterized type, V or K. While implementing the MyMap interface, the classes pass actual parameters (String or Integer) to one of the interface’s parameterized types (K or V):

It’s important to use a correct combination of type parameters and actual parameters in the method declarations. The following class won’t compile because class Map- LegendGeneric doesn’t implement method put(K key, String value) from the MyMap interface:

Generic classes and interfaces are collectively referred to as generic types.