Enumerations & Annotations

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Lecture 5: Interfaces.
Java Annotations. Annotations  Annotations are metadata or data about data. An annotation indicates that the declared element should be processed in.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 10 Classes Continued
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1162 JDK 5.0 Features Christian Kemper Principal Architect Borland.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.
Programming in Java CSCI-2220 Object Oriented Programming.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Object Oriented Programming
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Java 1 Introduction Why annotations?  Enhance ease-of-development  Shift some code generation from programmer to compiler What are annotations?
 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 2 Mehdi Einali Advanced Programming in Java 1.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
SCJP 5, 1/7 Declarations, Initialization and Scoping
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Abstract classes and interfaces
Advanced Programming in Java
Advanced Programming in Java
More Sophisticated Behavior
Inheritance and Polymorphism
Methods Attributes Method Modifiers ‘static’
University of Central Florida COP 3330 Object Oriented Programming
Advanced Programming in Java
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
Object Based Programming
Abstract classes and interfaces
Object-Oriented Programming: Polymorphism
Interface.
Enumerations & Annotations
Interfaces.
Advanced Programming Behnam Hatami Fall 2017.
Advanced Java Programming
Advanced Programming in Java
Inheritance Inheritance is a fundamental Object Oriented concept
Object-Oriented Programming
Advanced Programming in Java
Enumerations & Annotations
Java Programming Course
Applying OO Concepts Using Java
Chapter 14 Abstract Classes and Interfaces
Abstract classes and interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Enumerations & Annotations Advanced Programming in Java Enumerations & Annotations Mehdi Einali

enum

Enumerations Suppose you have a type with a few instances Example: Student Type : <BS, MS, PhD> SMS Status : <Sent, Delivered, Not Delivered, Not Sent> WeekDay: <Saturday, Sunday, Monday, …> How do you implement it? The class should not be inherited The instances are limited: no further instances

Bad Implementation1 final class WeekDay{ public static final WeekDay Saturday= new WeekDay(1); public static final WeekDay Sunday= new WeekDay(2); public static final WeekDay Monday = new WeekDay(3); public static final WeekDay Tuesday= new WeekDay(4); private int code; private Color(int i) { this.code = i; }

Bad Implementation2 interface WeekDay{ int Saturday= 1; int Sunday= 2; int Monday = 3; int Tuesday= 4; } class Client implements WeekDay{ public void foo(){

Java enum Java introduces enumerations for this purpose Enumerated Data Type A simple class enum keyword instead of class or interface Comma seperated enum instances enum instances are constant

sample enum Shape { Rectangle, Circle, Square } enum StudentType{ BS, MS, PhD

use Color color = Color.Black; Shape shape = Shape.Circle; show(shape, color); ... private static void show ( Shape shape, Color color) { //show a shape with this color }

Enum Characteristics enum types are implicitly final Can not be a super-class Because they declare constants that should not be modified Instances are constants enum constants are implicitly public, static and final No new instances can be created object instantiation of enum types with operator new results in a compilation error.

Switch on enum

More than constant Enum can be a more complex class With many constructors And fields And methods

Implicitly static final

Implicit methods

annotations

Annotation

What is annotation An annotation is a special form of metadata Added to Java source code Classes, methods, variables, parameters and packages may be annotated Unlike Javadoc tags, Java annotations can be reflective They can be embedded in class files (byte code) May be retained by the Java VM at run-time Example @Override, @Deprecated, @ SuppressWarnings, …

Why Annotation? Enables “declarative programming” style Less coding since tool will generate the boliler plate code from annotations in the source code Easier to change Eliminates the need for maintaining "side files" that must be kept up to date with changes in source files Information is kept in the source file example) Eliminate the need of deployment descriptor

{"name":"Ali Karimi","number":"7"} sample {"name":"Ali Karimi","number":"7"}

3 different types @Override

Write annotation

use

end