This keyword.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Python Objects and Classes
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
TA: Nouf Al-Harbi NoufNaief.net :::
Road Map Introduction to object oriented programming. Classes
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
UML Basics & Access Modifier
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
CISC6795: Spring Object-Oriented Programming: Polymorphism.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Intro to OOP with Java, C. Thomas Wu
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
1 Chapter 10 Object-Oriented Thinking. 2 Class Abstraction and Encapsulation Class abstraction means to separate class implementation details from the.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Peyman Dodangeh Sharif University of Technology Fall 2014.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Programming Fundamentals I (COSC- 1336), Lecture 10 (prepared after Chapter 10 of Liang’s 2011 textbook) Stefan Andrei 12/18/20151 COSC-1336, Lecture 10.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Object-Oriented Design.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 12 Inheritance and Class Design 1.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance and Polymorphism
Object-Oriented Programming: Inheritance and Polymorphism.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Object-Oriented Programming: Polymorphism Chapter 10.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 6: Composition and Inheritance CS202 Fall 2013.
1 More About Derived Classes and Inheritance Chapter 9.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Chapter 11 Inheritance and Polymorphism
Advanced Programming in Java
Chapter 15 Abstract Classes and Interfaces
Chapter 11 Object-Oriented Design
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
CS 302 Week 11 Jim Williams, PhD.
Chapter 10 Thinking in Objects
Extending Classes.
Advanced Programming Behnam Hatami Fall 2017.
Fundaments of Game Design
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

this keyword

BMI Class

this keyword refers to an object itself. public class BMI { private String name; private int age; private double weight; // in pounds private double height; // in inches public static final double KILOGRAMS_PER_POUND = 0.45; public static final double METERS_PER_INCH = 0.0254; public BMI(String name, int age, double weight, double height) { this.name = name; this.age = age; this.weight = weight; this.height = height; } this keyword refers to an object itself. One common use of the this keyword is reference a class’s hidden data fields.

public BMI(String name, double weight, double height) { this(name, 20, weight, height); } Another common use of the this keyword to enable a constructor to invoke another constructor of the same class. public double getBMI() { double bmi = weight * KILOGRAMS_PER_POUND / ((height * METERS_PER_INCH) * (height * METERS_PER_INCH)); return Math.round(bmi * 100) / 100.0;

public String getStatus() { double bmi = getBMI(); if (bmi < 16) return "seriously underweight"; else if (bmi < 18) return "underweight"; else if (bmi < 24) return "normal weight"; else if (bmi < 29) return "over weight"; else if (bmi < 35) return "seriously over weight"; else return "gravely over weight"; }

Abstract Class and Abstract Methods An abstract class is declared with keyword abstract. An abstract class normally contains one or more abstract methods. ABSTRACT CLASSES MAY NOT BE INSTANTIATED using the new operator. public abstract void draw(); // abstract method Abstract methods do not provide implementations. A class that contains any abstract methods must be explicitly declared abstract even if that class contains some concrete methods.

Abstract Class and Abstract Methods Each concrete subclass of an abstract superclass also must provide concrete implementations of each of the superclass’s abstract methods. Constructors and static methods cannot be declared abstract. Constructors are not inherited, so an abstract constructor could never be implemented. Though non-private static methods are inherited, they cannot be overridden. Since abstract methods are meant to be overridden so that they can process objects based on their types, it would not make sense to declare a static method as abstract.

Software Engineering Observation An abstract class declares common attributes and behaviors (both abstract and concrete) of the various classes in a class hierarchy. An abstract class typically contains one or more abstract methods that subclasses must override if they are to be concrete. The instance variables and concrete methods of an abstract class are subject to the normal rules of inheritance.

Q1)Design a class named Triangle that extends GeometricObject. class GeometricObject { ………… }   class Triangle extends GeometricObject { ………….. }

The class contains: Q2)Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. class Triangle extends GeometricObject { private double side1 = 1.0, side2 = 1.0, side3 = 1.0;

Q3)A no-argument constructor that creates a default triangle Q3)A no-argument constructor that creates a default triangle. public Triangle() { } Q4) A constructor that creates a triangle with the specified side1, side2, and side3. public Triangle(double side1, double side2, double side3) { this.side1 = side1; this.side2 = side2; this.side3 = side3; }

Q5)The accessor methods for all three data fields Q5)The accessor methods for all three data fields. // Override abstract getArea() method in GeometricObject public double getArea() { // returns the area of triangle double s = (side1 + side2 + side3) / 2; return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)); } /*Override abstract getPerimeter () method in GeometricObject*/ public double getPerimeter() { return side1 + side2 + side3; //returns perimeter of triangle. }

// Override the toString method public String toString() { //returns string description for thetriangle. // Implement it to return the three sides return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3; } }  

public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; An abstract class’s purpose is to provide an appropriate superclass from which other classes can inherit and thus share a common design Classes that can be used to instantiate objects are called concrete classes.

