CS 200 More Classes Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Advertisements

Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Topic 4 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
UML Basics & Access Modifier
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Writing Classes (Chapter 4)
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Chapter Outline What inheritance is Calling the superclass constructor Overriding superclass methods Protected members Chains of inheritance The Object.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Object Oriented Programming
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,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
1 More About Derived Classes and Inheritance Chapter 9.
Chapter 11 Inheritance and Polymorphism
Abstract Classes, Abstract Methods, Override Methods
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Chapter 15 Abstract Classes and Interfaces
Inheritance ITI1121 Nour El Kadri.
Abstraction, Interface, Inheritance, Polymorphism, Override / Overload
Chapter 11 Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Java Inheritance.
Chapter 8 Classes and Objects
Chapter 5 Hierarchies IS-A associations superclasses subclasses
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CS Week 14 Jim Williams, PhD.
CS 302 Week 11 Jim Williams, PhD.
CS Week 13 Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
Chapter 9 Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Lecture 22 Inheritance Richard Gesick.
CS 200 More Classes Jim Williams, PhD.
Chapter 9: Polymorphism and Inheritance
Week 6 Object-Oriented Programming (2): Polymorphism
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Implementing Non-Static Features
Advanced Programming Behnam Hatami Fall 2017.
Topic 4 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“
CS 302 Week 9 Jim Williams.
Inheritance Inheritance is a fundamental Object Oriented concept
CS 200 Additional Topics and Review
CS 200 Additional Topics and Review
Lecture 18: Polymorphism (Part II)
Chapter 11 Inheritance and Polymorphism
CS 200 Creating Classes Jim Williams, PhD.
Chapter 8 Class Inheritance and Interfaces
CS 200 Creating Classes Jim Williams, PhD.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Lecture 8 Object Oriented Programming (OOP)
CS 200 Additional Topics and Review
CSG2H3 Object Oriented Programming
Presentation transcript:

CS 200 More Classes Jim Williams, PhD

Week 13 Team Lab: Instantiable Classes, Javadoc BP2: M3 due Thursday Submit to zyBooks M2 tests Follow the specification Do any extensions or improvements, after you turn in. Lecture: More Instantiable Class Basics

