1 Object-Oriented Programming Concepts. 2 Recap from last lecture Variables and types –int count Assignments –count = 55 Arithmetic expressions –result.

Slides:



Advertisements
Similar presentations
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
What is an object? Your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior;
EEC-681/781 Distributed Computing Systems Java Tutorial Wenbing Zhao Cleveland State University
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Object-oriented Programming Concepts
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
9/4/2015Abstract classes & Interface1 Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces.
Object-Oriented Programming Concepts
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Intro to OOP with Java, C. Thomas Wu
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
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.
Object Oriented Programming
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
ISBN Object-Oriented Programming Chapter Chapter
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
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.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Object Oriented Programming in Java Habib Rostami Lecture 2.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
Inheritance and Polymorphism
Java Inheritance.
Section 11.1 Class Variables and Methods
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Extending Classes.
Implementing Non-Static Features
Advanced Programming Behnam Hatami Fall 2017.
Advanced Java Programming
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

1 Object-Oriented Programming Concepts

2 Recap from last lecture Variables and types –int count Assignments –count = 55 Arithmetic expressions –result = count/5 + max Control flow –if – then – else –while – do –do – while –For SubPrograms –Methods

3 Programming Programming consists of two steps: design (the architects) coding (the construction workers) –Object oriented design –Object oriented programming

4 What Is an Object? Real world examples: bicycle; dog; dinosaur; table; rectangle; color. Objects have two characteristics: state (attributes) and behavior Software objects maintain its state in variables or data, and implement the behavior using methods. An Object

5 What Is an Object? Real-world objects can be represented using software objects: e.g., electronic dinosaur, bicycle Software objects may correspond to abstract concepts: e.g., an event A Bicycle Object

6 What Is an Object? Methods to brake, change the pedal cadence, and change gears. Concept of encapsulation: hiding internal details from other objects: you do not need to know how the gear mechanism works. In Java, both methods and variables can be hidden A Bicycle Object

7 What Are Messages? Software objects interact and communicate with each other using messages (method invocation) A message with parameters

8 What Are Messages? Three components comprise a message: 1.The object to whom the message is addressed (Your Bicycle) 2.The name of the method to perform (changeGears) 3.Any parameters needed by the method (lower gear)

9 What Are Classes? A class is a blueprint or prototype that defines the variables and the methods common to all objects of a certain kind. Instantiation of a class: create an instance (object) according to the blueprint specification.

10 What Are Classes? Consist of public API and private implementation details

11 Object vs Class Each object has its own instance variables: e.g., each bicycle has its own (x,y) position.

12 Object vs Class Usually no memory is allocated to a class until instantiation, whereupon memory is allocated to an object of the type. Except when there are class variables. All objects of the same class share the same class variables: e.g., extinct variable of dinosaur class; tax rate of certain class of goods.

13 What Is Inheritance? A class inherits state and behavior from its superclass. A subclass can define additional variables and methods. A subclass can override methods of superclass (e.g., change gear method might be changed if an additional gear is provided.) Can have more than one layer of hierarchy Software Reuse

14 What is an Interface? Definition: An interface is a device that unrelated objects use to interact with each other. An object can implement multiple interfaces. Object 1 Interface A Interface B Interface C Object

15 Primitive and Reference Data Type int x; x = 5; x: 5 Point p1, p2; p1 = new Point(); p2 = p1; p1:p2: x: 0 y: 0 Primitive Data Type Reference Data Type

16 Brief Introduction to Classes A point in 2-D space: public class SimplePoint { public int x = 0; public int y = 0; } Upon instantiation

17 Brief Introduction to Classes A simple rectangle class: public class SimpleRectangle { public int width = 0; public int height = 0; public SimplePoint origin = new SimplePoint(); } Reference type vs primitive type

