1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected.

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

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.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
Chapter 6. Overloading Methods and Constructors  Two or more methods in a class may have the same name as long as their parameter lists are different.
Our BankAccount class should define three methods  deposit  withdraw  getBalance How a programmer will carry out these operations?  We assume that.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
Road Map Introduction to object oriented programming. Classes
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
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.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance Motivation –Code reuse –Conceptual modeling.
Writing Classes (Chapter 4)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Introduction to Object-Oriented Programming
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.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Classes CS 21a: Introduction to Computing I First Semester,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Classes All Java code must be inside classes Class types – Utility classes Used to store constants, utility methods, etc. – Driver classes – Abstract Data.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CSC 142 Computer Science II Zhen Jiang West Chester University
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
93 4/11/98 CSE 143 Class Constructors [Sections ]
AOP with AspectJ Awais Rashid, Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 3 Implementing Classes
Comp1004: Building Better Objects II Encapsulation and Constructors.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Lecture 3 John Woodward.
Implementing Classes Yonglei Tao.
Chapter Three - Implementing Classes
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Encapsulation and Constructors
More on Classes and Objects
JAVA CLASSES.
Java Programming Language
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Chapter 11 Inheritance and Polymorphism Part 1
Dr. R Z Khan Handout-3 Classes
Classes.
Presentation transcript:

1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected l Mutator and Accessor methods

2 Classes And Objects In Java, problems are solved by interaction among objects, with each object providing services in its area of specialization (through its methods) to other objects. – hence, Java is called an Object- Oriented Language. Objects are created from classes, thus, to write Java programs one must learn how to write classes. A class usually consists of fields (variables) and methods. These collectively are called class members.

3 Class and Objects Example class BankAccount { public double balance = 0.0; public void deposit(double amount ) { balance += amount ; } class RunBank { public static void main(String args[]) { BankAccount richStudent = new BankAccount(); BankAccount poorInstructor; poorInstructor = new BankAccount(); richStudent.deposit(10000); poorInstructor.deposit(5.10 ); System.out.println("Student Balance:" + richStudent.balance ); System.out.println("Prof Balance: " + poorInstructor.balance)); }

4 Constructors l A class may also have one or more constructors, one of which is used to initialise the fields when an object of the class is created. l If a class has more than one constructor, then each constructor must have a different signature – number, type and/or position of parameters.

5 Constructor Example class BankAccount { public double balance; //Constructor public BankAccount(double initialBalance) { balance = initialBalance; } public BankAccount() { //Constructor balance = 0.0; } public void deposit(double amount) { balance += amount ; } public String toString() { return "Account Balance: " + balance; }

6 Constructor Example class RunBank { public static void main( String args[] ) { BankAccount richStudent = new BankAccount(10000); BankAccount poorInstructor = new BankAccount(); poorInstructor.balance = 100; System.out.println( "Prof: " + poorInstructor ); System.out.println( "Student: " + richStudent ); }

7 Constructor Example l Notice the addition of toString() method in the above example. l This is a special method which java automatically called to convert objects to strings where necessary. l This happens in println() and when adding objects to strings

8 Implicit Constructors l If a class has no constructor, the compiler generates a default (or implicit) constructor for the class. The default constructor initializes those fields of the object that are not already initialised with their default values. l The default values for the different types are shown in the table below TypeDefault value booleanfalse byte(byte) 0 short(short) 0 int0 long0L char \u0000 float0.0f double0.0d object referencenull

9 Overloading Methods l The signature of a method is its name with the number, type and order of its parameters. l The return type is not part of the signature of a method. l Two methods in the same class can have the same name if their signatures are different.

10 Overloading Methods Examples class OverLoad { public void same() { System.out.println( "No arguments" ); } public void same( int firstArgument ) { System.out.println( "One int arguments" ); } public void same( char firstArgument ) { System.out.println( "One char arguments" ); } public int same( int firstArgument ) { // Compile Error System.out.println( "One int arguments" ); return 5; } public void same( char firstArgument, int secondArgument) { System.out.println( "char + int arguments" ); } public void same( int firstArgument, char secondArgument ) { System.out.println( "int + char arguments" ); }

11 this (not that) l this refers to the object on which the method operates l It is used to pass self as a parameter to methods operating on the object. class BankAccount { public double balance; public BankAccount(double balance) { this.balance = balance; } public void deposit(double amount) { balance += amount ; } public String toString() { return "Account Balance: " + balance; }

12 Another example: class CustomerList { public BankAccount[] list = new BankAccount[100]; public int nextFreeSlot = 0; public void add(BankAccount newItem ) { list[nextFreeSlot] = newItem; nextFreeSlot++; } class BankAccount { public double balance; public BankAccount(double balance ) { this.balance = balance; } public void badBalanceCheck( CustomerList badAccounts) { if ( balance <= 0 ) badAccounts.add(this); }

13 Another example cont.. class RunBank { public static void main( String args[] ) { BankAccount richStudent = new BankAccount( ); CustomerList customersToDrop = new CustomerList(); richStudent.badBalanceCheck( customersToDrop); }

14 public, private and protected. l Most of the methods of a class are meant to provide services to other objects, thus they should be declared as public. However, the fields should be declared as private or at best protected so that only methods of the class or subclass of this class can change them. l A better BankAccount class is shown below.

15 public, private and protected. class BankAccount { private double balance; public BankAccount(double initialBalance) { balance = initialBalance; } public BankAccount() { balance = 0.0; } public double getBalance() { return balance; } public void setbalance(double newBalance) { balance = newBalance; } public void deposit(double amount) { balance += amount ; } public String toString() { return "Account Balance: " + balance; }

16 Mutator and Accessor Methods l Methods that allow changes to be made to fields of the class are called mutator methods (e.g deposit and setBalance) l Methods that only access the fields but do not change them are called accessor methods.