Will the default constructor be provided in this case? class Cup { private String contents; Cup(String contents) { this.contents = contents; } yes no compile time error runtime error try it and see.

Accessor/Getter Methods Methods to retrieve information Provide read access to information within the instance/object. String getName() { } String name() { } boolean isHappy() { }

Mutator/Setter Methods Allow user to change data/state of an object. Provide write access to data. void setName( String newName) { } void name( String name) { } void happy( boolean happy) { }

this implicit parameter Available within instance methods. Refers to the instance used to call the method. String str = "hello". System.out.println( str.charAt(0)); Within the charAt instance method, this keyword refers to the reference in str.

this implicit parameter class Car { String owner; void setOwner( String owner) { this.owner = owner; } in main: Car mine = new Car(); mine.setOwner("me");

Is an instance of Car mutable? public class Car { private String model; public Car(String model) { this.model = model; } public String toString() { return model;

Encapsulation Enclosing something Internal/Developer vs External/Users access Use visibility and getters and setters to allow appropriate user access Data should be Private Provide getters/accessors for read access Provide setters/mutators for write access Provide constructors to initialize on creation

Demo Creating a class with instance variables Instantiating a class Writing and calling a constructor Writing and calling a getter/accessor

Rectangle Design a Rectangle class Fields: width & height as double with default of 1.0 and private Constructors: no-arg constructor & a constructor with specified width and height, public Methods: getArea() and getPerimeter(), public Draw a UML diagram for the class then implement the class. Write a Shapes program that: Creates 2 rectangles (4 by 10) and (3.5 by 25.4) Display width, height, area and perimeter public class Rectangle { //implicitly extends Object //instance variables aka fields and attributes private double height; private double width; private static int count = 0; //class variable aka class fields and attributes public Rectangle() { //no-arg constructor this( 1.0, 1.0); //call the 2-arg constructor } public Rectangle( double size) { //constructor this( size, size); //call the 2-arg constructor public Rectangle(double height, double width) { //constructor this.height = height; this.width = width; count++; //class (static) variables can be accessed from instance methods and constructors public double getArea() { //derived field return height * width; public double getPerimeter() { //another derived field return height * 2 + width * 2; public double getHeight() { return this.height; //setter/mutator for height that validates any changes to height public void setHeight(double newHeight) { if ( newHeight > 0.0) { this.height = newHeight; //accessor for class field public static int getCount() { //instance fields cannot be accessed from class (static) methods //as which instance isn't defined - no implicit this parameter return count; //overriding the same toString() method inherited from Object public String toString() { return "Rectangle height=" + height + " width=" + width; import java.util.ArrayList; public class Shapes { public static void main(String[] args) { //user of the Rectangle class //since this Shapes class is in the same package (default package) //as the Rectangle class then all non-private members of Rectangle //would be accessible. ArrayList<Rectangle> list = new ArrayList<>(); list.add( new Rectangle()); list.add( new Rectangle( 4.0, 10.0)); System.out.println( "count: " + Rectangle.getCount()); for ( Rectangle shape : list) { System.out.println( shape.getArea());

Visibility Modifiers For members of a class: public private protected <package> Package Demo

Can methodA call methodB? //classes in different files in same package class A { public void methodA() { B b = new B(); b.methodB(); } class B { void methodB() { yes no depends error try it and see.

Can a method outside the package call methodA()? //classes in different files in same package class A { public void methodA() { B b = new B(); b.methodB(); } class B { void methodB() { yes no depends error try it and see

More Classes The Object class Derived classes Overriding methods Polymorphism ArrayLists of Objects is-a vs has-a

Object class Base class for all other classes Every class in Java directly or indirectly extends (inherits) from Object. class Picture { //implicitly extends Object } class Picture extends Object {

Derived Classes Since all classes extend from Object they inherit: public String toString() public boolean equals(Object obj)

Overriding Replacing inherited methods, may call super class method class Picture { @Override //compiler directive public String toString() { return "Picture: " +super.toString(); }

Equals - What does equals mean? Depends on the implementation in the class. class Picture { public boolean equals(Object obj) { //what do equal Picture objects mean? return ???; }

Polymorphism (runtime) At runtime determines the correct method to call based on the actual object. Object o = new Picture(); System.out.print( o.toString()); Is Object's or Picture's toString method called?

ArrayLists of Objects Demo polymorphism using ArrayList

is-a vs has-a Relationships is-a: inheritance class Dog extends Animal { } has-a: composition class Car { Engine engine; Wheel [] wheels; }

Shapes Add Circle Class to Shapes program: Field: radius Constructor: radius is the argument Methods: getArea(), getCircumference(), toString() Recall: Area = π * radius * radius; Circumference = π × diameter Add Circle instances/objects to Shapes Draw UML Diagrams

Lots of Jobs if you understand Programming Software Developer/Programmer/Software Engineer Testing/Quality Assurance Technical Support Technical Writing Business/Systems Analyst Software Architect Product Manager Project Manager Development Manager Technical Sales Marketing

Analysis and Design Analysis: Understanding the Problem Design: Preparing a Solution Diagramming can be useful for both.

UML Diagrams Goal in CS 200 is to recognize: Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow

UML Diagrams: References Examples of Diagrams: https://www.uml-diagrams.org/ Simple Drawing Tool: http://www.umlet.com/umletino/umletino.html

Use Case Behavior diagram How external users (actors) interact with the system. https://www.uml-diagrams.org/use-case-diagrams.html

Class Diagram Shows structure of a system with features, constraints and relationships. Independent of time. https://www.uml-diagrams.org/class-diagrams-overview.html

Object Diagram A snapshot of a system at a point in time. Instances of classes with specific attribute values. https://www.uml-diagrams.org/class-diagrams-overview.html

Activity Diagram (Control Flow) Behavior diagram Shows flow of control emphasizing sequence and conditions https://www.uml-diagrams.org/activity-diagrams.html

What Kind of Diagram is this? Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow

What Kind of Diagram is this? Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow

What Kind of Diagram is this? Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow

What Kind of Diagram is this? Use Case Diagram Class Diagram Object Diagram Activity Diagram/Control Flow