18 A Brief Introduction to Classes A Point Class with a constructor: public class Point { public int x = 0; public int y = 0; // a constructor! public Point(int x, int y) { this.x = x; this.y = y; } } new Point(44,78);

19 A Brief Introduction to Classes More sophisticated Rectangle Class: public class Rectangle { public int width = 0; public int height = 0; public Point origin; // four constructors public Rectangle() { origin = new Point(0, 0); } public Rectangle(Point p) { origin = p; }

20 A Brief Introduction to Classes public Rectangle(int w, int h) { this(new Point(0, 0), w, h); } public Rectangle(Point p, int w, int h) { origin = p; width = w; height = h; } // a method for moving the rectangle public void move(int x, int y) { origin.x = x; origin.y = y; }

21 A Brief Introduction to Classes // a method for computing the area of the rectangle public int area() { return width * height; } // clean up! protected void finalize() throws Throwable { origin = null; super.finalize(); }

22 Basic Structures of a Class Class Declaration Variable Instance Variable Class Variable Constructor Method Instance Method Class Method Cleanup Rectangle2.java

23 Creating Classes A blueprint or prototype that you can use to create many objects. Type for objects. classDeclaration { classBody }

24 The Class Declaration Simplest class declaration class NameOfClass {... } e.g., class ImaginaryNumber{... } Start with capital letter by convention

25 The Class Declaration Class declaration can say more about the class: declare what the class's superclass is declare whether the class is public, abstract, or final (if not specified, then default) list the interfaces implemented by the class

26 Declaring a Class's Superclass All class has a superclass. If not specified, superclass is Object class by default To specify an object's superclass explicitly, class NameOfClass extends SuperClassName{... } e.g., class ImaginaryNumber extends Number {... } Part of java.lang package

27 Declare whether the Class is Public, Final, or Abstract Modifier class NameOfClass {... } Default: accessible only by classes within same package Public: accessible by classes everywhere Final: the class cannot be further subclassed. Abstract: some methods are defined but unimplemented; must be further subclassed before instantiation.

28 Listing the Interfaces Implemented by a Class An interface declares a set of methods and constants without specifying the implementation for any of the methods. e.g., class ImaginaryNumber extends Number implements Arithmetic {... } Contains unimplemented methods such as add(), substract()

29 The Class Declaration

30 The Class Body Contains two different sections: variable declarations and methods. classDeclaration { memberVariableDeclarations methodDeclarations } e.g., class TicketOuttaHere { Float price; String destination; Date departureDate; void signMeUp(Float forPrice, String forDest, Date forDate) { price = forPrice; destination = forDest; departureDate = forDate; }

31 Declaring Member Variables a member variable declaration type variableName; e.g., class IntegerClass { int anInteger;... // define methods here... } IntegerClass anIntergerObject; anIntegerObject = new IntegerClass(); Lowercase by convention All variables must have a type; “class”is also a type

32 Statement for Member Variable Declaration [accessSpecifier] [static] [final] [transient][volatile] type variableName accessSpecifier defines which other classes have access to the variable (public, private, or protected) static indicates that the variable is a class member variable, not an instance member variable. final indicates that the variable is a constant: class Avo { final double AVOGADRO = 6.023e23;} transient variables are not part of the object's persistent state volatile means that the variable is modified asynchronously By convention, all capitals

33 Managing Inheritance All classes inherit from the Object class.

34 Creating Subclasses class SubClass extends SuperClass {... } A Java class can have only one direct superclass. Java does not support multiple inheritance.

35 What Member Variables Does a Subclass Inherit? Rule: A subclass inherits all of the member variables within its superclass that are accessible to that subclass. –Member variables declared as public or protected. Do not inherit private member variables. –Member variables declared with no access modifier so long as subclass is in the same package –If subclass declares a member variable with the same name, the member variable of the superclass is hidden.

36 Hiding Member Variables class Super { Number aNumber; } class Sub extends Super { Float aNumber; }

37 What Methods Does a Subclass Inherit? Rule: A subclass inherits all of the methods within its superclass that are accessible to that subclass. –public or protected methods, but not private methods –no access modifier but in the same package –If subclass declares a method with the same name, the method of the superclass is overridden.

38 Overriding Methods A subclass can either completely override the implementation for an inherited method or the subclass can enhance the method by adding functionality to it.

39 Replacing a Superclass's Method Implementation An example: Thread class has an empty implementation of run(). class BackgroundThread extends Thread { void run() {... }

40 Adding to a Superclass's Method Implementation Another Example: want to preserve initialization done by superclass in constructor: class MyWindow extends Window { public MyWindow(Frame parent) { super(parent);... // MyWindow-specific initialization here... } Superclass constructor

41 Methods a Subclass Cannot Override A subclass cannot override methods that are declared final in the superclass.

42 M ethods a Subclass Must Override Subclass must override methods that are declared abstract in the superclass, or the subclass itself must be abstract.

43 Being a Descendent of Object Every class in the Java system is a descendent (direct or indirect) of the Object class. Your class may want to override: –clone –equals –finalize –toString Your class cannot override (they are final): –getClass –notify –notifyAll –wait –hashCode

44 Summary You should know Objects are created from classes An object's class is its type Difference between reference and primitive types. You also should have a general understanding or a feeling for the following: How to create an object from a class What constructors are What the code for a class looks like What member variables are How to initialize objects What methods look like