CEG860 (Prasad)L3EG1 Object-Oriented Programming Examples.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 Array and Collections.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
CPSC 441 TUTORIAL – JANUARY 16, 2012 TA: MARYAM ELAHI INTRODUCTION TO C.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
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 Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Lecture 28: Abstract Classes & Inheritance Announcements & Review Lab 8 Due Thursday Image and color effects with 2D arrays Read: –Chapter 9 Cahoon & Davidson.
April 20023CSG11 Electronic Commerce Java (1) John Wordsworth Department of Computer Science The University of Reading Room 129,
CS3180 (Prasad)L3OOP1 Object-Oriented Programming Programming with Data Types to enhance reliability and productivity ( through reuse and by facilitating.
Introduction to Java Programming
Principles of Object-Oriented Software Development The language Java.
1 Java (vs. C++). 2 Object-oriented Programming Java and C++ are the most popular object-oriented programming languages Java Sun Microsystems in 1990.
Programming Paradigms cs784(Prasad)L5Pdm1. Programming Paradigm A way of conceptualizing what it means to perform computation and how tasks to be carried.
CS884 (Prasad)Java Goals1 “Perfect Quote” You know you've achieved perfection in design, Not when you have nothing more to add, But when you have nothing.
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 slides created by Marty Stepp
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Building Java Programs Interfaces, Comparable reading: , 16.4, 10.2.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
+ Java vs. Javascript Jessi Style. + Java Compiled Can stand on its own Written once, run anywhere Two-stage debugging Java is an Object Oriented Programming.
Programming Languages and Paradigms Object-Oriented Programming.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Java and C++, The Difference An introduction Unit - 00.
Chapter 11 and 12: Abstract Data Types and Encapsulation Constructs; Support for Object Orientation Lesson 11.
Computing with C# and the.NET Framework Chapter 1 An Introduction to Computing with C# ©2003, 2011 Art Gittleman.
Programming Paradigms Procedural Functional Logic Object-Oriented.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
Programming Languages and Paradigms Object-Oriented Programming.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Collections. The Plan ● Why use collections? ● What collections are available? ● How are the collections different? ● Examples ● Practice.
Arrays, ArrayLists, and Collections. Rationale Suppose we have a program to compute the average of three numbers. We could write a simple little method.
Chapter 10 Inheritance and Polymorphism
C++ The reason why it is still in use. Table of Contents Disadvantages Disadvantages Advantages Advantages Compare with object-oriented programming language.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Ceg860 (Prasad)L112Inh1 Inheritance Reusability Extendibility Type Safety Reusability ; Extendibility ; Type Safety (Minimize Repetition, Accommodate Variation.
More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300.
Wel come To Seminar On C#.
A comparison of C-Sharp and Java Zunaid Jogee Supervisor: T. Stakemire.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CS 3180 (Prasad)L67Classes1 Classes and Interfaces Inheritance Polymorphism.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
Building Java Programs Interfaces reading: , 16.4.
Sixth Lecture ArrayList Abstract Class and Interface
Programming Paradigms
University of Central Florida COP 3330 Object Oriented Programming
C# and the .NET Framework
Introduction to C# AKEEL AHMED.
null, true, and false are also reserved.
1.4 ทบทวน JAVA OO Programming Concepts Declaring and Creating Objects
Introducing Java.
slides created by Marty Stepp
Programming Paradigms
Agenda Types and identifiers Practice Assignment Keywords in Java
Computer Science II for Majors
Presentation transcript:

CEG860 (Prasad)L3EG1 Object-Oriented Programming Examples

CEG860 (Prasad)L3EG2 Inheritance/Redefinition : Example import java.awt.Color; class Rectangle { int w, h; Rectangle (int ww, int hh) { w = ww; h = hh; } int perimeter () { return ( 2*(w + h) ); }

CEG860 (Prasad)L3EG3 class ColoredRectangle extends Rectangle { Color c; // inheritance ColoredRectangle (Color cc, int w, int h) { super(w,h); c = cc; } } class Square extends Rectangle { Square(int w) { super(w,w); } int perimeter () { // overriding return ( 4*w ); } }

CEG860 (Prasad)L3EG4 Polymorphism : Example class Eg { public static void main (String[] args) { r Rectangle r = new Rectangle(5,6); r.() System.out.println( r.perimeter() ); r r = new ColoredRectangle (Color.red,1,1) ; r.() System.out.println( r.perimeter() ); }

CEG860 (Prasad)L3EG5 Dynamic Binding : Example class Eg { public static void main (String[] args) { rs Rectangle [] rs = { new Rectangle(5,6), new ColoredRectangle (Color.red,1,1), new Square(2)} ; iii for (int i = 0 ; i < rs.length ; i++ ) rs[i].() System.out.println( rs[i].perimeter() ); }}

CEG860 (Prasad)L3EG6 Rendition in C++ #include using namespace std; class Rectangle { protected: int w, h; public: Rectangle (int ww, int hh) { w = ww; h = hh; } virtual int perimeter () { return ( 2*(w + h) ); } };

CEG860 (Prasad)L3EG7 class ColoredRectangle : public Rectangle { private: // inheritance int c; public: ColoredRectangle (int cc, int w, int h) : Rectangle(w,h) { c = cc; } }; class Square : public Rectangle { public: Square(int w) : Rectangle(w,w) {} int perimeter () { // overriding return ( 4*w ); // protected, not private } };

CEG860 (Prasad)L3EG8 void main (char* argv, int argc) { Rectangle r (5,6); cout << r.perimeter() << endl; ColoredRectangle cr (0,1,1) ; r = cr; // coercion (truncation) cout << r.perimeter() << endl << cr.perimeter() << endl; // inheritance Square s = Square(5); r = s; // NOT polymorphism cout << r.perimeter() << endl; cout << s.perimeter() << endl; // static binding }

CEG860 (Prasad)L3EG9 void main (char* argv, int argc) { Rectangle* r = new Rectangle(5,6); cout << r  perimeter() << endl; r = new ColoredRectangle(0,1,1) ; cout << r  perimeter() << endl; r = new Square(5) ; cout << r  perimeter() << endl; // polymorphism and dynamic binding // perimeter() explicitly declared virtual }

CEG860 (Prasad)L3EG10 Polymorphic Data Structure and Dynamic Binding in C++ void main (char* argv, int argc) { const RSLEN = 3; // coercion, no dynamic binding Rectangle rs [RSLEN]= { Rectangle(5,6), ColoredRectangle(0,1,1), Square(5)} ; for (int i = 0 ; i < RSLEN ; i++ ) cout << rs[i].perimeter() << endl; } void main (char* argv, int argc) { const RSLEN = 3; // polymorphism Rectangle* rs [RSLEN]= { new Rectangle(5,6), new ColoredRectangle(0,1,1), new Square(5)} ; for (int i = 0 ; i < RSLEN ; i++ ) cout << rs[i]  perimeter() << endl; }

CEG860 (Prasad)L3EG11 Rendition in C# (Dynamic Binding) using System.Drawing; class Rectangle { protected int w, h; public Rectangle (int ww, int hh) { w = ww; h = hh; } public virtual int perimeter () { System.Console.WriteLine( "Rectangle.perimeter() called" ); return ( 2*(w + h) ); }} class ColoredRectangle : Rectangle { protected Color c; // inheritance public ColoredRectangle (Color cc, int w, int h):base(w,h) { c = cc;} } class Square : Rectangle { public Square(int w): base(w,w) { } public override int perimeter () { // overriding System.Console.WriteLine( "Square.perimeter() called" ); return ( 4*w ); }}

CEG860 (Prasad)L3EG12 class Eg { public static void Main (string[] args) { Rectangle r = new Rectangle(5,6); System.Console.WriteLine( r.perimeter() ); r = new ColoredRectangle(Color.Red,1,1) ; System.Console.WriteLine( r.perimeter() ); r = new Square(2) ; System.Console.WriteLine( r.perimeter() ); }

CEG860 (Prasad)L3EG13 Polymorphic Data Structure class EgA { public static void Main (string[] args) { Rectangle [] rs = { new Rectangle(5,6), new ColoredRectangle(Color.Red,1,1), new Square(2) } ; for (int i = 0 ; i < rs.Length ; i++ ) System.Console.WriteLine( rs[i].perimeter() ); }

CEG860 (Prasad)L3EG14 Rendition in C# (Static Binding) using System.Drawing; class Rectangle { protected int w, h; public Rectangle (int ww, int hh) { w = ww; h = hh; } public virtual int perimeter () { System.Console.WriteLine( "Rectangle.perimeter() called" ); return ( 2*(w + h) ); }} class ColoredRectangle : Rectangle { protected Color c; // inheritance public ColoredRectangle (Color cc, int w, int h):base(w,h) { c = cc;} } class Square : Rectangle { public Square(int w): base(w,w) { } public new int perimeter () { // does not override System.Console.WriteLine( "Square.perimeter() called" ); return ( 4*w ); }}

CEG860 (Prasad)L3EG15 Example : OOP Style vs Procedural Style  Client Determine the number of elements in a collection.  Suppliers Collections : Vector, String, List, Set, Array, etc  Procedural Style A client is responsible for invoking appropriate supplier function for determining the size.  OOP Style Suppliers are responsible for conforming to the standard interface required for exporting the size functionality to a client.

CEG860 (Prasad)L3EG16 Client in Scheme (define (size C) (cond ( (vector? C) (vector-length C) ) ( (pair? C) (length C) ) ( (string? C) (string-length C) ) ( else “ size not supported ”) ) )) (size (vector 1 2 (+ 1 2))) (size ‘(one “two” 3))

CEG860 (Prasad)L3EG17 Suppliers and Client in Java interface Collection { int size(); } class myVector extends Vector implements Collection { } class myString extends String implements Collection { public int size() { return length();} } class myArray implements Collection { int[] array; public int size() {return array.length;} } Collection c = new myVector(); c.size();

CEG860 (Prasad)L3EG18 Extra-Slides

CEG860 (Prasad)L3EG19 Java Collections : Interfaces

CEG860 (Prasad)L3EG20 Java Collections : Implementations Implementations Hash Table Resizable ArrayBalanced TreeLinked List Interfaces Set HashSet TreeSet List ArrayList LinkedList Map HashMap TreeMap

CEG860 (Prasad)L3EG21 Java : Compiler and Interpreter javac java source code bytecode native code mipspentiumsparcalpha JIT JVM

CEG860 (Prasad)L3EG22 Evolution of JVM (JDK) Java 1.0: Interpreter Java 1.1: Interpreter + JIT Compiler Java > 2 : Hotspot –Profiling and Adaptive Dynamic Compilation of “hot” code –Method in-lining and other aggressive optimizations, and Decompilation –Improved Memory Management for long-running (server) programs –Fast Thread Synchronization

CEG860 (Prasad)L3EG23.NET Compilers C# csc java source languages MSIL (MS Intermediate Language) native code Wintel JIT Visual BasicCOBOL CLR