Road Map Introduction to object oriented programming. Classes

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

Fields, Constructors, Methods
Computer Science and Engineering College of Engineering The Ohio State University Classes and Objects: Members, Visibility The credit for these slides.
Road Map Introduction to object oriented programming. Classes
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Computer Science I Inheritance Professor Evan Korth New York University.
Introduction to Java Programming, 4E Y. Daniel Liang.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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)
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
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.
ECE122 Feb. 22, Any question on Vehicle sample code?
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
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.
Objects and Classes Mostafa Abdallah
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Topic: Classes and Objects
Lecture 3: Introduction to Object and Classes
Objects as a programming concept
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Road Map Inheritance Class hierarchy Overriding methods Constructors
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Classes and Methods
Contents Introduction to Constructor Characteristics of Constructor
Chapter 9 Objects and Classes
Chapter 8 Objects and Classes
CIS16 Application Development and Programming using Visual Basic.net
Object Oriented Programming in java
Class.
Chapter 8 Objects and Classes
Chapter 9 Objects and Classes Part 01
OO Programming Concepts
Chapter 4 Constructors Section 4.4
Creating and Using Classes
Chapter 7 Objects and Classes
SPL – PS3 C++ Classes.
Chapter 5 Classes.
Presentation transcript:

Computer Science I Classes and Objects Professor: Evan Korth New York University

Road Map Introduction to object oriented programming. Classes Encapsulation Members Objects Constructors Reading: Liang 7: chapter 7: 7.1 – 7.4 Liang 8: chapter 8: 8.1 – 8.5 Evan Korth New York University

Object Oriented Programming Emphasis is placed on nouns or objects. Nouns (objects) have properties and behaviors. How do we build these objects? How do we represent their properties? How do we define their behaviors? Evan Korth New York University

Classes The main building blocks of Java programs. Defines objects of the same type. Like a blueprint. Evan Korth New York University

Classes (cont) Every .java file has one or more classes. Only one of the classes can be a public class. That class must have the same name as the .java file. If the class has an method called main(), execution can begin in that class. (Therefore, you can test a class by adding a main method to it.) If there are other classes in the file, they cannot be public classes. Evan Korth New York University

Encapsulation Encapsulation refers to the process of combining elements to create a new entity. You encapsulate the properties (attributes) and behaviors (activities) of an entity into a class. Encapsulation also enables us to hide the implementation of a class to other classes (information hiding / abstraction). Evan Korth New York University

Designing Classes A class declaration includes members of the class. A member can be either a data member or a method member. A data member (AKA field) is used to define state (attributes or properties) of the entity. A method member is used to define the behaviors of the entity. Evan Korth New York University

Data members Data members can be a primitive type or a reference to another object*. Primitive types are integer types, floating point types, characters and booleans. (Note: an int is not the same as an object of type Integer) The scope of a data member is the entire class, no matter where within the class it is declared. * More on object references in a moment Evan Korth New York University

Default values for data members 0 for all numeric type variables (including both floating point types and all integer types) \u0000 for char variables null for reference variables* false for boolean type variables Note: No default values for local variables (variables declared inside a method). * More on object references in a moment Evan Korth New York University

Objects An object is an instance of a class. If we think of a class as a blueprint, an object is one model created from that blueprint. You can create any number of objects from one class. An object is distinctly identified by an object reference (except for anonymous objects). Evan Korth New York University

Declaring object references In order to reference an object, we need an object reference variable. To declare an object reference variable we use the syntax: ClassName objectReferenceName; The above statement creates a variable objectReferenceName which can reference a ClassName object. It does NOT create an object. Evan Korth New York University

Instantiating objects In order to create an object, we use the new keyword along with a constructor* for the class of the object we wish to create. To refer to the object, we “point” an object reference variable to the new object. objectReferenceName = new Constructor(); The declaration and instantiation can be combined as follows: ClassName objectReferenceName = new ClassName(); Note: the name of a constructor is the same as the name of the class * More on constructors soon Evan Korth New York University

Accessing Members of a Class Within a class you can access a member of the class the same way you would any other variable or method. Outside the class, a class member is accessed by using the syntax: Referencing variables: objectReferenceName.varName Calling methods (sending messages): objectReferenceName.methodName(params) Evan Korth New York University

Constructors Constructors are special methods that instantiate objects. A constructor is invoked with the new operator. A constructor should initialize the class variables. If the variables are not initialized, default values are used. A constructor does not have a return type. A constructor’s identifier (name) is the same as the class it constructs. Evan Korth New York University

Constructors continued Constructors can be overloaded but each one must have its own signature. A constructor with fewer arguments can call a constructor with more arguments (we will see how to do this soon). If no constructor is defined, a default constructor is automatically supplied which accepts no parameters. Variables are initialized to their default values. If one constructor is explicitly defined, the automatic default constructor is no longer available. In such case, if you want a no parameter constructor, you must define it yourself. Evan Korth New York University