CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.

Slides:



Advertisements
Similar presentations
Inheritance Writing and using Classes effectively.
Advertisements

TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Abstract Classes and Interfaces
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
01-Intro-Object-Oriented-Prog-Alice1 Barb Ericson Georgia Institute of Technology Aug 2009 Introduction to Object-Oriented Programming in Alice.
OOP Languages: Java vs C++
Programming Games Computer science big ideas. Computer Science jargon. Show virtual dog Homework: [Catch up: dice game, credit card or other form.] Plan.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
04-Intro-Object-Oriented-In-Java1 Barb Ericson Georgia Institute of Technology Sept 2009 Introduction to Object-Oriented Programming in Java.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Introduction to Object-Oriented Programming
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Methods in Java CSC1401. Overview In this session, we are going to see how to create class-level methods in Java.
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.
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
10-Nov-15 Java Object Oriented Programming What is it?
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
CSC 142 Computer Science II Zhen Jiang West Chester University
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
CPSC1301 Computer Science 1 Chapter 8 Introduction to Processing Digital Sounds part 3.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Georgia Institute of Technology More on Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
TeachJava! 2003 Corky Cartwright Dung Nguyen Stephen Wong Charlie Reis, James Hsia, Peter Centgraf.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
1 Objects and Classes: creating simulations Powerpoint created by Beverly McGuire.
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part 2 Barb Ericson Georgia Institute of.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Georgia Institute of Technology Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah Branch King Abdulaziz University.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Barb Ericson Georgia Institute of Technology Dec 2009
CS1316: Representing Structure and Behavior
CSC 113: Computer programming II
Introduction to Java part 2
CS1316: Representing Structure and Behavior
Declaring Variables – Mod 4
Barb Ericson Georgia Institute of Technology Oct 2005
Workshop for Programming And Systems Management Teachers
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,
Barb Ericson Georgia Institute of Technology June 2005
Introduction to Java part 2
More on Creating Classes
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Barb Ericson Georgia Institute of Technology Oct 2005
More on Creating Classes part 3
Presentation transcript:

CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1

Learning Goals Computing concepts  Identifying objects and classes  Declaring a class  Declaring fields  Default field values  Inherited methods  Overriding an inherited method

Identifying Objects and Classes Object-oriented programs  Consist of interacting objects Which are defined by and created by classes To identify the objects in a task  What are the things that are doing the work or being acted upon?  How do you classify them?  What data (fields) do they need to know to do the task?  What procedures (methods) do they need?

Identifying the Objects and Classes Say that we want to write a program to simulate Bingo Each player has a bingo card, there is a ball picker that picks a bingo ball which has a letter and number on it. One way to start is to underline the nouns  Card, ball, picker, player, letter, number

Class Definition Each class is defined in a file  With the same name as the class: Student.java Class names  Are singular (Student not Students)  Start with an uppercase letter  The rest of the word is lowercase  Uppercase the first letter of each additional word The syntax for a class definition is:  visibility class Name {} Inside the class definition goes:  Fields, constructors, and methods

Class Declaration To declare a Student class  Click on the New button in DrJava Type in: public class Student { } Save it in Student.java  Click on File then Save Click the Compile All button to compile it

Student Fields A student should have a name and some grades  What type should we use for each of these? The Field name can be a String object Maybe we will have at most 20 grades and they can have a decimal point in them  Since they can have a decimal point let’s use double as the type  We could name them grade1, grade2, grade3 …  Or, we can use an array

Declaring Fields Syntax  visiblity type name;  visibility type name = expression; Usually use private for the visibility [default]  So that other classes can’t access it directly The type is any of the primitive types, a class name, or an interface name Arrays are declared with [] after the type or after the name  type[] name; or type name[]; Names start with a lowercase letter  The first letter of each additional word is uppercased

Default Field Values If you don’t specify an initial value for a field  It will get one anyway when it is created Numbers = 0 Objects = null (not referring to any object yet) boolean = false public class Student { /////////////// fields /////////////// private String name; private double[ ] gradeArray; } Initial value will be null

Testing the Student Class Add the fields to the Student class definition and compile it Try the following in the interactions pane  Student studentObj = new Student();  System.out.println(studentObj); What happens?  you may not get the exact same thing

What Happened? (Inherited Methods) When you executed  System.out.println(studentObj); The class Student was checked for a toString method  Since it didn’t have one the parent class was checked for a toString method The one in Object was executed  Which prints the hash code for the object The Student class inherited the toString method from the Object class

How Inheritance Works When a method is invoked on an object We first check for that method in the object that defines the object’s class If it isn’t there we look in the parent of that class

All Classes Inherit from Object If you don’t specify the parent class when you declare a class  The class will inherit from java.lang.Object You can specify the parent class  Add extends Parent to the class declaration public Student extends Object A declaration of public class Student Is the same as public class Student extends Object

Getting the Class An object keeps a reference to the class that created it  You can get this class with Class currClass = obj.getClass(); Each class keeps a reference to its parent class  You can get this class with Class parentClass = currClass.getSuperclass(); Try:  Student student1 = new Student();  Class studentClass = student1.getClass();  Sytem.out.println(studentClass);  Class parentClass = studentClass.getSuperclass();  System.out.println(parentClass);

Overriding an Inherited Method If a class defines a method with the same name and parameter list as an inherited method  This method will be called instead of the parent method To override Object’s toString add this one to Student: public String toString() { return “Student object named “ + this.name; }

Testing toString Compile Student.java Type the following in the interactions pane Student studentObj = new Student(); System.out.println(studentObj); What do you get this time?

Summary Object-oriented programs  Have interacting objects To decide what classes to create  Identify the objects doing the action or being acted upon And classify them (what type of thing are they?) To declare a class  public class Student {} To declare a field  private type fieldName; All classes inherit from Object  Inherit the toString() method Add a toString() method to your own classes  To override the inherited method