© A+ Computer Science - www.apluscompsci.com OOP © A+ Computer Science - www.apluscompsci.com.

Slides:



Advertisements
Similar presentations
JAVA Revision Lecture Electronic Voting System Marina De Vos.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Computer Science and Engineering College of Engineering The Ohio State University Classes and Objects: Members, Visibility The credit for these slides.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
ECE122 Feb. 22, Any question on Vehicle sample code?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Topics Instance variables, set and get methods Encapsulation
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Class Fundamentals BCIS 3680 Enterprise Programming.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Objects as a programming concept
Classes (Part 1) Lecture 3
OOP Powerpoint slides from A+ Computer Science
Intro To Classes Review
Chapter 3: Using Methods, Classes, and Objects
Creating Your OwnClasses
CS 302 Week 11 Jim Williams, PhD.
Multiple Choice -answer the easiest question 1st
Defining Classes and Methods
Lesson A4 – Object Behavior
Creating Objects in a Few Simple Steps
Classes & Objects: Examples
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Classes, Encapsulation, Methods and Constructors (Continued)
Today’s topics UML Diagramming review of terms
© A+ Computer Science - Classes And Objects © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
PreAP Computer Science Quiz Key
Instance Method – CSC142 Computer Science II
CIS 199 Final Review.
Defining Classes and Methods
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Dr. R Z Khan Handout-3 Classes
A+ Computer Science PARAMETERS
A+ Computer Science AP Review 2019 AP CS A EXAM
CSG2H3 Object Oriented Programming
Presentation transcript:

© A+ Computer Science - www.apluscompsci.com OOP © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Pieces of the OOP Puzzle Review © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com constructors public Triangle() { setSides(0,0,0); } Default Constructor Constructors are typically used to initialize all of the Object’s data/properties. The default constructor usually sets all data/properties to a zero value. The exact zero value depends on the specified type of each instance variable. Constructors are similar to methods. Constructors set the properties of an object to an initial state. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com constructors public Triangle(int a, int b, int c) { setSides(a,b,c); } Initialization Constructor Constructors are typically used to initialize all of the Object’s data/properties. The initialization constructor usually sets the data/properties to a provided parameter value. Constructors are similar to methods. Constructors set the properties of an object to an initial state. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com modifier methods public void setSideA(int a) { sideA=a; } Set methods are modifier methods. Set methods are used to change the data/properties of an Object. Modifier methods are methods that change the properties of an object. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com accessor methods public int getSideA() { return sideA; } Accessor methods are used to retrieve the data/properties from the Object. Get methods are accessor methods. Accessor methods do not make changes to the data/properties. Accessor methods are methods that retrieve or grant access to the properties of an object, but do not make any changes. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com accessor methods public String toString() { return "" + getSideA() + //more get calls } The toString() method is an accessor method. The toString() method should return all data/properties. The toString() should not change the data/properties of the Object. Accessor methods are methods that retrieve or grant access to the properties of an object, but do not make any changes. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com encapsulation All data members should have private access. The public constructors, accessor methods, and modifier methods should be used to manipulate the data. All data is tucked away nicely inside the class. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com encapsulation The public methods give you access to an object’s private data / properties. getIt( ) private data / instance variables / properties Class/ Object setIt( ) toString( ) © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Private/Public © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com What does public mean? All members with public access can be accessed inside and outside of the class where they are defined. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com What does private mean? All members with private access can only be accessed inside of the class where they are defined. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Constructors © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Constructors If you do not provide any constructors, Java will provide a default constructor. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com equals © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com The equals() method The equals() method is used to see if two objects have the same contents. String one = "comp"; String two = "sci"; out.println(one.equals(two)); To determine if two Objects are the same, the data/properties of both Objects must be compared. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com class Monster { private int height; //methods public boolean equals(Monster two){ if(getHeight()==two.getHeight()) return true; return false; } //test code in the main Monster m1= new Monster(33); Monster m2 = new Monster(12); out.println(m1.equals(m2)); equals() To determine if two Objects are the same, the data/properties of both Objects must be compared. Monster contains a height property only. Monsters are compared by comparing the heights. If the heights of two Monsters are the same, the Monsters are considered equal. OUTPUTfalse © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Overloading Overloading occurs when you have more than one method or constructor with the same name. Each method or constructor must have a different parameter list. # of parameters && data types matter © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com class Monster{ private int height; //default assinged to 0 private double weight; //default assinged to 0 public Monster(){ height=0; weight=0.0; } public Monster(int ht){ height=ht; public Monster(double wt){ weight=wt; public Monster(int ht, double wt){ Overloading The Monster constructor has been overloaded as it appears 4 times. Each time Monster() is written, a different set of parameters is provided. Java can differentiate between the Monster() constructors by the parameter list. © A+ Computer Science - www.apluscompsci.com

Monster Object Diagram Monster() - constructors setX( ) - modifiers getX( ) - accessors toString( ) - accessor Monster © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com class Monster { //instance vars / data fields public Monster(){ //code } public void setX( params ){ public int getX(){ public String toString() { constructor modifier accessor accessor © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Constructors class Monster{ // instance variables public Monster(){ code } public Monster( int ht ) { code } public Monster(int ht, int wt) { code } public Monster(int ht, int wt, int age) { code } //more methods } © A+ Computer Science - www.apluscompsci.com

Monster Instantiation 1 © A+ Computer Science - www.apluscompsci.com Monster m = new Monster(); m MONSTER Properties – height – 0 weight - 0 age - 0 methods m is a reference variable that refers to a Monster object. © A+ Computer Science - www.apluscompsci.com

Monster Instantiation 2 © A+ Computer Science - www.apluscompsci.com Monster m = new Monster(23); 0x234 m MONSTER Properties – height – 23 weight – 0 age - 0 methods 0x234 m is a reference variable that refers to a Monster object. © A+ Computer Science - www.apluscompsci.com

Monster Instantiation 3 © A+ Computer Science - www.apluscompsci.com Monster m = new Monster(23, 45); 0x239 m MONSTER Properties – height – 23 weight – 45 age - 0 methods 0x239 m is a reference variable that refers to a Monster object. © A+ Computer Science - www.apluscompsci.com