43
Classes, Objects, and OOP in Java June 16, 2017

Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Classes, Objects, and OOP in Java

June 16, 2017

Page 2: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Which is a Class in the below code?

A. Mario

B. itsAMe

C. new

D. “Red Hat?”

Mario itsAMe = new Mario(“Red Hat?”);

Page 3: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Whats the difference?

A. One is type, the other is not

B. One is a reference, the other is a Class

C. One is more efficient than the other

D. One is Java, the other is C

int vs. Integer

Page 4: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Whats the difference?

A. One is a reference, one is a primitive

B. One is Unicode, the other is ASCII

C. One is an array, the other is type “String”

D. One is type “char”, the others of type “String”

char[] lang = {“j”, “a”, “v”, “a”}; String lang = “java”;

Page 5: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Which evaluate to True?

A. first == tied

B. first.isEqual(tied)

C. first.getClass() == tied.getClass()

D. first == String && second == String

String first = “First”; String tied = “First”;

Page 6: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Which are Synonyms?

A. Object and Instance

B. Type and Class

C. int and Integer

D. static and final

Page 7: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Which are Knowable?

A. getPurposeOfLife() returns an object

B. DanceGod is “final”

C. getPurposeOfLife is “public”

D. DanceGod is a class

GIVEN _only_ this information:1. getPurposeOfLife is a static method 2. DanceGod.getPurposeOfLife() is valid

Page 8: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Which is a true difference between functions in C, and methods in Java?

A. Functions do not declare their return type, methods do

B. Methods in java can take fewer arguments that functions in C

C. Methods in java are tied to classes, functions in C are tied to structs.

D. Java allows named parameters, C does not

Page 9: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

What is knowable about visibility

A. Syntax error

B. is public is visible only to static methods

C. isPublic is visible to other objects

D. isPublic cannot be changed

public class PublicClass { bool isPublic = true; }

Page 10: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

What is a Constructor

A. An uncommon feature from older versions of java

B. How code configures functionality to trigger when an object is constructed

C. How Java determines the type of the object to be constructed

D. A class that creates other classes

Page 11: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Done!

Page 12: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Housekeeping

• Homework 1 has passed

• Homework 2 over the weekend

• Course topics / schedule

• Class reworking

Page 13: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

OOP and Java

• Goals

• History of OOP

• OOP in Java

Page 14: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Why OOP? (Goals)• “Text Book”

• Encapsulation (Review today)

• Polymorphism (Most of today)

• Inheritance (Today and Monday)

• ¯\_(ツ)_/¯

Page 15: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Problem OneFunctions and Data

struct band { char * name; person members[10]; int num_members; };

person band_tallest_member(band *a_band) {…}

// Adding a new member?

// Wanting to do so something else with a “band” (ex, if we // want to test if “MF Doom” was in the band)

// Default values for bands?

Page 16: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Encapsulation

• Binding together functionality and data

• Hiding the inner workings of code

• Prevent “boiler” plate, setup code

• Integrity

Page 17: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Problem Two Similar Data

struct band { char * name; person members[10]; int num_members; };

person band_tallest_member(band *a_band) {…}struct company { char * name; person members[10]; int num_members; int age; };

// Finding tallest member of the company?

Page 18: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Polymorphism

• Abstracting over similar things

• Maintaining type information

• From a “consumer” / “client” / “user” perspective

Page 19: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Problem ThreeSimilar Functionality on Datastruct band { char * name; person members[10]; int num_members; (some code to calculate tallest person)};

struct company { char * name; person members[10]; int num_members; int age; (some code to calculate tallest person)};

// How to not write the same thing twice

Page 20: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Inheritance

• Defining type hierarchies

• Shoving functionality into parent types

• Common, but not required, in OOP

Page 21: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Java and OOP• Class is everything

• Types

• Functionality taxonomy

• Data taxonomy

• Namespaces

• Functions (sorta…)

• Quirk of Java

Page 22: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Encapsulation in Java

• Define a class

• Define some data the class

• Define some functionality that uses that class

Page 23: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Encapsulation in Javastruct band { char * name; person members[10]; int num_members;};

person band_tallest_member(band *a_band) {…}

Page 24: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Encapsulation.java

Page 25: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Encapsulation in Java

• “this” makes things unambiguous

• public / private controls access

• constructors can create defaults

• Explicit is better than implicit

Page 26: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Polymorphism in Java

• Class hierarchy

• Interfaces (later)

• Generics (even later)

Page 27: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Class Hierarchy

Company

Band

-getTallestMember

-getTallestMember-getCompanyAge

Page 28: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Class Hierarchy

Company

Band

-getTallestMember

-getTallestMember-getCompanyAge

PeopleDoingThingsTogether

Page 29: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Polymorphism.java

Page 30: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Inheritance in Java

• Sharing code between types

• Check child classes first, then parents, then parents of parents, then…

• “extends"

Page 31: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Class Hierarchy

Company

Band

-getTallestMember

-getTallestMember-getCompanyAge

PeopleDoingThingsTogether

Page 32: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Class Hierarchy

Company

Band

-getTallestMember

-getCompanyAge

PeopleDoingThingsTogether

Page 33: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Class Hierarchy

Company

Band

-getTallestMember

-getCompanyAge

PeopleDoingThingsTogether

-getTallestMember

Page 34: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Class Hierarchy

Company

Band

-getTallestMember

-getCompanyAge

PeopleDoingThingsTogether

-getTallestMember

PettyCompanies

ReasonableCompanies

Page 35: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Inheritance in Java

• Start with most specific class

• Keep looking “up” until we find a match… then stop

• `MethodNotFoundException` or compiler error if no chance

Page 36: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Further Code Sharing• Child methods will often be similar…

• We want D.R.Y. code

• How to “slightly" change parent implementations

• “super” and “this”

• “Abstract”

Page 37: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

InheritanceSuper.java

Page 38: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Java OO Particularities

• Arguments are part of a method’s signature

• Disambiguation happens at runtime

• Static / final / scoped classes etc.

Page 39: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red
Page 40: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Programming, Step 1

• Groups

• Brainstorm class hierarchy

• Three tiers deep, at least 4 types

• Examples of three methods, at least one for each level of hierarchy

Page 41: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Programming, Step 2• Describe a class hierarchy

• Should have at least the following classes

• Animals

• Cats

• Lizards

• ColdBlooded

• WarmBlooded

• Dogs

• Food needs

Page 42: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Programming, Step 3

• Implement simple program that sorts user provided animals into bins, depending on their needs

• getName()

• main(String[] args)

• Print out summary

Page 43: Classes, Objects, and OOP in Javapsnyder/cs342-summer2017/... · 6/16/2017  · OOP in Java June 16, 2017. Which is a Class in the below code? A. Mario B. itsAMe C. new D. “Red

Wrap Up

• Coding good? Slides better?

• Homework 2

• Reading for Monday

• Office hours… now!