COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed
|
|
- Polly McLaughlin
- 5 years ago
- Views:
Transcription
1 1 1 COMP200 INHERITANCE OOP using Java, from slides by Shayan Javed
2 2 Inheritance Derive new classes (subclass) from existing ones (superclass). Only the Object class (java.lang) has no superclass Every class = subclass of Object.
3 3 Inheritance Code reuse methods and properties. Use classes to model objects of the same type Define general class! Then define specialised classes.
4 4 Inheritance Can only inherit from one class. Can inherit from multiple classes in other languages (C++)
5 5 Example Let s say you create the following classes:! Circle! Rectangle! Triangle! etc. Share certain properties, but also specialised properties
6 6 Example public class GeometricObject { private String Color; private String name; private float area; // constructors... public GeometricObject(String color, String name) { // get/set methods, etc.
7 7 Example public class Circle extends GeometricObject { private double radius; // specific property public Circle(double radius, String color, String name) { this.radius = radius; this.color = color; this.name = name; // get/set methods, etc. public double getarea() { return radius * radius * Math.PI;
8 8 Example An error in the constructor! public Circle(double radius, String color, String name) { this.radius = radius; this.color = color; this.name = name; Why can t we use this.color and this.name?
9 9 Example They are private properties. NOT accessible by subclasses. Should use setcolor() instead. public methods/properties inherited.
10 10 Example public class Rectangle extends GeometricObject { private double width, height; // specific properties public Rectangle(double w, double h, String color, String name) { this.height = h; this.width = w; setcolor(color); setname(name); // get/set methods, etc. public double getarea() { return width * height;
11 11 The super keyword Are constructors inherited? Yes. super used for:! calling a super class constructor! calling a super class method
12 12 The super keyword Constructors: super() calls the default constructor super(arguments) calls the constructors according to the arguments
13 13 The super keyword Constructors: public Circle(double radius, String color, String name) { super(color, name); this.radius = radius;
14 14 The super keyword Constructors: public Circle(double radius, String color, String name) { super(color, name); this.radius = radius; public GeometricObject(String color, String name) { this.color = color; this.name = name;
15 15 The super keyword What if we don t add the super(..) constructor call?
16 16 The super keyword public Circle(double radius, String color, String name) { this.radius = radius;
17 17 The super keyword public Circle(double radius, String color, String name) { this.radius = radius; Is the same as: public Circle(double radius, String color, String name) { super(); this.radius = radius;
18 18 The super keyword Called Constructor Chaining
19 19 The super keyword Calling the super class methods super.method(parameters)... Optional
20 20 Protected fields Using get/set methods to access properties of the superclass is cumbersome
21 21 The protected keyword Often want subclasses to directly access properties/methods Use protected instead of private/public Subclasses can now directly access
22 22 Private public class GeometricObject { private String Color; private String name; private float area; // constructors... // get/set methods, etc.
23 23 The protected keyword public class GeometricObject { protected String Color; protected String name; protected float area; // constructors... // get/set methods, etc.
24 24 The protected keyword public class Circle extends GeometricObject { private double radius; // specific property public Circle(double radius, String color, String name) { this.radius = radius; this.color = color; this.name = name; Now works!
25 25 Polymorphism Important aspect of OOP
26 26 Polymorphism Important aspect of OOP capability of an action or method to do different things based on the object that it is acting upon.
27 27 Polymorphism Important aspect of OOP capability of an action or method to do different things based on the object that it is acting upon. characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form.
28 28 Polymorphism 1. Overriding Methods 2. Overloaded methods 3. Dynamic (late) method binding
29 29 Overriding Methods Sometimes need to overwrite methods written in the superclass Re-define the method in the subclass.
30 30 Overriding Methods The Object class and tostring() tostring() = String output when you print the object
31 31 Overriding Methods The Object class and tostring() tostring() = String output when you print the object Object class has default tostring() method
32 32 The tostring() method public String tostring() { String str;... return str;
33 33 The tostring() method Circle circle1 = new Circle( Circle1, Red, 3.5); System.out.println(circle1); Output:?
34 34 The tostring() method Circle circle1 = new Circle( Circle1, Red, 3.5); System.out.println(circle1); Output: Circle@19821f
35 35 The tostring() method So override the method public String tostring() { String str = ; str += Circle: + getname() +, ; str += Color: + getcolor() +, ; str += Radius: + getradius(); return str;
36 36 The tostring() method Circle circle1 = new Circle( Circle1, Red, 3.5); System.out.println(circle1); Output: Circle: Circle1,Color: Red,Radius: 3.5
37 37 The equals() method The Object class and equals() equals (Object obj) = returns whether the object passed in and this one are the same
38 38 The equals() method Default implementation: public boolean equals(object obj) { return (this == obj); Compares references
39 39 The equals() method Circle circle1 = new Circle(3.5); Circle circle2 = new Circle(3.5);
40 40 The equals() method Circle circle1 = new Circle(3.5); Circle circle2 = new Circle(3.5); circle1.equals(circle2); //?
41 41 The equals() method Circle circle1 = new Circle(3.5); Circle circle2 = new Circle(3.5); circle1.equals(circle2); // returns false!
42 42 The equals() method Circle circle1 = new Circle(3.5); Circle circle2 = new Circle(3.5); circle1.equals(circle2); // returns false! Circle circle3 = circle1; circle1.equals(circle3); //?
43 43 The equals() method Circle circle1 = new Circle(3.5); Circle circle2 = new Circle(3.5); circle1.equals(circle2); // returns false! Circle circle3 = circle1; circle1.equals(circle3); // returns true!
44 44 The equals() method Override it public boolean equals(object obj) { if (obj instanceof Circle) return this.radius == ((Circle)obj).getRadius(); else return false;
45 45 The instanceof operator Test if object is of a specified type if (object instanceof Class)
46 46 The instanceof operator Test if object is of a specified type if (object instanceof Class) Example: Circle circle1 = new Circle(3.4); if (circle1 instanceof Circle) {
47 47 The instanceof operator Testing with the SuperClass: if (subclassobj instanceof SuperClass) {?
48 48 The instanceof operator Testing with the SuperClass: if (Circle instanceof GeometricObject) {?
49 49 The instanceof operator Testing with the SuperClass: if (subclassobj instanceof SuperClass) { returns true!
50 50 The instanceof operator Example: Circle circle1 = new Circle(3.4); if (circle1 instanceof GeometricObject) { returns true!
51 51 The instanceof operator What if we write: if (SuperClassObj instanceof subclass) {
52 52 The instanceof operator if (SuperClassObj instanceof subclass) { returns true as long as the object is an instance of the subclass
53 53 The instanceof operator Example: Object is a superclass of Circle public boolean equals(object obj) { if (obj instanceof Circle) return this.radius == ((Circle)obj).getRadius(); else return false;
54 54 The instanceof operator Be careful with null objects String s = null; s instanceof String = false
55 55 Object casting So we can determine if (SuperClassObj instanceof subclass) Convert it to subclass by casting
56 56 Object casting So we can determine if (SuperClassObj instanceof subclass) Convert it to subclass by casting Can now access members/properties
57 57 Object casting Once again... public boolean equals(object obj) { if (obj instanceof Circle) { return this.radius == ((Circle)obj).getRadius(); else return false;
58 58 Object casting Once again... public boolean equals(object obj) { if (obj instanceof Circle) { Circle circle = (Circle)obj; // casting return this.radius == circle.getradius(); else return false;
59 59 Overloading Methods Looked at it before. Can have multiple methods with the same name. But different parameters, return type
60 60 Dynamic binding Object type is determined at run-time ability of a program to resolve references to subclass methods at runtime.
61 61 Dynamic binding public class GeometricObject { protected String Color; protected String name; protected float area; public float getarea() { return -1;
62 62 Dynamic binding // Circle public class Circle { public float getarea() { return Math.PI * radius * radius; // Rectangle public class Rectangle { public float getarea() { return width * height;
63 63 Dynamic binding GeometricObject[] objs = new GeometricObject[2];
64 64 Dynamic binding GeometricObject[] objs = new GeometricObject[2]; objs[0] = new Circle(3); objs[1] = new Rectangle(1, 2);
65 65 Dynamic binding GeometricObject[] objs = new GeometricObject[2]; objs[0] = new Circle(3); objs[1] = new Rectangle(1, 2); for (GeometricObject i : objs) { System.out.println(i.getArea());
66 66 Dynamic binding GeometricObject[] objs = new GeometricObject[2]; objs[0] = new Circle(3); objs[1] = new Rectangle(1, 2); for (GeometricObject i : objs) { System.out.println(i.getArea()); What s the output?
67 67 Dynamic binding Output: The subclass getarea() methods are called. At run-time, Java dynamically determines the class.
68 68 final Classes Sometimes classes shouldn t be allowed to be extended. The String class for example. Use the keyword final final class String {...
69 69 final Methods Methods which can t be overridden in the subclasses.
70 70 Summary... Inheritance! super! protected Polymorphism! Overloading! Overwriting! Dynamic binding instanceof Object casting final classes and methods
71 71 Next lecture... Abstract Classes Interfaces
Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber
Chapter 10 Inheritance and Polymorphism Dr. Hikmat Jaber 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the
More informationCOMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed
1 1 COMP200 ABSTRACT CLASSES OOP using Java, from slides by Shayan Javed Abstract Classes 2 3 From the previous lecture: public class GeometricObject { protected String Color; protected String name; protected
More informationIntroduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017
Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 11: Inheritance and Polymorphism Part 1 Instructor: AbuKhleif, Mohammad Noor Sep 2017 Instructor AbuKhleif, Mohammad
More informationLecture Notes Chapter #9_b Inheritance & Polymorphism
Lecture Notes Chapter #9_b Inheritance & Polymorphism Inheritance results from deriving new classes from existing classes Root Class all java classes are derived from the java.lang.object class GeometricObject1
More informationIST311. Advanced Issues in OOP: Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism IST311/602 Cleveland State University Prof. Victor Matos Adapted from: Introduction to Java Programming: Comprehensive Version, Eighth Edition
More informationInheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University
Inheritance and Polymorphism CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Model classes with similar properties and methods: Circles, rectangles
More informationCS-202 Introduction to Object Oriented Programming
CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction
More informationAbstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof
Abstract Class Lecture 21 Based on Slides of Dr. Norazah Yusof 1 Abstract Class Abstract class is a class with one or more abstract methods. The abstract method Method signature without implementation
More informationChapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {
Chapter 21- Using Generics Case Study: Geometric Bunch In this example a class called GeometricBunch is made to wrap around a list of GeometricObjects. Circle and Rectangle are subclasses of GeometricObject.
More informationIn this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.
Jordan University Faculty of Engineering and Technology Department of Computer Engineering Object-Oriented Problem Solving: CPE 342 Lab-8 Eng. Asma Abdel Karim In this lab, you will be given the implementation
More informationINHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U
C A N A D I A N I N T E R N A T I O N A L S C H O O L O F H O N G K O N G INHERITANCE & POLYMORPHISM P2 LESSON 12 P2 LESSON 12.1 INTRODUCTION inheritance: OOP allows a programmer to define new classes
More informationChapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,
Chapter 11 Inheritance and Polymorphism 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design
More informationInheritance and Polymorphism
Inheritance and Polymorphism Dr. M. G. Abbas Malik Assistant Professor Faculty of Computing and IT (North Jeddah Branch) King Abdulaziz University, Jeddah, KSA mgmalik@kau.edu.sa www.sanlp.org/malik/cpit305/ap.html
More information24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini
24. Inheritance Java Fall 2009 Instructor: Dr. Masoud Yaghini Outline Superclasses and Subclasses Using the super Keyword Overriding Methods The Object Class References Superclasses and Subclasses Inheritance
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract
More information22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini
22. Inheritance Java Summer 2008 Instructor: Dr. Masoud Yaghini Outline Superclasses and Subclasses Using the super Keyword Overriding Methods The Object Class References Inheritance Object-oriented programming
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract
More informationMethods Common to all Classes
Methods Common to all Classes 9-2-2013 OOP concepts Overloading vs. Overriding Use of this. and this(); use of super. and super() Methods common to all classes: tostring(), equals(), hashcode() HW#1 posted;
More informationMaking New instances of Classes
Making New instances of Classes NOTE: revised from previous version of Lecture04 New Operator Classes are user defined datatypes in OOP languages How do we make instances of these new datatypes? Using
More informationCOMP 250. inheritance (cont.) interfaces abstract classes
COMP 250 Lecture 31 inheritance (cont.) interfaces abstract classes Nov. 20, 2017 1 https//goo.gl/forms/ymqdaeilt7vxpnzs2 2 class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object
More informationCS1150 Principles of Computer Science Objects and Classes
CS1150 Principles of Computer Science Objects and Classes Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Object-Oriented Thinking Chapters 1-8
More informationObject Oriented Programming. Java-Lecture 11 Polymorphism
Object Oriented Programming Java-Lecture 11 Polymorphism Abstract Classes and Methods There will be a situation where you want to develop a design of a class which is common to many classes. Abstract class
More informationCOMP200 - Object Oriented Programming: Test One Duration - 60 minutes
COMP200 - Object Oriented Programming: Test One Duration - 60 minutes Study the following class and answer the questions that follow: package shapes3d; public class Circular3DShape { private double radius;
More informationToday. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.
Today Book-keeping Inheritance Subscribe to sipb-iap-java-students Interfaces Slides and code at http://sipb.mit.edu/iap/java/ The Object class Problem set 1 released 1 2 So far... Inheritance Basic objects,
More informationChapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9
Chapter 6 Arrays Chapter 7 Objects and Classes Chapter 8 Strings Chapter 9 Inheritance and Polymorphism GUI can be covered after 10.2, Abstract Classes Chapter 12 GUI Basics 10.2, Abstract Classes Chapter
More informationChapter 14 Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces 1 What is abstract class? Abstract class is just like other class, but it marks with abstract keyword. In abstract class, methods that we want to be overridden
More informationJava Object Oriented Design. CSC207 Fall 2014
Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes
More informationCISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College
CISC 3115 TY3 C09a: Inheritance Hui Chen Department of Computer & Information Science CUNY Brooklyn College 9/20/2018 CUNY Brooklyn College 1 Outline Inheritance Superclass/supertype, subclass/subtype
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes
More informationObject Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods
Object Oriented System Development Paradigm Sunnie Chung CIS433 System Analysis Methods OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents
More informationChapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk 1 Motivations Suppose you will define classes
More informationindex.pdf January 21,
index.pdf January 21, 2013 1 ITI 1121. Introduction to Computing II Circle Let s complete the implementation of the class Circle. Marcel Turcotte School of Electrical Engineering and Computer Science Version
More informationCH. 2 OBJECT-ORIENTED PROGRAMMING
CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED
More informationCISC370: Inheritance
CISC370: Inheritance Sara Sprenkle 1 Questions? Review Assignment 0 due Submissions CPM Accounts Sara Sprenkle - CISC370 2 1 Quiz! Sara Sprenkle - CISC370 3 Inheritance Build new classes based on existing
More informationProgramming 2. Inheritance & Polymorphism
Programming 2 Inheritance & Polymorphism Motivation Lame Shape Application public class LameShapeApplication { Rectangle[] therects=new Rectangle[100]; Circle[] thecircles=new Circle[100]; Triangle[] thetriangles=new
More informationJava Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding
Java Class Design Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding eugeny.berkunsky@gmail.com http://www.berkut.mk.ua Objectives Implement encapsulation Implement inheritance
More informationJava Primer. CITS2200 Data Structures and Algorithms. Topic 2
CITS2200 Data Structures and Algorithms Topic 2 Java Primer Review of Java basics Primitive vs Reference Types Classes and Objects Class Hierarchies Interfaces Exceptions Reading: Lambert and Osborne,
More informationChapter 6: Inheritance
Chapter 6: Inheritance EECS 1030 moodle.yorku.ca State of an object final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80; GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT);
More informationIT101. Inheritance, Encapsulation, Polymorphism and Constructors
IT101 Inheritance, Encapsulation, Polymorphism and Constructors OOP Advantages and Concepts What are OOP s claims to fame? Better suited for team development Facilitates utilizing and creating reusable
More informationCMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.
CMSC131 Inheritance Object When we talked about Object, I mentioned that all Java classes are "built" on top of that. This came up when talking about the Java standard equals operator: boolean equals(object
More informationChapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism 1 Motivations OOP is built on three principles: Encapsulation (classes/objects, discussed in chapters 9 and 10), Inheritance, and Polymorphism. Inheritance: Suppose
More informationCS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism rights reserved. 2 Motivation Suppose you want to define classes to model circles, rectangles, and
More informationJava Magistère BFA
Java 101 - Magistère BFA Lesson 3: Object Oriented Programming in Java Stéphane Airiau Université Paris-Dauphine Lesson 3: Object Oriented Programming in Java (Stéphane Airiau) Java 1 Goal : Thou Shalt
More informationJAVA MOCK TEST JAVA MOCK TEST II
http://www.tutorialspoint.com JAVA MOCK TEST Copyright tutorialspoint.com This section presents you various set of Mock Tests related to Java Framework. You can download these sample mock tests at your
More informationAdvanced Placement Computer Science. Inheritance and Polymorphism
Advanced Placement Computer Science Inheritance and Polymorphism What s past is prologue. Don t write it twice write it once and reuse it. Mike Scott The University of Texas at Austin Inheritance, Polymorphism,
More informationInheritance & Polymorphism Recap. Inheritance & Polymorphism 1
Inheritance & Polymorphism Recap Inheritance & Polymorphism 1 Introduction! Besides composition, another form of reuse is inheritance.! With inheritance, an object can inherit behavior from another object,
More informationInheritance. Lecture 11 COP 3252 Summer May 25, 2017
Inheritance Lecture 11 COP 3252 Summer 2017 May 25, 2017 Subclasses and Superclasses Inheritance is a technique that allows one class to be derived from another. A derived class inherits all of the data
More informationProgramming in Java, 2e Sachin Malhotra Saurabh Choudhary
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary Chapter 5 Inheritance Objectives Know the difference between Inheritance and aggregation Understand how inheritance is done in Java Learn polymorphism
More informationSuper-Classes and sub-classes
Super-Classes and sub-classes Subclasses. Overriding Methods Subclass Constructors Inheritance Hierarchies Polymorphism Casting 1 Subclasses: Often you want to write a class that is a special case of an
More information25. Generic Programming
25. Generic Programming Java Fall 2009 Instructor: Dr. Masoud Yaghini Generic Programming Outline Polymorphism and Generic Programming Casting Objects and the instanceof Operator The protected Data and
More informationJava Comparable interface
Java Comparable interface Recall that to define a binary search tree, the elements that we are considering must be comparable to each other, meaning that there must be a well-defined ordering. For strings
More informationContents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance
Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass
More informationCOMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017
COMP 250 Lecture 32 interfaces (Comparable, Iterable & Iterator) Nov. 22/23, 2017 1 Java Comparable interface Suppose you want to define an ordering on objects of some class. Sorted lists, binary search
More informationInheritance and Polymorphism
Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg
More informationDay 4. COMP1006/1406 Summer M. Jason Hinek Carleton University
Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information
More informationLecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting
Lecture 36: Cloning Last time: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting Today: 1. Project #7 assigned 2. equals reconsidered 3. Copying and cloning 4. Composition 11/27/2006
More informationRules and syntax for inheritance. The boring stuff
Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for
More informationOutline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.
Outline Inheritance Class Extension Overriding Methods Inheritance and Constructors Polymorphism Abstract Classes Interfaces 1 OOP Principles Encapsulation Methods and data are combined in classes Not
More informationConstructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Constructor Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design these classes so to avoid redundancy? The
More informationPolymorphism and Interfaces. CGS 3416 Spring 2018
Polymorphism and Interfaces CGS 3416 Spring 2018 Polymorphism and Dynamic Binding If a piece of code is designed to work with an object of type X, it will also work with an object of a class type that
More informationWe are on the GUI fast track path
We are on the GUI fast track path Chapter 13: Exception Handling Skip for now Chapter 14: Abstract Classes and Interfaces Sections 1 9: ActionListener interface Chapter 15: Graphics Skip for now Chapter
More informationCSE 143 Lecture 20. Circle
CSE 143 Lecture 20 Abstract classes Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; public double area() { return Math.PI * radius * radius; public
More informationINHERITANCE. Spring 2019
INHERITANCE Spring 2019 INHERITANCE BASICS Inheritance is a technique that allows one class to be derived from another A derived class inherits all of the data and methods from the original class Suppose
More informationInheritance and Encapsulation. Amit Gupta
Inheritance and Encapsulation Amit Gupta Project 1 How did it go? What did you like about it? What did you not like? What can we do to help? Suggestions Ask questions if you don t understand a concept
More informationHAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.
HAS-A Relationship Association is a weak relationship where all objects have their own lifetime and there is no ownership. For example, teacher student; doctor patient. If A uses B, then it is an aggregation,
More informationCOS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism
COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism Ibrahim Albluwi Composition A GuitarString has a RingBuffer. A MarkovModel has a Symbol Table. A Symbol Table has a Binary
More informationOBJECT ORİENTATİON ENCAPSULATİON
OBJECT ORİENTATİON Software development can be seen as a modeling activity. The first step in the software development is the modeling of the problem we are trying to solve and building the conceptual
More informationCS 251 Intermediate Programming Inheritance
CS 251 Intermediate Programming Inheritance Brooke Chenoweth University of New Mexico Spring 2018 Inheritance We don t inherit the earth from our parents, We only borrow it from our children. What is inheritance?
More informationReusing Classes. Hendrik Speleers
Hendrik Speleers Overview Composition Inheritance Polymorphism Method overloading vs. overriding Visibility of variables and methods Specification of a contract Abstract classes, interfaces Software development
More informationMore About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario
More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario The Story So Far... Classes as collections of fields and methods. Methods can access fields, and
More informationCS171:Introduction to Computer Science II. Li Xiong
CS171:Introduction to Computer Science II Simple Sorting (cont.) + Interface Li Xiong Today Simple sorting algorithms (cont.) Bubble sort Selection sort Insertion sort Interface Sorting problem Two useful
More informationObject Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming
Overview of OOP Object Oriented Programming is a programming method that combines: a) Data b) Instructions for processing that data into a self-sufficient object that can be used within a program or in
More informationLecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University
Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types
More informationObject-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018
Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 4: OO Principles - Polymorphism http://courses.cs.cornell.edu/cs2110/2018su Lecture 3 Recap 2 Good design principles.
More informationPolymorphism. return a.doublevalue() + b.doublevalue();
Outline Class hierarchy and inheritance Method overriding or overloading, polymorphism Abstract classes Casting and instanceof/getclass Class Object Exception class hierarchy Some Reminders Interfaces
More informationHomework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine
Homework 6 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 21, 2013 Question 1 What is the output of the following C++ program? #include #include using namespace
More informationC12a: The Object Superclass and Selected Methods
CISC 3115 TY3 C12a: The Object Superclass and Selected Methods Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/4/2018 CUNY Brooklyn College 1 Outline The Object class and
More informationAdvanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II
Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg Ad hoc-polymorphism Outline Method overloading Sub-type Polymorphism Method overriding Dynamic
More informationInheritance Sort in ascending order. Reusability 5 Sort Take The 4 Order 12,10,5,4. Class. Use this class to define a new class
CS 112 / Section 02 Ece Akhan, Serkan Fidancı, Birol Cabukusta Notes of March 18, 2008 and March 20, 2008: Inheritance Sort in ascending order Reusability 5 Sort 12 5 4 12 İn Take 10 12 Sort 5 10 Desc.
More informationLecture Notes Chapter #9_c Abstract Classes & Interfaces
Lecture Notes Chapter #9_c Abstract Classes & Interfaces Abstract Classes parent class child class more abstract more concrete, i.e., less abstract abstract class o class with an abstract modifier o class
More informationInheritance. Notes Chapter 6 and AJ Chapters 7 and 8
Inheritance Notes Chapter 6 and AJ Chapters 7 and 8 1 Inheritance you know a lot about an object by knowing its class for example what is a Komondor? http://en.wikipedia.org/wiki/file:komondor_delvin.jpg
More informationHAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.
HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all
More informationChapter 9 Inheritance
Chapter 9 Inheritance I. Scott MacKenzie 1 Outline 2 1 What is Inheritance? Like parent/child relationships in life In Java, all classes except Object are child classes A child class inherits the attributes
More informationIntroduction to Inheritance
Introduction to Inheritance James Brucker These slides cover only the basics of inheritance. What is Inheritance? One class incorporates all the attributes and behavior from another class -- it inherits
More informationInheritance -- Introduction
Inheritance -- Introduction Another fundamental object-oriented technique is called inheritance, which, when used correctly, supports reuse and enhances software designs Chapter 8 focuses on: the concept
More informationPROGRAMMING III OOP. JAVA LANGUAGE COURSE
COURSE 3 PROGRAMMING III OOP. JAVA LANGUAGE PREVIOUS COURSE CONTENT Classes Objects Object class Acess control specifier fields methods classes COUSE CONTENT Inheritance Abstract classes Interfaces instanceof
More informationCS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes
CS/ENGRD 2110 SPRING 2019 Lecture 7: Interfaces and Abstract Classes http://courses.cs.cornell.edu/cs2110 1 Announcements 2 A2 is due Thursday night (14 February) Go back to Lecture 6 & discuss method
More informationAbstract Classes Interfaces
Abstract Classes Interfaces Reading: Chapter 14 (skip 14.1,14.6, 14.12, and 14.13; read 14.7 lightly) Objectives 2 To design and use abstract classes ( 14.2-14.3). To specify common behavior for objects
More informationProgramming Languages and Techniques (CIS120)
Programming Languages and Techniques (CIS120) Lecture 25 November 1, 2017 Inheritance and Dynamic Dispatch (Chapter 24) Announcements HW7: Chat Client Available Soon Due: Tuesday, November 14 th at 11:59pm
More informationCLASS DESIGN. Objectives MODULE 4
MODULE 4 CLASS DESIGN Objectives > After completing this lesson, you should be able to do the following: Use access levels: private, protected, default, and public. Override methods Overload constructors
More informationArgument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.
Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar
More informationCSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:
PROGRAMMING ASSIGNMENT 3: Read Savitch: Chapter 7 Programming: You will have 5 files all should be located in a dir. named PA3: ShapeP3.java PointP3.java CircleP3.java RectangleP3.java TriangleP3.java
More informationPROGRAMMING LANGUAGE 2
31/10/2013 Ebtsam Abd elhakam 1 PROGRAMMING LANGUAGE 2 Java lecture (7) Inheritance 31/10/2013 Ebtsam Abd elhakam 2 Inheritance Inheritance is one of the cornerstones of object-oriented programming. It
More informationExample: Count of Points
Example: Count of Points 1 class Point { 2... 3 private static int numofpoints = 0; 4 5 Point() { 6 numofpoints++; 7 } 8 9 Point(int x, int y) { 10 this(); // calling the constructor with no input argument;
More informationInheritance and object compatibility
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not the other way around Examples: reference/pointer can
More informationPolymorphism and Inheritance
Walter Savitch Frank M. Carrano Polymorphism and Inheritance Chapter 8 Objectives Describe polymorphism and inheritance in general Define interfaces to specify methods Describe dynamic binding Define and
More informationAbstract Classes and Interfaces
Abstract Classes and Interfaces Reading: Reges and Stepp: 9.5 9.6 CSC216: Programming Concepts Sarah Heckman 1 Abstract Classes A Java class that cannot be instantiated, but instead serves as a superclass
More informationCMSC 132: Object-Oriented Programming II
CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation
More informationLearn more about our research, discover data science, and find other great resources at:
Learn more about our research, discover data science, and find other great resources at: http://www.dataminingapps.com Chapter 7 Delving Further into Object- Oriented Concepts Overview Annotations Overloading
More information