Generic Programming.

Slides:



Advertisements
Similar presentations
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Advertisements

Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Creating Generic Classes. Introduction Java Generics were added to allow for type- safe collections and eliminate the need for burdensome, code-cluttering.
1 L39 Generics (1). 2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack.
 2006 Pearson Education, Inc. All rights reserved Generics.
Java Generics.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
 2005 Pearson Education, Inc. All rights reserved Generics.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
CIS 270—Application Development II Chapter 18-Generics.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
CSI 3125, Preliminaries, page 1 Generic Class & Generic methods.
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
Building java programs, chapter 3 Parameters, Methods and Objects.
By Mr. Muhammad Pervez Akhtar
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Classes - Intermediate
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
 Pearson Education, Inc. All rights reserved. 1 Ch 18 Generics OBJECTIVES In this chapter you will learn:  To create generic methods that perform.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Object Oriented Programming Lecture 2: BallWorld.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Collections (Stack, queue, tree, graph). References as Links References can be used to create a variety of linked structures, such as a linked list.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Information and Computer Sciences University of Hawaii, Manoa
3 Introduction to Classes and Objects.
Lecture 5: Some more Java!
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 20 Generic Classes and Methods
Chapter 14 Templates C++ How to Program, 8/e
Building Java Programs
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
Building Java Programs
SELECTION STATEMENTS (1)
An Introduction to Java – Part I
OUTPUT STATEMENTS GC 201.
Advanced Programming Behnam Hatami Fall 2017.
Method Mark and Lyubo.
Starting Out with Java: From Control Structures through Objects
Lecture 3 Functions Simple functions
Pass by Reference, const, readonly, struct
7 Arrays.
Pemrograman Dasar Methods PTIIK - UB.
An Introduction to Java – Part I, language basics
Advanced Programming in Java
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CHAPTER 6 GENERAL-PURPOSE METHODS
Introduction to Programming
CS2011 Introduction to Programming I Methods (II)
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
7 Arrays.
Arrays Arrays A few types Structures of related data items
Building Java Programs
Generics 5/11/2019.
Methods/Functions.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Generic Classes and Methods
Presentation transcript:

Generic Programming

Introduction Generic programming means to write code that can be reused for objects of many different types Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods or, with a single class declaration, a set of related types, respectively.

Generics also provide compile-time type safety that allows programmers to catch invalid types at compile time.

Generic Classes: A generic class declaration looks like a non-generic class declaration, except that the class name is followed by a type parameter section. As with generic methods, the type parameter section of a generic class can have one or more type parameters separated by commas. These classes are known as parameterized classes or parameterized types because they accept one or more parameters.

public class Box<T> { private T t; public void add(T t) this public class Box<T> { private T t; public void add(T t) this.t = t; } public T get() return t;

public static void main(String[] args) { Box<Integer> integerBox = new Box<Integer>(); Box<String> stringBox = new Box<String>(); integerBox.add(new Integer(10)); stringBox.add(new String("Hello World")); System.out.printf("Integer Value :%d\n\n", integerBox.get()); System.out.printf("String Value :%s\n", stringBox.get()); } }

OUTPUT: Integer Value :10 String Value :Hello World

Generic Methods: A single generic method declaration that can be called with arguments of different types. Based on the types of the arguments passed to the generic method, the compiler handles each method call appropriately.

All generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method's return type ( < E > in the next example). Each type parameter section contains one or more 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 act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments. A generic method's body is declared like that of any other method. Note that type parameters can represent only reference types, not primitive types (like int, double and char).

public class GenericMethodTest { // generic method printArray public static < E > void printArray( E[] inputArray ) // Display array elements for ( E element : inputArray ) System.out.printf( "%s ", element ); } System.out.println();

public static void main( String args[] ) { // Create arrays of Integer, Double and Character Integer[] intArray = { 1, 2, 3, 4, 5 }; Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 }; Character[] charArray = { 'H', 'E', 'L', 'L', 'O' }; System.out.println( "Array integerArray contains:" ); printArray( intArray ); // pass an Integer array

System.out.println( "\nArray doubleArray contains:"); printArray( doubleArray ); // pass a Double array System.out.println( "\nArray characterArray contains:"); printArray( charArray ); // pass a Character array }