CLASSES AND OBJECTS Object-Oriented Basics. Vocabulary Review – C++ Class Object Instance this new Constructor Default constructor Access (private/public/protected)

Slides:



Advertisements
Similar presentations
1 Inheritance Classes and Subclasses Or Extending a Class.
Advertisements

Classes and Objects in Java
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Inheritance Writing and using Classes effectively.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Road Map Introduction to object oriented programming. Classes
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,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Arrays and Objects OO basics. Topics Basic array syntax/use OO Vocabulary review Simple OO example Instance and Static methods Static vs. instance vs.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Internet Software Development Classes and Inheritance Paul J Krause.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
1 Software Construction Lab 4 Classes and Objects in Java Basics of Classes 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!
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Classes All Java code must be inside classes Class types – Utility classes Used to store constants, utility methods, etc. – Driver classes – Abstract Data.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
GP3, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 3 We start at 13:00 Slides are available from the course home page Feel free to.
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.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Session 7 Methods Strings Constructors this Inheritance.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
1 Object-Oriented Software Engineering CS Java OO Fundamentals Contents Classes and Objects Making new objects Method declarations Encapsulating.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Chapter 3 Implementing Classes
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
COP Topics Java Basics - How to write & call your own method - Math methods (static methods) - String methods (instance methods) - Errors Classes.
Defining Your Own Classes II
Static data members Constructors and Destructors
Simple Classes in C# CSCI 293 September 12, 2005.
Encapsulation and Constructors
JAVA Constructors.
Basics of OOP A class is the blueprint of an object.
Session 2: Introduction to Object Oriented Programming
Chapter 11 Inheritance and Encapsulation and Polymorphism
ITE “A” GROUP 2 ENCAPSULATION.
Chengyu Sun California State University, Los Angeles
Presentation transcript:

CLASSES AND OBJECTS Object-Oriented Basics

Vocabulary Review – C++ Class Object Instance this new Constructor Default constructor Access (private/public/protected) static parent/child; superclass/subclass abstract class/pure virtual function

Object-Oriented ConceptReview Encapsulation – hiding unimportant details Black box – something that magically “does its thing” Abstraction – taking away inessential features Example: car –if engine control module fails, replace it –mechanic can provide inputs, test outputs –doesn’t need to know how it works OO challenge: finding the right abstractions (what “black boxes” do we need to solve the problem at hand?)

Simple Class public class Book { // static = one copy per class // final = constant, can't change public static final int MAX_HIGHLIGHTS = 100; // these are INSTANCE variables // may also be called ATTRIBUTES or MEMBER variables // In CSCI306, MUST be private unless you have a compelling argument otherwise private int numPages; private int[] highlights; private int numHighlights; private int currentPage; public Book(int numPages) { super(); // covered in more detail under Inheritance this.numPages = numPages; currentPage = 1; // Notice use of constant, avoid 'Magic constants' // highlights = new int[100]; - NOT GOOD!!!!! highlights = new int[MAX_HIGHLIGHTS]; }

Continued public void addHighlight() { highlights[numHighlights] = currentPage; numHighlights++; } public void addHighlight(int whichPage) { highlights[numHighlights] = whichPage; numHighlights++; } public void printHighlights() { System.out.println("Highlighted pages\n"); for (int page=0; page<numHighlights; page++) System.out.println(highlights[page]); }

Continued //Setters and getters by Eclipse public static void main(String[] args) { Book aBook = new Book(300); aBook.setCurrentPage(15); aBook.addHighlight(); aBook.addHighlight(32); aBook.printHighlights(); }

Methods in Java Instance –called with an object –most methods are of this type –able to access/update object (instance) data Class/static – called with the class name –also known as static –no object, so no object data… must pass needed data in as parameters –don’t make a static method unless you have a reason –main is static – why?

More Instance Method Examples public class MyPoint { private int x, y; public MyPoint(int x, int y){ this.x = x; this.y = y; } public int getX() { return x; } } // in main MyPoint p1 = new MyPoint(3, 4); MyPoint p2 = new MyPoint(5, 6); System.out.println(p1.getX()); System.out.println(p2.getX()); notice that you call an instance method using an object constructor – just like C++

Static Method Examples Existing class: System.out.println(Math.round(x + y)); Example of creating your own: public class Financial { public static double percentOf(double p, double a) { return (p/100) * a; } double tax = Financial.percentOf(taxRate, total); Call with class name, not instance. NOTE: inside class, don’t need class name All data is passed in, no “owned” variables numerous Math functions static keyword

Variables Instance – each object has its own copy Class/static – one copy per class. Often used with final. Local – only exists inside the function

BankAccount Example lastAccountNum public class BankAccount { private static int lastAccountNum = 0; private String name; private double balance; private int accountNum; // methods here } BankAccount mine = new BankAccount(“Jim”, 5000); BankAccount yours = new BankAccount(“Sally”, 10000); just one copy of lastAccountNum Jim Sally mineyours

Variables Instance variables –each object has its own copy (as in C++) –objects are automatically garbage collected (unlike C++!) –fields are initialized with a default value (e.g., 0, null) if not explicitly set in constructor. Still good to do your own initialization. –NullPointerException if you forget to call new Local variables –must be explicitly initialized –error if you use before initializing (unlike C++) –only exist inside method (as in C++)

Instance vs Static vs Local – Which to Use? Use instance variables for data that “belong” to one instance of the class and will be operated on by multiple methods Use local variables for temporary calculations within a method Use static variables ONLY for constants and values that are clearly shared between objects. If you don’t have a reason for static, don’t use it! Use static methods for utility methods Common mistake – use static just to get rid of compiler errors

Intance vs Local vs Static Example public class Financial{ public static double percentOf(double p, double a) { return (p/100) * a; } public class BankAccount { private double balance; private double intRate; // addInterest updates balance & returns amt of interest public double addInterest() { double interest = balance * intRate; balance += interest; return interest; } Common errors: Making all variables instance, even if only used in one function. Making all variables static – like using globals! instance variables, can access inside all instance methods local variable: interest

DON’T DO THIS!! public class SomeClass { public static int count; public static void update() { count++; } public static void main(String[] args) { count = 0; //.. Some stuff update(); //.. Some stuff update(); System.out.println(count); }

DO SOMETHING LIKE THIS! public class SomeClass { private int count; // let Eclipse do getter/setter public void update() { count++; } public static void main(String[] args) { SomeClass sc = new SomeClass(); // not needed, done in constructor: count = 0; //.. Some stuff sc.update(); //.. Some stuff sc.update(); //System.out.println(count); System.out.println(sc.getCount();); }