abstract class continued /. Construct a default geometric object abstract class continued /** Construct a default geometric object */ protected GeometricObject() { dateCreated = new java.util.Date(); } /** Construct a geometric object with color and filled value */ protected GeometricObject(String color, boolean filled) { this.color = color; this.filled = filled;

public String getColor() { return color; } abstract class continued /** Return color */ public String getColor() { return color; } public void setColor(String color) {/** Set a new color */ this.color = color; } /*Return filled. Since filled is boolean, the get method is named isFilled */ public boolean isFilled() { return filled; } /** Set a new filled */ public void setFilled(boolean filled) { this.filled = filled; } /** Get dateCreated */ public java.util.Date getDateCreated() { return dateCreated; }

abstract class continued @Override public String toString() { return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled; } /** Abstract method getArea */ public abstract double getArea(); /** Abstract method getPerimeter */ public abstract double getPerimeter();

Association relationships between classes & Aggregation relationships between classes & Composition relationships between classes

Association Association represents a general binary relationship that describes an activity between two classes. An association is usually represented as a data field in the class.

Translation is not Unique for Association If you don’t need to know the courses a student takes or a faculty teaches, the data field courseList in Student or Faculty can be omitted.

Association Between Same Class Association (also aggregation) may exist between objects of the same class. For example, a person may have a supervisor. public class Person { // The type for the data is the class itself private Person supervisor; ... }

Aggregation Between Same Class What happens if a person has several supervisors?

Object Composition Composition is actually a special case of the aggregation relationship. Aggregation models has-a relationships and represents an ownership relationship between two objects. The owner object is called an aggregating object and its class an aggregating class. The subject object is called an aggregated object and its class an aggregated class.

Class Representation An aggregation relationship is usually represented as a data field in the aggregating class. For example, the relationship in Figure 10.6 can be represented as follows:

Aggregation and Composition Aggregation: special form of association Represents ownership relationship Aggregation models the has-a relationship. Composition: special form of aggregation object exclusively owned by aggregated object

class Person { private Heart heart; private List<Hand> hands; } class City { private List<Tree> trees; private List<Car> cars In composition (Person, Heart, Hand), "sub objects" (Heart, Hand) will be destroyed as soon as Person is destroyed. In aggregation (City, Tree, Car) "sub objects" (Tree, Car) will NOT be destroyed when City is destroyed

class StereoSystem { private boolean state ; StereoSystem() {} StereoSystem(boolean state) { this.state = state ; } System.out.println("Stereo System State: " + (state == true ? "On!" : "Off!")) ; } } class Car { private StereoSystem s ; Car() {} Car(String name, StereoSystem s) { this.s = s ; } public static void main(String[] args) { StereoSystem ss = new StereoSystem(true) ; // true(System is ON.) or false (System is OFF) Car c = new Car("BMW", ss) ; 7 } }

Aggregation and Composition Both aggregation and composition represent a whole-part (has-a/part-of) association. The main differentiator between aggregation and composition is the lifecycle dependence between whole and part.

When the whole is destroyed the part may continue to exist. In aggregation, the part may have an independent lifecycle, it can exist independently. When the whole is destroyed the part may continue to exist. For example, a car has many parts. A part can be removed from one car and installed into a different car. If we consider a business, before a car is destroyed, they remove all saleable parts. Those parts will continue to exist after the car is destroyed.

Composition is a stronger form of aggregation. The lifecycle of the part is strongly dependent on the lifecycle of the whole. When the whole is destroyed, the part is destroyed too. For example, a building has rooms. A room can exist only as part of a building. The room cannot be removed from one building and attached to a different one.

Design When we decide between aggregation and composition we need to answer the following question: can the part exist independently? For example The car-part relationship mentioned above is an aggregation, but if the business requirements state that “no part of a vehicle can be used for resale or as spare part; at the end of the amortization period the vehicle is destroyed” Then this will suggest a composition relationship between car and its parts.

Java Implementation Objects do not need to be destroyed explicitly; An object is automatically “destroyed” when it is garbage collected. An object is garbage collected when it is not referenced by any other object. Regardless of the type of relationship – aggregation or composition – the part is referenced by the whole. If the whole is destroyed (garbage collected), then the part is no longer referenced by the whole. If there is no other object referencing the part then the part is destroyed too.

The Difference Between Aggregation and Composition Is the variable that holds the part object accessible to objects other than the whole? If the answer is yes, then we have aggregation, If the answer is no then we have composition.

Aggregation - Java sample code

public class Car { private String make; private int year; private Engine engine; public Car(String make, int year, Engine engine) { this.make = make; this.year = year; this.engine = engine; } public String getMake() { return make; } public int getYear() { return year; } public Engine getEngine() { return engine; } }

public class Engine { private int engineCapacity; private int engineSerialNumber; public Engine(int engineCapacity, int engineSerialNumber) { this.engineCapacity = engineCapacity; this.engineSerialNumber = engineSerialNumber; } public int getEngineCapacity() { return engineCapacity; public int getEngineSerialNumber() { return engineSerialNumber;

Aggregation Explanation The engine object is created outside and is passed as argument to Car constructor When this Car object is destroyed, the engine is still available to objects other than Car If the instance of Car is garbage collected the associated instance of Engine may not be garbage collected (if it is still referenced by other objects)

Composition - Java sample code

public class Car { private String make; private int year; private Engine engine; public Car(String make, int year, int engineCapacity, int engineSerialNumber) { this.make=make; this.year=year; engine = new Engine(engineCapacity, engineSerialNumber); } we create the engine using parameters passed in Car constructor only the Car instance has access to the engine instance when Car instance is garbage collected, the engine instance is garbage collected too*/

public String getMake() { return make; } public int getYear() { return year; public int getEngineSerialNumber() { return engine.getEngineSerialNumber(); public int getEngineCapacity() { return engine.getEngineCapacity();