CS-202 Introduction to Object Oriented Programming
|
|
- Brendan Murphy
- 6 years ago
- Views:
Transcription
1 CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism
2 Introduction to Inheritance
3 Introduction inheritance: an important and powerful object oriented technique that allows one to define new classes from existing classes. powerful feature for reusing software. defines an is-a relationship between two classes. Example: Suppose you want to define a class to model various shapes (Circles, Rectangles, Triangles, etc.). these classes could have many common features. design the classes to avoid redundancy between them.
4 Superclasses and Subclasses
5 Superclasses and Subclasses You can use a class to model objects of the same type. Even if classes are different, they can share common properties and behaviors these can be generalized in a class and then shared by other classes. Assume there are two classes C1 and C2 where C2 "extends" C1. in this case C1 is the superclass, parent class, or base class and C2 is the subclass, child class, extended class, or derived class A subclass inherits all accessible data fields and methods from its superclass, and can even add new data fields and methods.
6 A First Example Let's design a class to model geometric objects like circles and triangles. Geometric objects have many common properties and behaviors. GeometricObject Class: a general class that can be used to model all geometric objects. has properties color and filled and their getters / setters also has datecreated property and its getter also has a tostring() method to return a string representation of the object.
7 A First Example The Circle class: a special type of geometric object shares common properties and methods with other geometric objects define Circle to "extend" the GeometricObject class inherits everything from GeometricObject adds some of its own methods. The Rectangle Class: can also be defined as a subclass of GeometricObject inherits everything from GeometricObject adds some of its own methods.
8 A First Example
9 A First Example The Circle class uses the following syntax: extends is a keyword which tells the compiler that Circle is a subclass of GeometricObject, thus inheriting from the superclass. the constructor for Circle is implemented by invoking setcolor(), and setfilled() which reside in the parent class inherited by Circle. You may be tempted to do something like this: However, color and filled are private in the GeometricObject class and are only accessible in that class (so not accessible to Circle)
10 Notes on Inheritance Despite its naming, a subclass is not a subset of its superclass. Usually a subclass contains more information and methods than its superclass. Private data fields in the superclass are never accessible outside of the class. They cannot be used directly in a subclass. They can be accessed / mutated through public getters / setters if these are define in the superclass. Not all is-a relationships should be modeled using inheritance a square is a rectangle, but you should not extend Square from Rectangle width and height properties are not appropriate for a square better option is to define a Square class to extend GeomeotricObject and define a side property.
11 Notes on Inheritance Inheritance models the is-a relationship don't start extending classes willy-nilly just to reuse methods. it doesn't make sense for a Tree class to extend a Person class even if they share common properties like height and weight. subclasses and superclasses should always have the is-a relationship. Two things to ask for whether or not to use inheritance? do the two classes share common properties (data fields) and behaviors (methods)? does the is-a relationship make sense in the real world?
12 Notes on Inheritance Example 1: Tree extends Person do the two classes share something in common does the is-a relationship make sense? yes, they both have a height no, because a Tree is not a Person Example 2: Student extends Person do the two classes share something in common yes, they can have common properties does the is-a relationship make sense? yes, because a Student is-a Person
13 Notes on Inheritance Unlike other languages, Java does not allow you to have multiple inheritance inheriting from more than one class Java classes can only inherit directly from one superclass. (single inheritance) (we will come back to multiple inheritance when we talk about interfaces.)
14 Using the super Keyword
15 The super Keyword super is a keyword which refers to the superclass of the class in which super is used super can be used in two ways: call a superclass constructor. call a superclass method.
16 Calling Superclass Constructors Constructors are not inherited by a subclass and can only be invoked using the super keyword. Examples: super(); //call the no-arg super constructor super(parameters); //call the super constructor with the given parameters NOTE: the use of super to call the superclass constructor MUST be the first statement in the subclass constructor. invoking the superclass constructor with its class name in the subclass is a syntax error
17 Constructor Chaining A constructor can invoke an overloaded constructor or its superclass constructor. if neither is invoked explicitly, the compiler automatically puts super() as first statement in the constructor. Constructing an instance of a class invokes the constructors of all the superclasses along the inheritance chain. subclass constructors always invoke the superclass constructors before performing their own tasks even if there are multiple levels of inheritance
18 Constructor Chaining Example See Code: Faculty.java, Employee.java, and Person.java
19 Constructor Chaining Caution Classes should always define a no-arg constructor to avoid programming errors. Consider: Why does this cause compile errors?
20 Calling Superclass Methods super can be used to call other methods from the superclass. Syntax: super.method(parameters); printcircle() from the Circle class could be rewritten as follows:
21 Caution: No super.super.methodname() Assume there are three classes A, B, and C. Assume C inherits from B and B inherits from A A B C Assume A has a method mymethod() which is implemented in B and C Question: if you can use super.mymethod() in B to call A's version of mymethod(), can you also use super.super.mymethod() to call A's version of mymethod() in C? No, this is a syntax error. Only allowed one level of method chaining with super.
22 Overriding Methods
23 Overridding Methods override a method: when a subclass modifies the implementation of a method defined in the super class you have already seen this when you "override" the tostring() method. tostring() is defined in the Object class, and can be given a more specific implementation in any class in Java (since all classes are subclasses of Object).
24 Overridding Methods Rules for Overridding: instance methods can only be overridden if it is accessible (public). if a method defined in the subclass is private in the superclass, the two methods are unrelated. static methods can be inherited, but a static method cannot be overridden if a static method defined in the superclass is redefined in a subclass, the superclass version is "hidden". to access it you need to use SuperClassName.staticMethodName
25 Overriding Methods vs. Overloading Methods
26 Overriding vs. Overloading overloading: define multiple methods with the same name, but different method signatures. overloaded methods can be either in the same class or different classes related by inheritance overloaded methods have the same name but different parameter lists overridding: providing a completely new implementation for a method in a subclass. overridden methods are in different classes related by inheritance overridden methods have the exact same signature and return type.
27 Overriding vs Overloading
28 Override Annotation To avoid any confusion / mistakes, you can use a special Java syntax called the override annotation to indicate that a method is an override of another method. annotation and is placed before the method in the subclass. the annotation denotes that the annotated method is required to override a method in the superclass if the method with this annotation does not override it's superclass method, the compiler will report an error. Example: if tostring() is mistyped tostring() a compile error is reported if the annotation is present, no compile error reported if the annotation is omitted.
29 Override Annotation Example
30 Polymorphism
31 Polymorphism According to Liang, the three pillars of object-oriented programming are encapsulation, inheritance, and polymorphism. All classes define a type. lets define two terms: subtype: a type defined by a subclass supertype: a type defined by a superclass Example: We can say that Circle is a subtype of GeometricObject and GeometricObject is a supertype of Circle
32 Polymorphism Inheritance enables a subclass to inherit features from its superclass with additional new features A subclass is a specialization of its superclass every instance of a subclass is also an instance of its superclass, but the reverse is NOT true. Example: Every Circle is a GeometricObject, but not every GeometricObject is a Circle. Therefore: you can always pass an instance of a subclass, to a parameter of its superclass type polymorphism: a variable of a supertype can refer to a subtype object.
33 Dynamic Binding
34 Dynamic Binding dynamic binding: a method can be implemented in several classes along the inheritance chain, and the JVM decides which method to invoke at runtime. As we have already seen, a method can be defined in a superclass and overridden in its subclass. multiple versions of the same method exist along the inheritance chain.
35 Dynamic Binding Example: tostring() is defined in the Object class, and overridden in GeometricObject Consider: Which version of tostring() gets called here? The Object class's tostring()? The GeometricObject class's tostring()?
36 Dynamic Binding a variable must declare a type lets introduce two new terms: declared type: the type that declares a variable actual type: the actual class for the object referenced by the variable In the previous example: the declared type of o is Object because o is declared as Object o = the actual type of o is GeometricObject because the variable o references an object created using new GeometricObject()
37 Dynamic Binding So, to answer the original question, Which version of tostring() is called? Answer: the version invoked is always determined by the actual type of the reference variable Therefore: the GeometricObject version is invoked.
38 Dynamic Binding When does dynamic binding occur?: at runtime
39 Casting Objects and the instanceof Operator
40 Casting Objects and the instanceof Operator Remember casting? char ch = 'k'; int n = (int)ch; Well we can typecast an object reference into another type of object reference
41 Casting Objects and the instanceof Operator implicit object casting assigning a reference of a subtype, to a reference of the subtype's supertype. this is always allowed because a subtype is always an instance of its supertype...always. Example: GeometricObject go = new Circle();
42 Casting Objects and the instanceof Operator explicit object casting Example: is the following valid? GeometricObject go = new Circle(); Circle c = go; //no this causes a compile error Why doesn't this work? a Circle is always an instace of GeometricObject, but a GeometricObject is not necessarily an instance of Circle. Can we make this work?.. Yes GeometricObject go = new Circle(); Circle c = (Circle)go;
43 Casting Objects and the instanceof Operator For the casting to be successful, you MUST always make sure that the object to be cast is an instance of the subclass. If the superclass object is not an instance of the subclass a runtime ClassCastException occurs. Example: If GeometricObject was not an instance of Circle, then it cannot be case into a Circle.
44 Casting Objects and the instanceof Operator How can you verify that a class is an instance of some other class? the instanceof operator (NOTE: all lowercase) NOTE: all lowercase returns true if Class A is an instanceof Class B Example:
45 Casting Objects and the instanceof Operator Why is casting objects even necessary? Consider this: Circle is a subclass of GeometricObject Circle defines a getdiameter() method GeometricObject does not define a getdiameter() method. Would the following code be valid?: GeometricObject go = new Circle(); System.out.println(go.getDiameter());
46 Casting Objects and the instanceof Operator Other things to note:
47 The Object's equals method
48 The Object's equals method equals(object) is another method defined in the Object class. public boolean equals(object o) This method tests whether two objects are equal: object1.equals(object2)
49 The Object's equals method The default implementation: public boolean equals(object obj) { return (this == obj); } doesn't really do much for us only returns whether or not both objects reference the same area in memory Good practice dictates that you should always override this method and implement your own for every class you create which could be compared to other instances of the same class.
50 The Object's equals method Other Notes:
51 The protected Data and Methods
52 The protected Data and Methods Review: private: can be accessed only from inside of the class public: can be accessed from any other class What if you want to allow subclasses to access data fields or methods defined in the superclass directly, but prevent nonsubclasses from accessing these same data fields and methods Use the protected modifier
53 The protected Data and Methods Rules for Use: private: no modifiers (default): allow members of the class to be accessed directly from any class within the same package but not from other package. protected: hide the members of a class completely so they cannot be accessed directly from outside the class. use private for members which will not be used ANYWHERE outside the class enable the members of the class to be accessed by the subclasses in any package or classes in the same package use protected if they are intended for extenders of the class, but not any other users of the class. public: enable members of the class to be accessed by any class. use public if they are intended for users of the class
54 The protected Data and Methods Other Notes: private and protected can only be used for members of the class public and default (no modifier) can be used on members of the class as well as the class itself a class with no modifier (a non public class) is not accessible by classes from other packages
55 The protected Data and Methods
56 The protected Data and Methods
57 Preventing Extending and Overriding
58 Preventing Extending and Overriding A final class cannot be extended A final method cannot be overridden A final data field is a constant If you want to prevent a class from being extended (subclassed) or a method from being overridden by its subclasses you can use the final keyword. Examples: Math, String, StringBuilder, StringBuffer are all final, you cannot make subclasses from them
59 Preventing Extending and Overriding
INHERITANCE & 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 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 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 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 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 informationChapter 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 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 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 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 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 informationInheritance (continued) Inheritance
Objectives Chapter 11 Inheritance and Polymorphism Learn about inheritance Learn about subclasses and superclasses Explore how to override the methods of a superclass Examine how constructors of superclasses
More informationChapter 5 Object-Oriented Programming
Chapter 5 Object-Oriented Programming Develop code that implements tight encapsulation, loose coupling, and high cohesion Develop code that demonstrates the use of polymorphism Develop code that declares
More informationWhat is Inheritance?
Inheritance 1 Agenda What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain super keyword Overriding methods (most important) Hiding methods Hiding fields Type casting
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 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 informationPractice for Chapter 11
Practice for Chapter 11 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Object-oriented programming allows you to derive new classes from existing
More informationCOMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed
1 1 COMP200 INHERITANCE OOP using Java, from slides by Shayan Javed 2 Inheritance Derive new classes (subclass) from existing ones (superclass). Only the Object class (java.lang) has no superclass Every
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 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 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 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 informationCOP 3330 Final Exam Review
COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time
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 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 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 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 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 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 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 informationCREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed
CREATED BY: Muhammad Bilal Arslan Ahmad Shaad JAVA Chapter No 5 Instructor: Muhammad Naveed Muhammad Bilal Arslan Ahmad Shaad Chapter No 5 Object Oriented Programming Q: Explain subclass and inheritance?
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 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 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 informationInheritance, Polymorphism, and Interfaces
Inheritance, Polymorphism, and Interfaces Chapter 8 Inheritance Basics (ch.8 idea) Inheritance allows programmer to define a general superclass with certain properties (methods, fields/member variables)
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 informationInheritance (IS A Relationship)
Inheritance (IS A Relationship) We've talked about the basic idea of inheritance before, but we haven't yet seen how to implement it. Inheritance encapsulates the IS A Relationship. A String IS A Object.
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 informationC++ Important Questions with Answers
1. Name the operators that cannot be overloaded. sizeof,.,.*,.->, ::,? 2. What is inheritance? Inheritance is property such that a parent (or super) class passes the characteristics of itself to children
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 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 informationIslamic University of Gaza Faculty of Engineering Computer Engineering Department
Student Mark Islamic University of Gaza Faculty of Engineering Computer Engineering Department Question # 1 / 18 Question # / 1 Total ( 0 ) Student Information ID Name Answer keys Sector A B C D E A B
More informationProgramming Exercise 14: Inheritance and Polymorphism
Programming Exercise 14: Inheritance and Polymorphism Purpose: Gain experience in extending a base class and overriding some of its methods. Background readings from textbook: Liang, Sections 11.1-11.5.
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 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 informationMotivations. Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces CS1: Java Programming Colorado State University Original slides by Daniel Liang Modified slides by Chris Wilcox Motivations You have learned how to write simple
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 informationObject Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance
CS257 Computer Science I Kevin Sahr, PhD Lecture 10: Inheritance 1 Object Oriented Features For a programming language to be called object oriented it should support the following features: 1. objects:
More informationClasses and Inheritance Extending Classes, Chapter 5.2
Classes and Inheritance Extending Classes, Chapter 5.2 Dr. Yvon Feaster Inheritance Inheritance defines a relationship among classes. Key words often associated with inheritance are extend and implements.
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 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 informationData Structures (list, dictionary, tuples, sets, strings)
Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in brackets: l = [1, 2, "a"] (access by index, is mutable sequence) Tuples are enclosed in parentheses: t = (1, 2, "a") (access
More informationGraphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub
Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java
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 informationChapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited
Chapter 13 Abstract Classes and Interfaces Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited 2015 1 Motivations You have learned how to write simple programs
More informationChapter 6 Introduction to Defining Classes
Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of
More informationOverview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017
Overview of OOP Dr. Zhang COSC 1436 Summer, 2017 7/18/2017 Review Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in square brackets: l = [1, 2, "a"] (access by index, is mutable
More informationProgramming in C# Inheritance and Polymorphism
Programming in C# Inheritance and Polymorphism C# Classes Classes are used to accomplish: Modularity: Scope for global (static) methods Blueprints for generating objects or instances: Per instance data
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 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 informationCS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces
CS 112 Programming 2 Lecture 10 Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces 2 1 Motivations We have learned how to write simple programs to create and display GUI components.
More informationCOMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code
COMPUTER SCIENCE DEPARTMENT PICNIC Welcome to the 2016-2017 Academic year! Meet your faculty, department staff, and fellow students in a social setting. Food and drink will be provided. When: Saturday,
More informationClass, Variable, Constructor, Object, Method Questions
Class, Variable, Constructor, Object, Method Questions http://www.wideskills.com/java-interview-questions/java-classes-andobjects-interview-questions https://www.careerride.com/java-objects-classes-methods.aspx
More informationAgenda. Objects and classes Encapsulation and information hiding Documentation Packages
Preliminaries II 1 Agenda Objects and classes Encapsulation and information hiding Documentation Packages Inheritance Polymorphism Implementation of inheritance in Java Abstract classes Interfaces Generics
More informationThe software crisis. code reuse: The practice of writing program code once and using it in many contexts.
Inheritance The software crisis software engineering: The practice of conceptualizing, designing, developing, documenting, and testing largescale computer programs. Large-scale projects face many issues:
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 informationLecture 18 CSE11 Fall 2013 Inheritance
Lecture 18 CSE11 Fall 2013 Inheritance What is Inheritance? Inheritance allows a software developer to derive a new class from an existing one write code once, use many times (code reuse) Specialization
More informationInheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L
Inheritance Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 9.4 1 Inheritance Inheritance allows a software developer to derive
More informationCS Programming I: Inheritance
CS 200 - Programming I: Inheritance Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Inheritance
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 informationBig software. code reuse: The practice of writing program code once and using it in many contexts.
Inheritance Big software software engineering: The practice of conceptualizing, designing, developing, documenting, and testing largescale computer programs. Large-scale projects face many issues: getting
More informationInheritance. The Java Platform Class Hierarchy
Inheritance In the preceding lessons, you have seen inheritance mentioned several times. In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those
More informationChapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces rights reserved. 1 Motivations You have learned how to write simple programs to create and display GUI components. Can you write the code to respond to user actions,
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 informationJava: introduction to object-oriented features
Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer
More informationMotivations. Objectives. object cannot be created from abstract class. abstract method in abstract class
Motivations Chapter 13 Abstract Classes and Interfaces You have learned how to write simple programs to create and display GUI components. Can you write the code to respond to user actions, such as clicking
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 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 informationProgramming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017
Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017 Three Properties of Object-Oriented Languages: Encapsulation Inheritance Dynamic method binding (polymorphism)
More information8. Polymorphism and Inheritance
8. Polymorphism and Inheritance Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives Describe polymorphism and inheritance in general Define interfaces
More informationOVERRIDING. 7/11/2015 Budditha Hettige 82
OVERRIDING 7/11/2015 (budditha@yahoo.com) 82 What is Overriding Is a language feature Allows a subclass or child class to provide a specific implementation of a method that is already provided by one of
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 informationInheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.
Inheritance Inheritance is the act of deriving a new class from an existing one. Inheritance allows us to extend the functionality of the object. The new class automatically contains some or all methods
More informationEKT472: Object Oriented Programming. Overloading and Overriding Method
EKT472: Object Oriented Programming Overloading and Overriding Method 2 Overriding Versus Overloading Do not confuse overriding a method in a derived class with overloading a method name When a method
More informationMore On inheritance. What you can do in subclass regarding methods:
More On inheritance What you can do in subclass regarding methods: The inherited methods can be used directly as they are. You can write a new static method in the subclass that has the same signature
More informationMore About Objects. Zheng-Liang Lu Java Programming 255 / 282
More About Objects Inheritance: passing down states and behaviors from the parents to their children. Interfaces: requiring objects for the demanding methods which are exposed to the outside world. Polymorphism
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism Abstract Classes
More informationComputer Science II (20073) Week 1: Review and Inheritance
Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system
More information1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'
Practice questions: 1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4' 2. Will System.out.println((char)4) display 4? a. Yes b. No 3. The expression "Java
More informationAbstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.
a and Interfaces Class Shape Hierarchy Consider the following class hierarchy Shape Circle Square Problem AND Requirements Suppose that in order to exploit polymorphism, we specify that 2-D objects must
More informationOOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.
OOPs Concepts 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. Type Casting Let us discuss them in detail: 1. Data Hiding: Every
More informationExercise: Singleton 1
Exercise: Singleton 1 In some situations, you may create the only instance of the class. 1 class mysingleton { 2 3 // Will be ready as soon as the class is loaded. 4 private static mysingleton Instance
More informationCSC207H: Software Design. Java + OOP. CSC207 Winter 2018
Java + OOP CSC207 Winter 2018 1 Why OOP? Modularity: code can be written and maintained separately, and easily passed around the system Information-hiding: internal representation hidden from the outside
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 informationM301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism
Block 1: Introduction to Java Unit 4: Inheritance, Composition and Polymorphism Aims of the unit: Study and use the Java mechanisms that support reuse, in particular, inheritance and composition; Analyze
More informationWeek 7. Statically-typed OO languages: C++ Closer look at subtyping
C++ & Subtyping Week 7 Statically-typed OO languages: C++ Closer look at subtyping Why talk about C++? C++ is an OO extension of C Efficiency and flexibility from C OO program organization from Simula
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 CSC 123 Fall 2018 Howard Rosenthal
Inheritance CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Defining what inheritance is and how it works Single Inheritance Is-a Relationship Class Hierarchies Syntax of Java Inheritance The super Reference
More informationJava Fundamentals (II)
Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class
More informationOutline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring
Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,
More information