10/7/2015IT 1791 Java’s World in UML Object Shape {abstract} This is done implicitly Shape {abstract} - ShapeName: String # setShapeName(newShapeName:

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Java Programming Abstract classes and Interfaces.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Exercise 1 Suppose C is a class that implements interfaces I and J. Which of the following Requires a type cast? C c = ……? I i = …..? J j = …..? c =
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
OOP: Inheritance By: Lamiaa Said.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
5/2/2015ITK 1791 Shape {abstract} - ShapeName: String # setShapeName(newShapeName: String): void + getShapeName(): String + getSurfaceArea(): double +
Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
UFCE3T-15-M Programming part 1 UFCE3T-15-M Object-oriented Design and Programming Block2: Inheritance, Polymorphism, Abstract Classes and Interfaces Jin.
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 slides created by Marty Stepp
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Building Java Programs Interfaces, Comparable reading: , 16.4, 10.2.
UML Basics & Access Modifier
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
 Definition: The ability to derive child classes (sub- classes) from parent classes (super-classes)  Characteristics:  Methods and instance variables.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
Information and Computer Sciences University of Hawaii, Manoa
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Chapter Outline What inheritance is Calling the superclass constructor Overriding superclass methods Protected members Chains of inheritance The Object.
CS-2852 Data Structures Week 5, Class 1 – Testing and Stacks Return Exams & Labs  Package-level access  UML connectors for instance variables Announcement:
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Lecture Notes – Inheritance and Polymorphism (Ch 9-10) Yonglei Tao.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
ABSTRACT DATA TYPES, ABSTRACT CLASSES AND INTERFACES And abstract art….
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 11 Inheritance and Polymorphism.
/** Feb 1996 Cay Horstmann */ import java.util.*; import corejava.*; public class EmployeeTest { public static void main(String[]
CS102 – OOP_2 Inheritance, Polymorphism, Interfaces & Abstract classes. David Davenport.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
1 / 41 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 5 Programming Fundamentals using Java 1.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Building Java Programs Interfaces reading: , 16.4.
9/27/2016IT 1791 Abstraction A tool (concept) to manage complexity Hide irrelevant details; focus on the features needed Primitive date types are already.
Chapter 1: Object-Oriented Programming and Java
Sixth Lecture ArrayList Abstract Class and Interface
Interface.
Abstraction A tool (concept) to manage complexity
Lecture Notes – Interface and Polymorphism (Ch 9-10)
Java’s World in UML Object Shape {abstract} This is done implicitly
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
MyList<T> It’s a generic type, <T> is a type parameter
null, true, and false are also reserved.
Introduction to Java Programming
(Java Collections Framework) AbstractSequentialList
JavaScript Reserved Words
Instructor: Alexander Stoytchev
CS 200 More Classes Jim Williams, PhD.
Agenda Types and identifiers Practice Assignment Keywords in Java
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

10/7/2015IT 1791 Java’s World in UML Object Shape {abstract} This is done implicitly Shape {abstract} - ShapeName: String # setShapeName(newShapeName: String): void + getShapeName(): String + getSurfaceArea(): double + getPerimeter(): double There are some details that are not as important to the concept of shape, hence leave them for the programmer to implement.

10/7/2015IT 1792 package shapes; public abstract class Shape { protected static final double DEFAULT_SIZE = ( double ) 1.0; protected static final String DEFAULT_NAME = "Unknown"; private String shapeName; public Shape() { this.shapeName = DEFAULT_NAME; } public Shape( String name ) { setShapeName( name ); } protected void setShapeName( String name ) { shapeName = new String( name ); } public abstract double getSurfaceArea(); …………………………… } Shape

10/7/2015IT 1793 Java’s World in UML Object Shape CircleRectangle {abstract} extends This is done implicitly

10/7/2015IT 1794 Shape {abstract} - ShapeName: String # setShapeName(newShapeName: String): void + getShapeName(): String + getSurfaceArea(): double + getPerimeter(): double Rectangle - length: double - height: double + setLength(newLength: double): void + getLength(): double + setHeight(newHeight: double): void + getHeight(): double Circle - radius: double + setRadius(newRadius: double): void + getRadius(): double

10/7/2015IT 1795 package shapes; public class Circle extends Shape { private double radius; public Circle() { super( "Circle" ); setRadius( super.DEFAULT_SIZE ); } public Circle( double theRadius ) { super( "Circle" ); if ( theRadius <= 0.0 ) { setRadius( Shape.DEFAULT_SIZE ); } else { setRadius( theRadius ); } } Circle (I)

10/7/2015IT 1796 package shapes; public class Circle extends Shape { private double radius;..... public double getRadius() { return this.radius; } public void setRadius( double theRadius ) { if ( theRadius <= 0 ) { return; } this.radius = theRadius; } public double getSurfaceArea() { return this.radius * this.radius * Math.PI; } public double getPerimeter() { return 2 * this.radius + Math.PI; } Circle (II)

10/7/2015IT 1797 Cylinder Extend to three-dimension Shape CircleRectangle {abstract} Cylinder - depth: double + setDepth(double): void + getDepth(): double + getCapacity(): double RectanglePrism - z: double + setZ(double): void + getZ(): double + getVolume(): double

10/7/2015IT 1798 public double price(Cylinder k, double unitPrice) { return k.getCapacity()*unitPrice; } public double price(RectanglePrism k, double unitPrice) { return k.getVolume()*unitPrice; } Is there a better way? /* Supposed that getVolumn is defined in Shape */ public double price(Shape k, double unitPrice) { return k.getVolume()*unitPrice; } Is there an even better way? Yes, we use “interface”. How to unify the interface? What’s the problem?

10/7/2015IT 1799 Java’s World in UML Object Shape CircleRectangle RectanglePrismCylinder ThreeD {interface} {abstract} implements extends This is done implicitly

10/7/2015IT interface Object Shape CircleRectangle RectanglePrismCylinder {abstract} ThreeD {interface} + setDepth(double): void + getDepth(): double + getVolume(): double implements

10/7/2015IT public double price(ThreeD k, double unitPrice) { return k.getVolum()*unitPrice; } public static void main(String[] args) { Rectangle a; Circle b; RectanglePrism c; Cylinder d; … … … price(c,2.99)+price(d,3.99); // price(a,2.99)+price(b,3.99); Not allowed

10/7/2015IT package shapes; public interface ThreeD { double getDepth(); void setDepth( double theDepth ); double getVolume(); } ThreeD Any class implements the interface must implement all methods in the interface. must be all abstract Some interfaces don’t even have the body called marker interface.

10/7/2015IT package shapes; public final class Cylinder extends Circle implements ThreeD { private double depth; public Cylinder() { this( Shape.DEFAULT_SIZE, Shape.DEFAULT_SIZE ); } public Cylinder( double theRadius, double theWidth ) { setShapeName( "Cylinder" ); if ( theRadius <= 0.0 ) { setRadius( Shape.DEFAULT_SIZE ); } else { setRadius( theRadius ); } if ( theWidth <= 0.0 ) { setDepth( Shape.DEFAULT_SIZE ); } else { setDepth( theWidth ); }.... } Cylinder (I)

10/7/2015IT Collection List LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface for several implementations {interface} The Java Collections Framework (JCF) RandomAccess {interface} A bstractSequentialLis t {abstract} Stack Vector

10/7/2015IT O bject’s toString and equals methods Shape CircleRectangle {abstract} Object + toString(): String + equals(o: Object): boolean the default method use identities to do the job. Now, we have a better idea about how this job to be done.

10/7/2015IT package shapes; public class Rectangle extends Shape {.... public String toString() { return this.getShapeName() + ": length = " + this.length + ", height = " + this.height; } public boolean equals( Object o ) { if ( ( o == null ) || ( ! ( o instanceof Rectangle ) ) ) { return false; } return ( ( ( ( Rectangle ) o ).getLength() == getLength() ) && ( ( ( Rectangle ) o ).getHeight() == getHeight() ) ); }..... } Rectangle So, this method can take any object of any class. Don’t make it Rectangle

Visibility 10/7/2015IT Access Levels ModifierClassPackageSubclassWorld publicYYYY protectedYYYN no modifierYYNN privateYNNN

10/7/2015 IT import shapes.*; public static void main(String[] args) { Rectangle r1 = new Rectangle(2,1); Rectangle r2 = new Rectangle(3,2); Circle c = new Circle(0.7); RectanglePrism rp1 = new RectanglePrism(1,1.5,1.3); RetanglePrime rp2 =... ; Cylinder cd1 =...,cd2 =... ;... } length = 2 height = 1 getSurfaceArea() {…} …. length = 3 height = 2 getSurfaceArea() {…} …. radius = 0.7 getSurfaceArea() {…} …. radius = 1 depth = 2 getSurfaceArea() {…} getVolumn)_ {…} …. radius = 1.5 depth = 3 getSurfaceArea() {…} getVolumn)_ {…} …. length = 1 height = 1.5 z =1.3 getSurfaceArea() {…} getVolumn)_ {…} …. length = 1.6 height = 3 z =1.6 getSurfaceArea() {…} getVolumn)_ {…} …. r1 r2 c rp1 rp2 cd1 cd2

10/7/2015 IT L1 L2 L3 L4 “Sean” SimpleListAry data size = 5 void remove(int i) {….} void add(int I, Integer item) {…} Integer max() {…}; … “Leon” “Tom” “Mary” SimpleListAry data size = 4 void remove(int i) {….} void add(int I, String item) {…} Stringr max() {…}; … data next SimpleListLk head size = 2 void remove(int i) {….} void add(int I, String item) {…} String max() {…}; … SimpleListLk head size = 3 void remove(int i) {….} void add(int I, Integer item) {…} Integer max() {…}; … data next data next data next data next “ISU” “IT179”