CS1316: Representing Structure and Behavior

Slides:



Advertisements
Similar presentations
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Advertisements

Using Classes to Store Data Computer Science 2 Gerb.
Inheritance Writing and using Classes effectively.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
1 CS1110 Classes, wrapper classes, Vectors. 10 Feb 2012 Miscellaneous points about classes. Discussion of wrapper classes and class Vector Use the text.
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
Andrew(amwallis) Classes!
Lecture 8 D&D Chapter 9 Inheritance Date.
Object Oriented Programming
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Intro To Classes Review
Classes and Objects in Java
Java Inheritance.
Web Design & Development Lecture 5
CS 106A, Lecture 22 More Classes
CSC 143 Inheritance.
Class Structure 16-Nov-18.
CSC 113 Tutorial QUIZ I.
Java Inheritance.
Extending Classes.
Class Structure 28-Nov-18.
Introduction to Java part 2
Week 6 Object-Oriented Programming (2): Polymorphism
CS1316: Representing Structure and Behavior
Inheritance.
Class Structure 7-Dec-18.
Building Java Programs
Assignment 7 User Defined Classes Part 2
CS18000: Problem Solving and Object-Oriented Programming
Class Structure 2-Jan-19.
Classes and Objects in Java
Barb Ericson Georgia Institute of Technology Oct 2005
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
Building Java Programs
CLASSES, AND OBJECTS A FIRST LOOK
Barb Ericson Georgia Institute of Technology June 2005
Lecture 15: Inheritance II
Overview of Java 6-Apr-19.
Object-Oriented Programming
F II 8. More on Inheritance Objectives
More on Creating Classes
Chapter 11 Inheritance and Polymorphism Part 1
Barb Ericson Georgia Institute of Technology Oct 2005
Lecture 8-2: Object Behavior (Methods) and Constructors
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
Classes and Objects in Java
Java Inheritance.
Classes and Methods 15-Aug-19.
CS100J Final Class on Classes 10 February 2005
CS 240 – Advanced Programming Concepts
Presentation transcript:

CS1316: Representing Structure and Behavior Structuring Models CS1316: Representing Structure and Behavior

Story Let’s make a Student a kind-of Person Exploring classes, superclasses/subclasses, constructors, Strings, and println Let’s make a Person People have names Let’s make a Student a kind-of Person Students have ID numbers. Setting them new objects automatically How inheritance works, and how overriding works

People have names public class Person { public String name; } > Person fred = new Person(); > fred.name null > fred.name = "Fred"; "Fred"

Should we be able to rename Fred? > fred.name = "Mabel"; > fred.name "Mabel" This is the what public instance variables (sometimes called fields) allow us to do?

If we wanted to control names, can do it in accessors > Person fred = new Person(); > fred.setName("Fred"); > fred.getName() "Fred" public class Person { private String name; public void setName(String somename) {this.name = somename;} public String getName() {return this.name;} } Could check in setName(): “Are you allowed to change my name?” Could check in getName(): “Are you allowed to ask my name?”

Barney may not be bright, but is he really null? > Person barney = new Person(); > barney.getName() null How could we set up a person’s name right from the start?

