Object-Oriented Programming 95-712 MISM/MSIT Carnegie Mellon University Professor Roehrig’s Notes.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Constructors And Instantiation. Constructor Basics Every class must have a constructor Even abstract classes!! No return types Their names must exactly.
Final Review.
Constructors 11/10/10. Today  Use constructors to initialize objects.  Use const to protect data members.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
1 CS2200 Software Development Polymorphism II A. O’Riordan, 2008 K. Brown,
Object-Oriented Programming MISM/MSIT Carnegie Mellon University Lecture 4: Polymorphism.
Intermediate Java II Sakir YUCEL MISM/MSIT Carnegie Mellon University Lecture: Exception Handling Slides adapted from Prof. Steven Roehrig.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
Object-Oriented Programming C Sakir YUCEL MISM/MSIT Carnegie Mellon University Lecture 5: Polymorphism Slides adapted from Prof. Steven Roehrig.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Abstract Classes and Interfaces
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
Object Oriented Programming: Java Edition By: Samuel Robinson.
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.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
1 21 COP 3540 Data Structures with OOP Overview: Chapter 1.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Mixing integer and floating point numbers in an arithmetic operation.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Introduction to Java Primitive Types Operators Basic input and output.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Inheritance and Design CSIS 3701: Advanced Object Oriented Programming.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
OOP Basics Classes & Methods (c) IDMS/SQL News
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
Staples are our staple Building upon our solution.
1 Assignment 2: AspectJ. 2 The simple language Tiny program y=2; x=2+y*3; z=x+y*2; –A program is a sequence of assignments; –Expressions on the right.
OOP Tirgul 12.
Inheritance ITI1121 Nour El Kadri.
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Object-Oriented Programming
Advanced Programming in Java
Anatomy of a Java Program
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
while while (condition) { statements }
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Object-Oriented Programming MISM/MSIT Carnegie Mellon University Professor Roehrig’s Notes

A Digression: Arithmetic public class Node { public Node() {} public double eval() { System.out.println("Error: eval Node"); return 0; } public class Binop extends Node { protected Node lChild, rChild; public Binop(Node l, Node r) { lChild = l; rChild = r; }

Arithmetic (cont.) public class Plus extends Binop { public Plus(Node l, Node r) { super(l, r); } public double eval() { return lChild.eval() + rChild.eval(); } public class Const extends Node { private double value; public Const(double d) { value = d; } public double eval() { return value; } }

Arithmetic (cont.) public class TestArithmetic { // evaluate ( ) public static void main(String[] args) { Node n = new Plus( new Plus( new Const(1.1), new Const(2.2)), new Const(3.3)); System.out.println(""+ n.eval()); }

Node n1 = new Const(1.1); Node n2 = new Const(2.2); Node n3 = new Plus(n1, n2); Node n4 = new Const(3.3); Node n5 = new Plus(n3, n4);

Arithmetic (cont.) Binary operators create a binary tree. Binary operators create a binary tree. Constants are “leaf” nodes. Constants are “leaf” nodes. We could easily add more operators and terminals. We could easily add more operators and terminals

Extensibility The Arithmetic program allows additional subclasses to be constructed and easily integrated. The Arithmetic program allows additional subclasses to be constructed and easily integrated. Polymorphism is the key in letting this happen. Polymorphism is the key in letting this happen. We should never make Node objects. We should never make Node objects. How to prevent this? How to prevent this?

Abstract Classes As always, get the compiler involved in enforcing our decisions. As always, get the compiler involved in enforcing our decisions. Now it’s a compiler error if we try to make a Node object (but Node references are OK). Now it’s a compiler error if we try to make a Node object (but Node references are OK). Subclasses are abstract (and we must so state) until all abstract methods have been defined. Subclasses are abstract (and we must so state) until all abstract methods have been defined. public abstract class Node { public Node() {} public abstract double eval(); }

Order of Construction Put print statements into the constructors in the Arithmetic example. The output is: Put print statements into the constructors in the Arithmetic example. The output is: Node constructor Const constructor 1.1 Node constructor Const constructor 2.2 Node constructor Binop constructor Plus constructor Node constructor Const constructor 3.3 Node constructor Binop constructor Plus constructor Node n = new Plus( new Plus( new Const(1.1), new Const(2.2)), new Const(3.3));