Java’s World in UML Object Shape {abstract} This is done implicitly

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.
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 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
UML Basics & Access Modifier
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
10/7/2015IT 1791 Java’s World in UML Object Shape {abstract} This is done implicitly Shape {abstract} - ShapeName: String # setShapeName(newShapeName:
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
 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.
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.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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 / 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.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
1 / 41 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 5 Programming Fundamentals using Java 1.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Topics Instance variables, set and get methods Encapsulation
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.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
1 More About Derived Classes and Inheritance Chapter 9.
Inheritance.
Chapter 11 Inheritance and Polymorphism
Chapter 1: Object-Oriented Programming and Java
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Inheritance ITI1121 Nour El Kadri.
Chapter 11 Inheritance and Polymorphism
Interface.
Lecture Notes – Interface and Polymorphism (Ch 9-10)
Chapter 8 Classes and Objects
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Chapter 5 Hierarchies IS-A associations superclasses subclasses
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
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.
CS 302 Week 11 Jim Williams, PhD.
CS Week 13 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
null, true, and false are also reserved.
Introduction to Java Programming
Announcements & Review
Code Animation Examples
JavaScript Reserved Words
Lecture 18: Polymorphism (Part II)
JAVA An Introduction to Java OOP Basics
Chapter 11 Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
CS 200 More Classes Jim Williams, PhD.
Agenda Types and identifiers Practice Assignment Keywords in Java
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

Java’s World in UML Object Shape {abstract} This is done implicitly ShapeName: String # setShapeName(newShapeName: String): void + getShapeName(): String + draw(Graphics g):void + erase(Graphics g):void + 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. 7/24/2018 IT 179

Java’s World in UML Object Shape {abstract} Rectangle Circle ThreeD This is done implicitly Shape {abstract} extends Rectangle Circle ThreeD {interface} RectanglePrism Cylinder implements 7/24/2018 IT 179

Shape 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(); …………………………… 7/24/2018 IT 179

Java’s World in UML Object Shape {abstract} Circle Rectangle This is done implicitly Shape {abstract} extends Circle Rectangle 7/24/2018 IT 179

Shape {abstract} Rectangle Circle ShapeName: String # setShapeName(newShapeName: String): void + getShapeName(): String + draw(Graphics g):void + erase(Graphics g):void + 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 7/24/2018 IT 179

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

public class Circle extends Shape { private double radius; ..... 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; 7/24/2018 IT 179

Extend to three-dimension Shape {abstract} Rectangle Circle RectanglePrism z: double + setZ(double): void + getZ(): double + getVolume(): double RectanglePrism Cylinder depth: double + setDepth(double): void + getDepth(): double + getCapacity(): double Cylinder 7/24/2018 IT 179

Java’s World in UML Object Shape {abstract} Rectangle Circle extends RectanglePrism Cylinder 7/24/2018 IT 179

Is there an even better way? public double price(Cylinder k, double unitPrice) { return k.getCapacity()*unitPrice; } public double price(RectanglePrism k, double unitPrice) { return k.getVolume()*unitPrice; overloading What’s the problem? 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? 7/24/2018 IT 179

Java’s World in UML Object Shape {abstract} Rectangle Circle ThreeD This is done implicitly Shape {abstract} extends Rectangle Circle ThreeD {interface} RectanglePrism Cylinder implements 7/24/2018 IT 179

interface Object Shape {abstract} Rectangle Circle RectanglePrism ThreeD {interface} + setDepth(double): void + getDepth(): double + getVolume(): double Rectangle Circle RectanglePrism Cylinder implements 7/24/2018 IT 179

polymorphism public double price(ThreeD k, double unitPrice) { return k.getVolum()*unitPrice; } polymorphism 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 7/24/2018 IT 179

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

Cylinder (I) 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 ); setDepth( theWidth ); .... 7/24/2018 IT 179

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

Rectangle Don’t make it Rectangle 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() ) ); ..... Don’t make it Rectangle So, this method can take any object of any class. 7/24/2018 IT 179

r1 r2 c rp1 rp2 cd1 cd2 7/24/2018 IT 179 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 = 1 height = 1.5 z =1.3 getSurfaceArea() {…} getVolumn)_ {…} …. length = 2 height = 1 getSurfaceArea() {…} …. length = 3 height = 2 getSurfaceArea() {…} …. r1 r2 c rp1 rp2 cd1 cd2 radius = 0.7 getSurfaceArea() {…} …. length = 1.6 height = 3 z =1.6 getSurfaceArea() {…} getVolumn)_ {…} …. radius = 1.5 depth = 3 getSurfaceArea() {…} getVolumn)_ {…} …. radius = 1 depth = 2 getSurfaceArea() {…} getVolumn)_ {…} …. 7/24/2018 IT 179

Visibility Access Levels Modifier Class Package Subclass Every one in the World public Y protected N no modifier private 7/24/2018 IT 179

Comparable<T> {interface} Object Comparable<T> {interface} + compareTo(o:T): int Shape {abstract} ThreeD {interface} + setDepth(double): void + getDepth(): double + getVolume(): double Rectangle Circle RectanglePrism Cylinder 7/24/2018 IT 179