CS 200 More Classes Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
OOP: Inheritance By: Lamiaa Said.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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,
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
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.
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.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
What is inheritance? It is the ability to create a new class from an existing class.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Inheritance and Access Control CS 162 (Summer 2009)
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
Coming up: Inheritance
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.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Unified Modeling Language (UML)
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Inheritance ITI1121 Nour El Kadri.
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Advanced Programming in Java
Computing with C# and the .NET Framework
Continuing Chapter 11 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.
Overloading and Constructors
Chapter 9 Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Lecture 22 Inheritance Richard Gesick.
CS 302 Week 8 Jim Williams, PhD.
Chapter 9: Polymorphism and Inheritance
Advanced Programming Behnam Hatami Fall 2017.
CS 302 Week 9 Jim Williams.
Inheritance Inheritance is a fundamental Object Oriented concept
CS 200 Additional Topics and Review
Java Programming, Second Edition
More About Inheritance & Interfaces
Basics of OOP A class is the blueprint of an object.
Chapter 11 Inheritance and Polymorphism
CS 200 Creating Classes Jim Williams, PhD.
Chapter 11 Inheritance and Polymorphism Part 2
CS 200 More Classes Jim Williams, PhD.
Chapter 11 Inheritance and Polymorphism Part 1
CS 200 Creating Classes Jim Williams, PhD.
Chapter 11 Inheritance and Encapsulation and Polymorphism
CS 200 Additional Topics and Review
Lecture 6 Inheritance CSE /26/2018.
Presentation transcript:

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