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.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming with Java
1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Jan Object Oriented Programming Yangjun Chen Dept. Business Computing University of Winnipeg.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 8 More Object Concepts
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
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.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Programming in Java CSCI-2220 Object Oriented Programming.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
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 3 Introduction to Classes and Objects Definitions Examples.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Topics Instance variables, set and get methods Encapsulation
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
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.
OOP Basics Classes & Methods (c) IDMS/SQL News
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Object Oriented Development, Abstraction & Inheritance
Java Primer 1: Types, Classes and Operators
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3: Using Methods, Classes, and Objects
Week 8 Lecture -3 Inheritance and Polymorphism
Nested class.
Chapter 9 Inheritance and Polymorphism
Corresponds with Chapter 7
Java Inheritance.
Basics of OOP A class is the blueprint of an object.
Chapter 14 Abstract Classes and Interfaces
Topics OOP Review Inheritance Review Abstract Classes
CSG2H3 Object Oriented Programming
Chapter 5 Classes.
Presentation transcript:

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 instances of the class Classes have two parts  Fields – which describe what the class is  Methods – which describe what the class does Each instance of a Class is an object.

Java OOP In Java everything is an object or class Objects send messages to teach other by method calls Instance methods belong to a particular object Static methods belong to a particular class

Different objects of a class have the same fields and methods. The values in the fields are different for each object.  Each Person has eye color as a field Each instance of a Person would have its own value or color.

Example Class Car { String LicensePlate; // “Ill ” double speed; // in miles per hour double maxSpeed; // in miles per hour public void stopCar(){ speed = 0; }

Constructing an Object Instantiating an object in Java is done with the keyword new followed by the call to the class’s constructor. Car mycar ; declaration mycar = new Car(); initialization Car mycar = new Car();

Member access Accessing the members/fields of an object is done with the dot separator  mycar.speed  mycar.speed = 50;  mycar.stopCar();

Objects from a different class Class CarTest { public static void main(String[] args){ Car c = new Car(); c.speed = 55; c.LicensePlate = “Il ” c.maxSpeed = 94.5; }

Initializing Fields Fields should be initialized when they are declared. class Car { String licensePlate = “”; double speed = 0; double maxSpeed =0; String toString(){ System.out.println(licensePlate + ” is moving “ + speed + “ miles per hour”); }

class CarTest2{ public static void main(String[] args){ Car c = new Car(); c.toString(); } javac Car.java javac CarTest2.java jara CarTest2 is moving at 0.0 miles per hour

Methods Each method in Java has a signature similar to other languages Access modifier Return type Name of method Any parameters/ arguments

Class Car { String LicensePlate =“”; // “Ill ” double speed = 0; // in miles per hour double maxSpeed = 98.6; // in miles per hour public void stopCar(){ this.speed = 0; } public void floorIt(){ this.speed = this.maxSpeed; }

Implied this The “this” reference is not always used Each object has it’s own data and methods and this instance would know what its data is. This reference can also be used to distinguish between local and instance variables.

Passing Arguments Altering the fields of an object is usually done by method calls, not direct access to the field. This protects the data. public void accelerate(double speed){ this.speed = this.speed + speed; if(this.speed >this maxSpeed){ this.speed = this.maxSpeed; } if(this.speed < 0.0){ this.speed = 0.0; }

Setters and Getters Accessing data and mutating it Setter/ Mutator methods merely set the value of a field to the value specified in the argument. public void setSpeed(double speed) {} Accesors/ getters retrieve data Public double getSpeed() {}

Constructors A constructor is required in order to instantiate an object.  If you don’t write one, the compiler will. Constructors have no return type, and the method name is the same as the class type. public Car () {}

Constructor function Besides instantiating an object of the class type Constructors initialize the data fields. If you don’t provide a value, all values are set to defaults.

Default and Alternate Constructors You can build a constructor with any number of arguments as a way of getting data into your objects. public Car(String license, double speed, double max) {} If you write an alternate,,, and you want a zero argument constructor you must write it.

Setting constraints. Setting up constructor and setter methods are the best way to control your data. Public Car(double speed, double max){ this.speed = speed; maxSpeed =(max !> 125 ) ? max : 125 if(this.speed >this maxSpeed){ this.speed = this.maxSpeed; } if(this.speed < 0.0){ this.speed = 0.0; }

Access Protection Four levels  Public  Private  Protected  Default aka package

Public - Open access from anywhere or any other object Private – the fields of an object or any object of the same class (siblings) Protected – subclasses and classes in same package Default – classes in same package

Benefits of Access Protection Allows enforcement of constraints Provides simpler client interface. Clients do not need to know everything, only the public parts Separates interface from implementation, allowing them to vary independently.

OK, what is what General guidelines Classes public Fields private Constructors public Getters and setter public Other methods?? Decide as appropriate case by case

Constructors in Java

Basic Syntax for constructors [Access modifier] (any arguments) { }

Constructor Restrictions Modifiers  Public or package for Top level classes No return type Name MUST match class name

Default constructor The zero argument constructor  If no constructors are defined, java creates this  The only action by the implicit constructor is to call the superclass constructor insuring that the inherited state is initialized.

Overloaded Constructors Differ from default in arguments The first line of every constructor must be either A this call to another constructor in the same class. A super call to a parent constructor.

Why you might want to call super explicitly Normally, you won't need to call the constructor for your parent class because it's automatically generated, but there are two cases where this is necessary. You want to call a parent constructor which has parameters (the automatically generated super constructor call has no parameters). There is no parameterless parent constructor because only constructors with parameters are defined in the parent class.

Static Initializer Blocks It doesn't have a name, arguments, or any access modifiers, and does not return a value. All we need to declare a static initializer is the keyword "static" and the code enclosed in braces. static { Pi = ; }