2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,

Slides:



Advertisements
Similar presentations
21-Aug-14 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Advertisements

Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
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.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
Starting Classes and Methods. Objects have behaviors In old style programming, you had: –data, which was completely passive –functions, which could manipulate.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
1 Chapter 7 Inheritance, Polymorphism, and Scope.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
Chapter 10 Classes Continued
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Java Programming Review (Part I) Enterprise Systems Programming.
OOP Languages: Java vs C++
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Object-Oriented Programming Concepts
Programming Languages and Paradigms Object-Oriented Programming.
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 32 Thanks for Lecture Slides: C How to Program by Paul Deital &
CSC 142 Computer Science II Zhen Jiang West Chester University
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
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.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSC 205 Java Programming II Inheritance Inheritance In the real world, objects aren’t usually one-of-a-kind. Both cars and trucks are examples of.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Basic Object-Oriented Concepts – towards implementation CS3340.
Basic Object-Oriented Concepts
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
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.
Topics Instance variables, set and get methods Encapsulation
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
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
OOP: Encapsulation &Abstraction
Data Structures and Algorithms revision
Objects as a programming concept
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Introduction to Object-Oriented Concept
Chapter 3: Using Methods, Classes, and Objects
CSC 205 Java Programming II
Class Structure 16-Nov-18.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Class Structure 28-Nov-18.
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
Core Concepts.
Class Structure 25-Feb-19.
CLASSES, AND OBJECTS A FIRST LOOK
Java Programming Language
Chap 2. Identifiers, Keywords, and Types
Classes and Methods 15-Aug-19.
Presentation transcript:

2-Oct-16 Basic Object-Oriented Concepts

2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions, which could manipulate any data An object contains both data and methods that manipulate that data An object is active, not passive; it does things An object is responsible for its own data But: it can expose that data to other objects

3 Concept: An object has state An object contains both data and methods that manipulate that data The data represent the state of the object Data can also describe the relationships between this object and other objects Example: A CheckingAccount might have A balance (the internal state of the account) An owner (some object representing a person)

4 Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields, or variables The class describes those fields An object may have methods The class describes those methods A class is like a template, or cookie cutter You use the class’s constructor to make objects

5 Concept: Classes are like Abstract Data Types An Abstract Data Type (ADT) bundles together: some data, representing an object or "thing" the operations on that data The operations defined by the ADT are the only operations permitted on its data Example: a CheckingAccount, with operations deposit, withdraw, getBalance, etc. Classes enforce this bundling together If all data values are private, a class can also enforce the rule that its defined operations are the only ones permitted on the data

6 Example of a class class Employee { // Fields private String name; //Can get but not change private double salary; // Cannot get or set // Constructor Employee(String n, double s) { name = n; salary = s; } // Methods void pay () { System.out.println("Pay to the order of " + name + " $" + salary); } public String getName() { return name; } // getter }

7 Approximate Terminology instance = object field = instance variable method = function sending a message to an object = calling a function These are all approximately true

8 Concept: Classes form a hierarchy Classes are arranged in a treelike structure called a hierarchy The class at the root is named Object Every class, except Object, has a superclass A class may have several ancestors, up to Object When you define a class, you specify its superclass If you don’t specify a superclass, Object is assumed Every class may have one or more subclasses

9 Example of (part of) a hierarchy A FileDialog is a Dialog is a Window is a Container Container PanelScrollPane Window DialogFrame FileDialog

10 Concept: Objects inherit from superclasses A class describes fields and methods Objects of that class have those fields and methods But an object also inherits: the fields described in the class's superclasses the methods described in the class's superclasses A class is not a complete description of its objects!

11 Example of inheritance class Person { String name; int age; void birthday () { age = age + 1; } class Employee extends Person { double salary; void pay () {...} } Every Employee has name and age fields and birthday method as well as a salary field and a pay method.

12 Concept: Objects must be created int n; does two things: It declares that n is an integer variable It allocates space to hold a value for n For a primitive, this is all that is needed Employee secretary; also does two things It declares that secretary is type Employee It allocates space to hold a reference to an Employee For an object, this is not all that is needed secretary = new Employee ( ); This allocate space to hold a value for the Employee Until you do this, the Employee is null

13 Notation: How to declare and create objects Employee secretary; // declares secretary secretary = new Employee (); // allocates space Employee secretary = new Employee(); // does both But the secretary is still "blank" ( null ) secretary.name = "Adele"; // dot notation secretary.birthday (); // sends a message

14 Notation: How to reference a field or method Inside a class, no dot notation is necessary class Person {... age = age + 1; } Outside a class, you need to say which object you are talking to if (john.age < 75) john.birthday (); If you don't have an object, you cannot use its fields or methods!

15 Concept: this object Inside a class, no dots are necessary, because you are working on this object If you wish, you can make it explicit: class Person {... this.age = this.age + 1;...} this is like an extra parameter to the method You usually don't need to use this

16 Concept: Constructors make objects Every class has a constructor to make its objects Use the keyword new to call a constructor secretary = new Employee ( ); You can write your own constructors; but if you don’t, Java provides a default constructor with no arguments It sets all the fields of the new object to zero If this is good enough, you don’t need to write your own The syntax for writing constructors is almost like that for writing methods

17 Syntax for constructors Do not use a return type and a name; use only the class name You can supply arguments Employee (String theName, double theSalary) { name = theName; salary = theSalary; }

18 Trick: Give field and parameter the same name A parameter overrides a field with the same name But you can use this. name to refer to the field class Person { String name; int age; Person (String name, int age) { this.name = name; this.age = age; } } Using the same name is a common and useful convention

19 The case of the vanishing constructor If you don't write a constructor for a class, Java provides one (the default constructor) The one Java provides has no arguments If you write any constructor for a class, Java does not provide a default constructor Adding a perfectly good constructor can break a constructor chain You may need to fix the chain

20 Concept: Classes can have fields and methods Usually a class describes fields (variables) and methods for its objects (instances) These are called instance variables and instance methods A class can have its own fields and methods These are called class variables and class methods There is exactly one copy of a class variable, not one per object Use the special keyword static to say that a field or method belongs to the class instead of to objects

21 Example of a class variable class Person { String name; int age; static int population; Person (String name) { this.name = name; this.age = 0; population++; }