Generics, Lambdas and Reflection

Slides:



Advertisements
Similar presentations
Design By Contract Using JMSAssert.
Advertisements

Practice Session 5 Java: Packages Collection Classes Iterators Generics Design by Contract Test Driven Development JUnit.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
OOSC - JMSAssert. Design By Contract A powerful technique for writing reliable software. Specifying the software purpose with the implementation. Key.
CSC 480 Software Engineering Design by Contract. Detail Design Road Map Begin with architectural models  Class model: domain classes  Overall state.
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,
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.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
Practice Session 5 Java: Packages Collection Classes Iterators Generics Default Methods Anonymous Classes Generic Methods Lambdas Design by Contract JUnit.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
Lecture 5:Interfaces and Abstract Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Java Generics.
Sixth Lecture ArrayList Abstract Class and Interface
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Used to help understand requirements more completely
7. Inheritance and Polymorphism
Chapter 19 Java Data Structures
Java Programming Language
Arab Open University 2nd Semester, M301 Unit 5
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Review Session.
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, Lambdas, Reflections
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Functional Programming with Java
Specifying Object Interfaces
Chapter 19 Generics Dr. Clincy - Lecture.
6 Delegate and Lambda Expressions
Chapter 6 Methods: A Deeper Look
Generics.
Generic programming in Java
CISC124 Assignment 4 on Inheritance due today at 7pm.
Generics.
Defining Classes and Methods
Functional interface.
Type Safety, Generics, Lambdas, Class Object
Test Driven Development
Python Primer 1: Types and Operators
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
CMSC 202 Generics.
Generics, Lambdas, Reflections
Test Driven Development
Defining Classes and Methods
Defining Classes and Methods
5. 3 Coding with Denotations
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Chapter 13 Abstract Classes and Interfaces Part 01
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Object Constraint Language (OCL)
Chapter 19 Generics.
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
„Lambda expressions, Optional”
SPL – PS1 Introduction to C++.
Chengyu Sun California State University, Los Angeles
SPL – PS3 C++ Classes.
Multiplying more than two integers
CS 240 – Advanced Programming Concepts
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

Generics, Lambdas and Reflection SPL – PS5 Generics, Lambdas and Reflection

Overview Return to Java Generics Anonymous classes and lambdas Default methods Reflection, class object

Java collections Collections are objects that holds other objects in some manner. For example: List, Map, and Set. Java platform provides many ready to use collection classes. You should usually work with Java’s collection classes, instead of arrays.

Generics A mechanism for creating objects parameterized by a type. For example – a stack. We want to create a stack of integers And a stack of cows

Generics (cont) Since there is code duplication, we may want to create a generalized stack. This stack can hold both cows and integers at the same time! Usually that’s not what we want. The solution – Use generics

Generics example

Default methods A default method is a method which is implemented directly in the interface. There’s a straight-forward way to implement the method isEmpty()

Default methods(cont) This implementation can suite most implementations of stack. We can add this implementation to the interface using default methods.

Generic methods In addition to generic classes and interfaces, Java supports generic methods. Let’s assume this is our Cow class: How do we define the maximal object in an array of cows?

Reminder: Comparator Given two objects of type T, the method compare will compare them and return: A positive integer if o1>o2 A negative integer if o2>o1 Zero if both objects are equal

Generic methods(cont) The following is a generic method used to find a maximal element in an array.

Generic methods(cont)

Anonymous classes Anonymous classes help make you your code more concise. Enable you to declare and instantiate a class at the same time. Use them if you need to use the class only once.

Lambda expressions An issue with anonymous classes is that their syntax can be a bit unclear. When the class we want to instantiate has only one method, we can use lambda expressions.

Lambda expressions (cont) The lambda syntax is (args…) -> {body} You can replace an anonymous class with a lambda expression only if the class has a single non-default method. If all your lambda does is return a value, you can replace (args…) -> {return ans;} with (args…) -> ans; No need to declare the type of the parameter No need to declare a single parameter in parenthesis (Required for multiple parameters) If lambda’s body contain a single expression, no need for curly braces.

Lambda expression example

Class object There’s a special class in Java called Class. Instances of class Class represents classes and instances running in the Java application. Every class in Java has a corresponding Class object. You can receive the object Class in runtime using the static class property, or the method getClass(). The class Object has several methods, getName() which returns the name of the class, and isAssignableFrom(class) which returns true if the calling class is a super-class or interface of the given class.

Class object example

Design by contract DBC is a simple terminology that helps us express contracts between specification and implementation. DBC relies on three words: Precondition – things that must be true before a method is invoked. Postcondition – things that must be true after a method is invoked. Invariants – things that must be true both before and after any method is invoked, and immediately after construction.

Rules that ensure testability Separate commands and queries. Separate basic queries and derived queries. Define derived queries in terms of basic queries. For each basic command, write post-condition which specify the value of every basic query. For each basic command and basic query, express the pre-conditions in terms of basic queries. Specify class invariants that impose constraints that must remain always true on basic queries.