Generics, Lambdas, Reflections

Slides:



Advertisements
Similar presentations
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Advertisements

Road Map Introduction to object oriented programming. Classes
1 L40 Generics (2). 2 OBJECTIVES  To understand raw types and how they help achieve backwards compatibility.  To use wildcards when precise type information.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
Abstract Classes and Interfaces
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Introduction to Generics
Inheritance and Access Control CS 162 (Summer 2009)
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Classes, Interfaces and Packages
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Java Generics.
Sixth Lecture ArrayList Abstract Class and Interface
CSC 243 – Java Programming, Spring 2014
Java Primer 1: Types, Classes and Operators
Java Programming Language
John Hurley Cal State LA
Java Generics.
Chapter 20 Generic Classes and Methods
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Generics.
Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…
Chapter 9 Inheritance and Polymorphism
Chapter 19 Generics Dr. Clincy - Lecture.
Inheritance Basics Programming with Inheritance
AVG 24th 2015 ADVANCED c# - part 1.
CMSC 202 Interfaces.
Generics (Parametric Polymorphism)
Chapter 6 Methods: A Deeper Look
Java Programming Language
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Generics.
Advanced Java Programming
Generic programming in Java
CISC124 Assignment 4 on Inheritance due today at 7pm.
Sridhar Narayan Java Basics Sridhar Narayan
Computer Programming with JAVA
Java Inheritance.
Type Safety, Generics, Lambdas, Class Object
CMSC 202 Generics.
Generics, Lambdas, Reflections
Inheritance and Polymorphism
CSC 220 – Processing Spring, 2017
Class.
Chapter 19 Generics.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Chapter 11 Inheritance and Encapsulation and Polymorphism
CMSC 202 Interfaces.
Classes and Objects Object Creation
Chengyu Sun California State University, Los Angeles
Generics, Lambdas and Reflection
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Generics, Lambdas, Reflections Lecture 6 Advanced Java Generics, Lambdas, Reflections

Type safety Java is a statically typed language. Type safety: Every variable has a type at compile time. Type safety: Making sure the object referenced by a variable is of a type compatible with the type of the variable. object’s type is equal to var’s type or object’s type extends var’s type or object’s type implements var’s interface type Generally, the compiler verifies type safety for us.

Type safety But sometimes the language constructs weakens the type information, to enable more generic code. Example:

Type safety Example: The get() and add()methods of List work with Object (a super class of every class). The type of objects in the List is not known at compile time to enable the reusability of List. We cannot know if all the types in the List are equal.

Generics Similar to C++’s templates, but simpler. Like in C++ Java's generics can be defined on methods, classes and interfaces.

Some comments Integer is an Object type (i.e., a non primitive type) that boxes the primitive int type, There is such wrapper type for each primitive type in java. Unlike C++ templates, generics can only receive Object types (non primitive types) as arguments. It is impossible to create Pair<int, int> and we must use the wrapper types. Java automatically perform boxing and unboxing, but this is a costly operation. Most containers use generics e.g., ArrayList<T>, HashMap<K,V>, etc.

Generic Method

Some comments A generic type argument can have a lower bound type defined using the extend word. Comparable is an interface which defines a single method int compareTo(T arg) All java's wrapper types implements this interface. 

Generic Limitations Not being able to work on primitive types. Generics are "erased" at runtime: At complie time the compiler knows that List<Integer> is not a List<String>. At run time, both of them looks like List<Object>. This limitation can be seen in the following code: This won’t compile  as the compiler knows that at runtime the defined methods has the same signature.

Generic Limitations More on “type erasure”: You can do: List<String> list; ((List)list).add(new Object()); An error will be thrown only when you get the value of a String. Arrays of generic types cannot be instantiated directly i.e., the following will not compile: But can be worked around this way:  pairs is an array of Pair without any additional generic type information.

Wildcards Pair<Object,Object> is not a super-class of Pair<Integer,Integer>. Pair<?,?> is a super-class of Pair<Integer,Integer>. Bad example: This method accepts only Collection<Object>

Wildcards (more at: https://docs. oracle Good example: This method accepts any Collection However The compiler does not allow to add() because the inner type is unknown at compile time.

Default methods in interface A default method is a method which implemented directly in an interface. It provides default implementation for classes implementing that interface. Example: isEmpty() method in Stack.

Default methods in interface

Default methods in interface

Abstract classes vs. interfaces Another solution: declare an abstract AbstractStack class which implements the Stack interface and the method isEmpty(). BUT: you can only derive from one parent class in Java. This will enforce classes to derive from this class, which can be very constraining. Since in Java a class can implement a number of interfaces - the default method does not enforce such constraints.

Anonymous classes They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

Anonymous classes (motivation) We had this example before: What if a class is not Comparable or has more than one way to compare objects?

Anonymous classes (motivation)

Anonymous classes (motivation)

Anonymous classes (now code..)

Anonymous classes – motivation for Lambdas If the implementation of your anonymous class is very simple. The code becomes cumbersome (a lot of “wrapping” for a little bit of code), and sometimes unclear. A common example is as an interface that contains only one method. In this case wer’re trying to pass functionality as an argument. Just as in our Comparator example. Lambda expressions enable you to do this compactly.

Anonymous classes – motivation for Lambdas Lambdas example: The general syntax is (args...) -> { body } If you just want to return something you can write: (args...) -> ...

Reflection – class object There is a special class called Class. Instances of Class represent classes and interfaces in a running Java application. Class has no public constructor. Instead, Class objects are constructed automatically by the JVM.

Reflection – class object The static class property: for a class A, A.class returns an object of the type Class. The method getClass(): for an object a of type A, a.getClass() returns an object of the type Class. getName() - corresponding class name isAssignableFrom(Class) -  the calling Class represents a superclass or superinterface of the class represented by the given Class object (see example)

 

Another example

Output: