Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
General OO Concepts Objectives For Today: Discuss the benefits of OO Programming Inheritance and Aggregation Abstract Classes Encapsulation Introduce Visual.
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Web Application Development Slides Credit Umair Javed LUMS.
Reusable Classes.  Motivation: Write less code!
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Written by: Dr. JJ Shepherd
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Chapter 6 Introduction to Defining Classes
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Java – Methods Part II Lecture Notes 6. Objects, Classes and Computer Memory When a Java program is executing, the memory must hold: 1. Templates for.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
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.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Constructors & Garbage Collection Ch. 9 – Head First Java.
Chapter 5 Introduction to Defining Classes
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Object Oriented Programming
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
OOP Basics Classes & Methods (c) IDMS/SQL News
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Class Fundamentals BCIS 3680 Enterprise Programming.
1 Guide gus; Creates a “mailbox” to hold the address of a Guide object. null gus.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Classes (Part 1) Lecture 3
Agenda Warmup AP Exam Review: Litvin A2
Chapter 3: Using Methods, Classes, and Objects
Section 11.1 Class Variables and Methods
Can perform actions and provide communication
Chapter 4: Writing classes
Java Programming Language
Presentation transcript:

Computer Science 111 Fundamentals of Computer Programming I Working with our own classes

So much for learning programming?

Review of object concepts So far we have defined a class for each program. In main we get an instance of the class and work with that single object. We have also made use of classes –From Java –From TerminalIO –From BreezySwing In the spirit of decomposition and reusability, we now focus on defining our own classes and using multiple objects in a program.

Review of object concepts When we define a class, we are making a template for the objects from the class – what kind of data and behavior will the objects have. The data is stored in instance variables and the behavior is given by the instance methods An object is an instance of a class. It has –State – the current values of its instance variables –Behavior – defined by its methods

Review of object concepts When we declare a variable with type specified by a class (as opposed to a primary type like int), the variable is actually a reference to an object of the given type. There is no memory allocated for the object until the object is instantiated. We can have the variable reference an actual object by – getting a new object or – have it reference an existing object

Object Concepts MyClass myObj; myObj myObj = new MyClass(); myObj

Object Concepts MyClass myObj1 = new MyClass(); myObj1 MyClass myObj2 = new MyClass(); myObj2 myObj1 = myObj2; Note that there is no reference to the memory allocated for the first object. This will be reclaimed by Java’s garbage collector.

Mutators and Accessors These are two general categories of methods that we provide. Mutators are methods that change the object’s state; i.e., cause changes to the instance variables. An example would be the setNumber() methods to change state of GUI fields. Another example would be the updateModelFromRoll() method. Accessors are methods that return information about the state. An example would be the getNumber() methods. We provide public mutators and accessors only for the variables for which we want to allow change and access capabilities.

Class template public class extends { // Declaration of instance variables private ; … // Code for constructor methods public ( ) { // Initialize instance variables … } // Code for the other methods public ( ){ … } … }

Class template by pieces public class extends { // // Classes usually made public so usable by all clients // The class can act as server providing service to clients // // The class name is legal Java identifier. Convention is to // begin with upper case letter. // // The classes in Java form a hierarchy in a tree structure with // the class Object at the very top. Each class we write is a //subclass of some other class. If we do not specify //otherwise, it is made a direct subclass of Object. // // We specify the class we are subclassing by saying our class //extends the other class. The class we extend is called // the parent of our class, and our class is its child. The //child inherits the characteristics of the parent.

Class template by pieces // Declaration of instance variables private ; … // We almost always make the instance variables private. // This keeps the users of our objects from directly // accessing these variables. We provide mutators // and accessors to do exactly what we allow and // nothing more. In fact, the instance variables and // their types need not be known. This allows for // implementation changes that do not effect client code. // This is referred to as information hiding.

Class template by pieces // Declaration of instance variables private ; … // As an example, suppose we are writing a class for // some kind of temperature class. We might provide // methods for giving the temperature in Fahrenheit or // in Celsius, etc. There would be no need to store // the value in both systems. If we provided access to // an instance variable called fahrenheit, then all code // using this access would be dependent on our keeping // that implementation of our class. If, instead, we // provided access methods called getFahrenheit and // and getCelsius, we could change the implementation // without problems with the client code.

Class template by pieces // Code for constructor methods public ( ) { // Initialize instance variables … } // Note that a constructor has the same name as the class. // Note also that a constructor does not have a return type. // A constructor is always made public. If not specified, this // is the default. // A constructor is executed when a new object is instantiated. // You can have constructors with parameters. // You can have more than one constructor – overloaded

Class template by pieces // Code for the other methods public ( ){ … } // Most of our methods will probably be public so they can be //used by various clients. // However, we still should make methods private if they //are “helper” methods for internal use by the objects of //the class we are defining.

Class template by pieces public class Temperature extends Object { private double fahrenheit; … public Temperature( ){ fahrenheit = 0.0; } public Temperature(double aTemp){ fahrenheit = aTemp; } … } … Temperature temperature1 = new Temperature( ); Temperature temperature2 = new Temperature(100.0);

toString methods We usually provide a method named toString that returns some string representation of the object. –This method has no parameters. –It can be called explicitly like other methods. writer.println(MyObj.toString()); –However it is also called explicitly when we use string concatenation or use println with the object name itself. –str = str + “Today” + myObj.toString(); is equivalent to: str = str + “Today” + myObj;

Example – Course Class

Example – Course Class (cont.)