Sadegh Aliakbary Sharif University of Technology Fall 2012.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Lecture 5: Interfaces.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
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.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
UML Class Diagram: class Rectangle
Chapter 10: Inheritance and Polymorphism
Sadegh Aliakbary Sharif University of Technology Fall 2010.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
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.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
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.
Peyman Dodangeh Sharif University of Technology Fall 2014.
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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
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,
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
SCJP 5, 1/7 Declarations, Initialization and Scoping
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Abstract classes and interfaces
More Sophisticated Behavior
Advanced Programming in Java
Inheritance and Polymorphism
University of Central Florida COP 3330 Object Oriented Programming
UML Class Diagram: class Rectangle
EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun
Abstract classes and interfaces
Interface.
Enumerations & Annotations
Enumerations & Annotations
Interfaces.
Inheritance Inheritance is a fundamental Object Oriented concept
Enumerations & Annotations
Java Programming Course
Chapter 14 Abstract Classes and Interfaces
Abstract classes and interfaces
Advanced Programming in Java
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Sadegh Aliakbary Sharif University of Technology Fall 2012

Agenda Enumerations Static Import Annotation Fall 2012Sharif University of Technology2

Enumerations Suppose you have a class with a few instances Example: Student Type : SMS Status : Color : How do you implement it? The class should not be inherited The instances are limited: no further instances Fall 2012Sharif University of Technology3

An Implementation final class Color{ public static final Color Black = new Color(1); public static final Color Blue = new Color(2); public static final Color Green = new Color(3); public static final Color Red = new Color(4); private int color; private Color(int i) { this.color = i; } Fall 2012Sharif University of Technology4 Applications of a private constructor

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 enum Color { Black, Blue, Green, Red } Fall 2012Sharif University of Technology5

Enum enum Color { Black, Blue, Green, Red } final class Color{ public static final Color Black = new Color(); public static final Color Blue = new Color(); public static final Color Green = new Color(); public static final Color Red = new Color(); } Fall 2012Sharif University of Technology6

Enum Sample enum Shape { Rectangle, Circle, Square } enum StudentType{ BS, MS, PhD } Fall 2012Sharif University of Technology7

Using Enums 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... } Fall 2012Sharif University of Technology8

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. Fall 2012Sharif University of Technology9

Enum Enum can be a more complex class With many constructors And fields And methods Fall 2012Sharif University of Technology10

enum Shape { Rectangle(1), Circle(2), Square(3); private int number; Shape(int i){ number= i; } public int getNumber(){ return number; } Shape shape = Shape.Circle; print (shape.getNumber()); shape = Shape.valueOf("Rectangle"); print(shape.getNumber()); Shape[] shapesArray = Shape.values(); for (Shape s : shapesArray) { print(s.name()); } try{ shape = Shape.valueOf("Pyramid"); }catch(Exception exp){ print("Pyramid is not included in Shape"); } Fall 2012Sharif University of Technology11

Static Import In order to access static members Qualify references with the class they came from. For example: double r = Math.sin(Math.PI * theta); static import allows unqualified access to static members without inheriting from the type containing the static members import static java.lang.Math.PI; or import static java.lang.Math.*; double r = sin(PI * theta); Fall 2012Sharif University of Technology12

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 @ SuppressWarnings, … Fall 2012Sharif University of Technology13

Fall 2012Sharif University of Technology14