CS 200 More Classes Jim Williams, PhD
Week 13 Team Lab: Instantiable Class BP2 Milestone 3 Due Thursday P7 Due next Thursday CS 300 Programming II in the future? Lecture: More Classes, UML Diagrams
Class as a template A template for objects/instances. attributes/fields accessors/getters mutators/setters constructors this (implicit parameter) public, private, protected, <package>
Default Constructor class Person { private String name; } Can we create an instance? Person p = new Person();
Constructor Overloading class Person { private String name; Person() { this("no name"); } Person(String name) { this.name = name;
Constructors yes - the default, no argument constructor yes - a really good one. no error If there is no constructor in a class is a constructor automatically provided by the compiler? best answer: A, yes - the default, no argument constructor.
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.
Visibility Modifiers For members of a class: public private protected <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
We will fill the blackboard today with diagrams. Move closer to the front if you can't clearly see.
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/ http://agilemodeling.com 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