Making new people have reasonable names public class Person { private String name; public Person(){ this.setName("Not-yet-named"); } public void setName(String somename) {this.name = somename;} public String getName() {return this.name;} > Person barney = new Person() > barney.getName() "Not-yet-named"

The Constructor A method with the same name as the class. It’s called when a new instance of the class is created. public Person(){ this.setName("Not-yet-named"); }

Constructors can take inputs (arguments) public class Person { private String name; public Person(){ this.setName("Not-yet-named"); } public Person(String name){ this.setName(name); public void setName(String somename) {this.name = somename;} public String getName() {return this.name;} > Person barney = new Person("Barney") > barney.getName() "Barney" > Person wilma = new Person() > wilma.getName() "Not-yet-named" > wilma.setName("Wilma") "Wilma" Both constructors exist. Java uses the right one depending on the arguments provided.

Must match inputs to some constructor > Person agent99 = new Person(99) java.lang.NoSuchMethodException: Person constructor There are constructors for no inputs and String inputs, but not for double or int inputs.

Some discourse rules for Java In a Western, the hero carries a gun, never a bazooka and never flies on a unicorn. There are “discourse rules” in different genres. There are such rules in Java. Class names start with capital letters. They are never plurals. (Want more than one of them? Make an array or a list!) Instance variables and methods always start with lowercase letters. Methods should describe verbs—what objects know how to do. Accessors are typically set- and get- the name of the field.

But what is Barney? > Person barney = new Person("Barney") Person@63a721 Looks like “Person” then cuss word…

Making the printable representation of People better public class Person { private String name; public String toString(){ return "Person named "+this.name; } toString() is called whenever an object needs to be converted to a string—such as when it is printed in the Interactions Pane > Person barney = new Person("Barney") > barney Person named Barney

Students are people, too. Let’s make Students They are Persons But they also have ID#s (Not exactly the best model in the world. Real students also have homes, friends, etc. But you only model what you need, and right now, we just need another data element.) Students are kind-of Persons Students extend Persons

Making Students public class Student extends Person { private int idnum; public int getID(){ return idnum;} public void setID(int id) {idnum = id;} }

Making Betsy > Student betsy=new Student("Betsy") java.lang.NoSuchMethodException: Student constructor > Student betsy = new Student() > betsy.getName() "Not-yet-named" > betsy.setName("Betsy") "Betsy" > betsy.setID(999) > betsy.getID() 999 Can’t make Betsy by name, because that only exists for Persons so far. Betsy does inherit the existing constructor for no input. Betsy does inherit setName and getName. And Betsy now has getID and setID

Who is Betsy? > betsy Person named Betsy Students inherit the toString() of the parent class.

Setting up Student constructors public class Student extends Person { private int idnum; // Constructors public Student(){ super(); //Call the parent's constructor idnum = -1; } public Student(String name){ super(name); public int getID(){ return idnum;} public void setID(int id) {idnum = id;} If a Student has no ID, the default now is -1. If we want to call the parent’s constructor, we must do it in the FIRST line.

Your turn: Actually try these in class Make a new constructor for Student that takes a name and an ID, so that we can say: Student mark = new Student(“Mark”,29) Make a toString() for Students so that they respond with name and ID.

Can we have a main() here? Any class can have a public static void main(String[] args) method. What would you use it for? How about for testing? Use it like we use the Interactions Pane

Getting values printed out How do we get things printed to the Interactions Pane from inside a main()? System.out.println(“Some String”)

A main() for Persons public static void main(String [] args){ Person fred = new Person("Fred"); Person barney = new Person("Barney"); System.out.println("Fred is "+fred); System.out.println("Barney is "+barney); } As long as you start with a string, you can “add” (with “+”) any object and it will be made into a string. Welcome to DrJava. > java Person Fred is Person named Fred Barney is Person named Barney

The whole Person.java public class Person { private String name; public Person(){ this.setName("Not-yet-named"); } public Person(String name){ this.setName(name); public String toString(){ return "Person named "+this.name; public void setName(String somename) {this.name = somename;} public String getName() {return this.name;} public static void main(String [] args){ Person fred = new Person("Fred"); Person barney = new Person("Barney"); System.out.println("Fred is "+fred); System.out.println("Barney is "+barney); The whole Person.java

Exploring greetings A subclass inherits all structure (data) and behavior (methods) from superclass. Unless it overrides it.

A new method for Person public void greet(){ System.out.println("Hi! I am "+this.name); } > Person bruce = new Person("Bruce") > bruce.greet() Hi! I am Bruce > Person george = new Person("George W.") > george.greet() Hi! I am George W. > Student krista = new Student("Krista") > krista.greet() Hi! I am Krista Should students greet() in the same way?

Trying to give Student a greet()

This way works public void greet(){ System.out.println("Hi, I'm "+this.getName()+ ", but I got to run to class..."); }

Changes Student, but not Person Welcome to DrJava. > Person george = new Person("George W.") > george.greet() Hi! I am George W. > Student krista = new Student("Krista") > krista.greet() Hi, I'm Krista, but I got to run to class...

Think about what happens here When we tell krista to greet() “I am a Student. Do I know how to greet()? If I don’t, I’ll ask my parent, Person, to handle it.” “I do know how to greet()! I’ll execute that method!” “Uh-oh. I don’t know how to getName(). Oh, Parent?!? Can you handle getName()?” public void greet(){ System.out.println("Hi, I'm "+this.getName()+ ", but I got to run to class..."); }