Outline. 15. Inheritance. Programming in Java. Computer Science Dept Va Tech August D Barnette, B Keller & P Schoenhoff
|
|
- Amberly Dean
- 6 years ago
- Views:
Transcription
1 Outline 1 Inheritance: extends ; Superclasses and Subclasses Valid and invalid object states, & Instance variables: protected Inheritance and constructors, super The is-a vs. has-a relationship (Inheritance vs. Composition) Class hierarchies & the Object class Overriding vs. Overloading Polymorphism Factoring out Common Behavior Abstract methods and classes Interfaces: implements
2 Inheritance In real life, hierarchies of classes exist. 2 mycar is just as easily a member of the Ford class as the Automobile class Class Automobile Customer Class Ford CorporateCustomer object mycar radioshack
3 Inheritance: extends This relationship is supported in Java by inheritance. made possible by the extends keyword. 3 class Ford extends Automobile{ Original class (Automobile) is the Base class. Newly defined class (Ford) is the Derived class. Base class must exist to make a Derived class.
4 Inheritance 4 Derived classes inherit all data from the Base class. Derived classes inherit all the method interfaces from the Base class, but may specify a new implementation. Derived classes may also add new methods. The Base class is referred to as super Base class = superclass; Derived class = subclass
5 Inheritance: BetterBR Example.1 5 BetterBR Want to create a BufferedReader from a file name without having to worry about code for the InputStreamReader, FileInputStream, and File classes. Want to be able to read an int as easily as we read a String. The user-defined BetterBR class extends the Java BufferedReader class BetterBR objects are also BufferedReader objects BetterBR objects can be used the same as BufferedReader objects
6 Inheritance : BetterBR Example.2 6 class BetterBR extends BufferedReader{ public BetterBR(String filename) throws IOException { super(new InputStreamReader( super invokes the base class constructor. new FileInputStream( new File(filename)))); public BetterBR() throws IOException { super(new InputStreamReader(System.in)); If a derived class has additional data members then their initialization should take place after the invocation of the super constructor.
7 Inheritance : BetterBR Example public int readint() throws Exception { string line; line = readline(); Why can we do this? return Integer.parseInt(line); readint() is a BetterBR method, but not a BufferedReader method.
8 Inheritance : BetterBR Example.4 8 main (String[] arg) throws Exception{ int i; String s; BetterBR bbr = new BetterBR( junk.txt ); i = bbr.readint(); while ( i >= 0) { System.out.println( i = + i); i = bbr.readint(); s = bbr.readline(); while ( s!= null ) { System.out.println( s = + s); s = bbr.readline(); A line Line #2 Last line
9 Object States 9 A Derived class inherits, (takes on), the data members (instance variables) of the Base class. The data members define an object s state. A derived class may include (extend) additional data (and function) members.
10 Object States: ExtendedName Eg.1 10 class Name { private String firstname, lastname, title; public Name(String first, String last){ firstname = first; lastname = last; title = ; // ================================================ class ExtendedName extends Name{ private String middlename; How does ExtendedName s constructor ensure a valid state for Name? public ExtendedName(String firstname, String middlename, String lastname){? super(firstname, lastname); this.middlename = middlename; What does this do?
11 Object States : ExtendedName Eg.2 A two-parameter constructor: 11 public ExtendedName(String firstname, String lastname) { super(firstname, lastname); middlename = ;// this.middlename = ; How does this constructor ensure a valid state?
12 Object States : ExtendedName Eg.3 12 A derived class s methods should never put the base class in an invalid state. A valid state is: Meaningful in the base class Possible in the base class Invalid state public ExtendedName(String firstname, String middlename, String lastname){ super(, ); middlename = ; The above code would place the inherited base class instance variables in an invalid state because the base class constructors never specified empty string states.
13 Object States & Access 13 Inherited class may need to directly access the private instance variables in the base class! Private members of the base class may not be directly accessed by derived classes. How can you protect your original class from being corrupted by client code, but still allow derived class code to access/modify base class instance variables? Accessor and mutator methods could be provided, but they may be inappropriate for client code invocation. Some data members should be able to be changed by derived class code, but not by client class code? Solution: protected keyword
14 Object Access 14 Three ways to classify accessibility of class members: private, public, protected Protected members in the Base class are private in the Derived class Private members in the Base class are inaccessible in the Derived class, even to the methods of the Derived class. Public members in the Base class are public in the Derived class
15 Object states class Name { protected String firstname, lastname, title; 15 public Name(String first, String last){ firstname = first; lastname = last; // ================================================ class ExtendedName extends Name { private String middlename; public ExtendedName(String firstname, String middlename, String lastname){ super(firstname, lastname); title = ; //base member access this.middlename = middlename;
16 Object states 16 Which should you use in a base class? private, public, protected Some people think all base class data should be private Then the derived class can t possibly invalidate the base class! Others think all base class data should be protected Makes it easy to do what you want with the derived class Comparison: In base In derived public protected private Works like public Works like public Works like private Works like public Works like private inaccessible
17 Constructors 17 When a derived class s constructor is called: Room is reserved for all the data members of both the derived class and its base class in the same object. The base class data gets set up then the derived class data gets set up. Derived class constructor must pass needed info to the base class constructor: super(); firstname lastname title middlename
18 is-a vs. has-a 18 Is-a An object of a derived class is an object of the base class In the left picture, a is a Has-a This is how we ve programmed up until now. In the right picture, a has a
19 is-a vs. has-a 19 The is-a relationship is called inheritance The has-a relationship is called composition (aggregation) class Name2 { public Name2( String firstname, String middlename, String lastname ){... private Name n; private String middlename; What are some disadvantages to composition?
20 When to use Inheritance 20 If all methods of the original class X should be methods of the new class Y, then use inheritance. If some of the methods of the original class X should be methods of the new class Y, then use composition. If it feels right to say that a Y is an X (as in a part-timer is an employee ), then use inheritance. If it feels right to say that a Y has an X (as in an employee has a name ), then use composition.
21 Don t make this mistake: class Name { public String firstname, lastname; public Name (String f, String l){...; 21 /***********************************************/ class ExtendedName extends Name { public String firstname, lastname, middlename; public ExtendedName (String f, String m, String l){...; What s wrong here?
22 The Object class 22 We ve used the Object class many times. Vectors store only type object. An Object is the very first, basic class defined by Java. Every class implicitly inherits from the Object class (either directly or indirectly). It has some specific methods you can re-define (or override). Some are: boolean equals(object other) String tostring( )
23 Class hierarchies in Java Both of these are possible: Title could be moved from the base Name class since not all names have a title. Name 23 Name ExtendedName ExtendedName SuffixedName Two subclasses for 1 superclass SuffixedName Subclasses extended to another subclass
24 Overriding 24 When a method is defined in a base class, it may be overridden (redefined differently) in the derived class. Technique: 1) override a method in a derived class, (i.e. provide an implementation of a base class function in the derived class) 2) create an object of the derived type 3) invoke the overridden method Whether the object is cast as the base type or derived type, the derived method s implementation is used. Don t confuse this with overloading Overloading: writing 2 methods in one class with the same name, differing by parameter lists.
25 Overriding 25 class Name{ public String getinitials(){ return fname.substring(0,1) lname.substring(0,1) +. ; /***********************************************/ class ExtendedName extends Name{ public String getinitials(){ return fname.substring(0,1) +. + getmiddleinitial() lname.substring(0,1) +. ;
26 Definition: Polymorphism 26 Polymorphism, in programming, is the ability to write code that is independent of the exact type of objects upon which it is executing. Polymorphic code is general code that only relies upon the common properties of objects in an inheritance hierarchy. General polymorphic code treats all objects of an inheritance hierarchy as base class objects. Polymorphism depends on inheritance and overriding. An object of the subclass can be referred to as an object of the superclass. When an overridden method is invoked upon an object reference of the superclass, the body of the subclass s overridden method is executed, (not the base class implementation).
27 Polymorphism: simple example 27 Vector v; Name n = new Name( Bill, Clinton ); v.add(n); ExtendedName en = new ExtendedName( George, W, Bush ); v.add(en); /**********************************************/ At this point, we don t know (or care) what kind of Name is being referenced! for(int I=0; I < v.size(); I++){ String s = ( (Name) v.elementat(i) ).getinitials(); System.out.println(s); B.C. G.W.B.
28 Overriden Base Member Invocation 28 A derived class over-riding function may invoke the base class version by use of the super keyword class Name{ public void print(){ system.out.print( lastname +, + firstname);... /***********************************************/ class ExtendedName extends Name{ public void print(){ super.print(); //invoke base class version system.out.print(, + middlename);...
29 Do you understand? 29 Explain the difference between overloading and overriding Explain the difference between inheritance and polymorphism Explain the difference between base and derived What 3 things must we have in order for these concepts to be useful? Where is the Object class in the Java hierarchy? Why would you want to do any of this? Give examples.
30 Applets Applet classes inherit from the Applet class Powerful set of GUI tools / window management. 30 class mywebprogram extends Applet{ /******/
31 Factoring out Common Behavior 31 When designing a set of classes with some commonality, look for their common behavior Example: camera store sells: Lenses focal length, zoom capability (Boolean) Film storage temperature, # exposures Cameras w/lens(bool), shutter speed, body color... But all of them have: Description, quantity on hand, price, inventory number
32 Factoring out Common Behavior class InventoryItem{ InventoryItem( ){ // constructor String getdescription(){return desc; int getquantityonhand(){return qoh; int getprice(){return price; 32 String desc; // description int inventorynumber; int qoh; // quantity on hand int price;
33 Factoring out Common Behavior 33 class Lens extends InventoryItem{ Lens( ){ // constructor super( ); /* other code */ /* METHODS SPECIFIC TO LENS*/ boolean iszoom(){return zoom; double getfocallength(){return focallength; /* INSTANCE VARIABLES SPECIFIC TO LENS*/ boolean zoom; double focallength;
34 Abstract methods & classes 34 A base class doesn t have to define all its methods! It can simply provide a prototype. If any method in a class doesn t have an implementation, it is an abstract method. Any class with an abstract method is an abstract class. An abstract class cannot be instantiated. (You cannot create an object of that type.) Why are abstract classes useful then?
35 Abstract methods & classes 35 Suppose you want to print out your camera store inventory items. All items will be stored in the same vector, so when you getnextelement(), you won t know whether you have a lens, camera, or film. You could provide a method in InventoryItem called print(), and then override it in all subclasses But what would the body of InventoryItem.print() look like? You can t define it, because the info you print depends on the type of item.
36 Abstract methods & classes 36 abstract class InventoryItem{ public InventoryItem(){ // etc. abstract public void print(); // no body! // other stuff /**********************************/ class Lens extends InventoryItem{ void public print(){ if (zoom) System.out.printLn( Zoom ); System.out.println( Lens ); // other stuff
37 Abstract classes 37 Another example is the InputStream class. InputStream is abstract. You cannot instantiate it directly. But there are several derived classes which you can instantiate. StringBufferInputStream, FileInputStream, and FilterInputStream InputStreamReader s constructor requires an InputStream argument. We can give InputStreamReader an object from any derived class. Each derived class works differently, but InputStreamReader never needs to know what type of InputStream it has!
38 Interfaces 38 Sometimes you need to specify generic behavior for a group of classes that are not related in any obvious hierarchical way. Example: the Enumeration type. Suppose you add another inventory item to your camera store: a package deal, including a camera, several lenses, and several rolls of film. Your class must inherit from your InventoryItem class, for obvious reasons. But let s say you also want it to work as an Enumeration type spitting out one of the packaged items at a time.
39 Interfaces 39 Fortunately, Enumeration is not a class it s an interface. It has no method bodies, just prototypes. It is not a class. interface Enumeration{ Object nextelement(); boolean hasmoreelements();
40 The implements keyword class PackageDeal extends InventoryItem implements Enumeration{ 40 private Vector packageditems; public PackageDeal( ){ // etc... Object nextelement(){ boolean hasmoreelements(){
41 The implements keyword 41 Now, if you had a method that took an Enumeration and wrote out the contents myutils.writeenum(e, System.out); You could pass it your PackageDeal objects and it would do the same thing. myutils.writeenum(mypd, System.out);
42 The implements keyword 42 class myutils{ public static void writeenum(enumeration e, PrintStream output){ String s; while ( e.hasmoreelements() ){ s = e.nextelement().tostring(); output.println(s); tostring() inherited from Object class
Inheritance 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 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 informationFull file at Chapter 2 - Inheritance and Exception Handling
Chapter 2 - Inheritance and Exception Handling TRUE/FALSE 1. The superclass inherits all its properties from the subclass. ANS: F PTS: 1 REF: 76 2. Private members of a superclass can be accessed by a
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 informationCSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community
CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring
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 informationMore on Objects in JAVA TM
More on Objects in JAVA TM Inheritance : Definition: A subclass is a class that extends another class. A subclass inherits state and behavior from all of its ancestors. The term superclass refers to a
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 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 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 informationBuilding custom components IAT351
Building custom components IAT351 Week 1 Lecture 1 9.05.2012 Lyn Bartram lyn@sfu.ca Today Review assignment issues New submission method Object oriented design How to extend Java and how to scope Final
More informationPIC 20A Streams and I/O
PIC 20A Streams and I/O Ernest Ryu UCLA Mathematics Last edited: December 7, 2017 Why streams? Often, you want to do I/O without paying attention to where you are reading from or writing to. You can read
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 informationComputer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7
Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7 1 Problem Ralph owns the Trinidad Fruit Stand that sells its fruit on the street, and he wants to use a computer
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 informationEncapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass
Encapsulation Again Inheritance Encapsulation and Inheritance Inf1-OOP Inheritance and Interfaces Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics March 9, 2015 The Object
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 informationInf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics
Inf1-OOP Inheritance and Interfaces Ewan Klein, Perdita Stevens School of Informatics January 12, 2013 Encapsulation Again Inheritance Encapsulation and Inheritance The Object Superclass Flat vs. Nested
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 informationI/O streams. Byte Streams Character Streams InputStream ByteArrayInputStream FileInputStream FilterInputStream
Course Name: Advanced Java Lecture 9 Topics to be covered I/O streams Byte Streams Character Streams InputStream ByteArrayInputStream FileInputStream FilterInputStream Introduction A Stream is a sequence
More informationOverview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008
Overview Lecture 7: Inheritance and GUIs Written by: Daniel Dalevi Inheritance Subclasses and superclasses Java keywords Interfaces and inheritance The JComponent class Casting The cosmic superclass Object
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 informationLecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?
CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 5: Inheritance & Polymorphism Lecture Contents 2 What is Inheritance? Super-class & sub class Protected members Creating subclasses
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 informationSoftware Paradigms (Lesson 3) Object-Oriented Paradigm (2)
Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Table of Contents 1 Reusing Classes... 2 1.1 Composition... 2 1.2 Inheritance... 4 1.2.1 Extending Classes... 5 1.2.2 Method Overriding... 7 1.2.3
More informationAP Computer Science Chapter 10 Implementing and Using Classes Study Guide
AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within
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 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 informationInf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.
Inf1-OP Inheritance UML Class Diagrams UML: language for specifying and visualizing OOP software systems UML class diagram: specifies class name, instance variables, methods,... Volker Seeker, adapting
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 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 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 informationInf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics
Inf1-OP Inheritance Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics March 12, 2018 UML Class Diagrams UML: language for specifying and visualizing OOP software
More informationCS313D: ADVANCED PROGRAMMING LANGUAGE
CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 5: Inheritance & Polymorphism Lecture Contents 2 What is Inheritance? Super-class & sub class Protected members Creating subclasses
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 informationBinghamton University. CS-140 Fall Problem Solving. Creating a class from scratch
Problem Solving Creating a class from scratch 1 Recipe for Writing a Class 1. Write the class boilerplate stuff 2. Declare Fields 3. Write Creator(s) 4. Write accessor methods 5. Write mutator methods
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 7. Inheritance
Chapter 7 Inheritance Introduction to Inheritance Inheritance is one of the main techniques of objectoriented programming (OOP) Using this technique, a very general form of a class is first defined and
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 informationLecture 11.1 I/O Streams
21/04/2014 Ebtsam AbdelHakam 1 OBJECT ORIENTED PROGRAMMING Lecture 11.1 I/O Streams 21/04/2014 Ebtsam AbdelHakam 2 Outline I/O Basics Streams Reading characters and string 21/04/2014 Ebtsam AbdelHakam
More informationChapter 12: Inheritance
Chapter 12: Inheritance Objectives Students should Understand the concept and role of inheritance. Be able to design appropriate class inheritance hierarchies. Be able to make use of inheritance to create
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 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 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 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 informationSpecial error return Constructors do not have a return value What if method uses the full range of the return type?
23 Error Handling Exit program (System.exit()) usually a bad idea Output an error message does not help to recover from the error Special error return Constructors do not have a return value What if method
More informationFirst IS-A Relationship: Inheritance
First IS-A Relationship: Inheritance The relationships among Java classes form class hierarchy. We can define new classes by inheriting commonly used states and behaviors from predefined classes. A class
More informationCSC9T4: Object Modelling, principles of OO design and implementation
CSC9T4: Object Modelling, principles of OO design and implementation CSCU9T4 Spring 2016 1 Class diagram vs executing program The class diagram shows us a static view of the responsibilities and relationships
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 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 informationObject Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse
Object-Oriented Design Object Oriented Design Goals Robustness Adaptability Flexible code reuse Principles Abstraction Encapsulation Modularity March 2005 Object Oriented Design 1 March 2005 Object Oriented
More informationLanguage Features. 1. The primitive types int, double, and boolean are part of the AP
Language Features 1. The primitive types int, double, and boolean are part of the AP short, long, byte, char, and float are not in the subset. In particular, students need not be aware that strings are
More informationCS 251 Intermediate Programming Methods and Classes
CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with
More informationCS 251 Intermediate Programming Methods and More
CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with
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 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 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 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 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 informationObject-Oriented Design. March 2005 Object Oriented Design 1
Object-Oriented Design March 2005 Object Oriented Design 1 Object Oriented Design Goals Robustness Adaptability Flexible code reuse Principles Abstraction Encapsulation Modularity March 2005 Object Oriented
More informationSystems programming. Object Oriented Programming. I. Object Based Programming II. Object Oriented Programming
Systems programming Object Oriented Programming I. Object Based Programming II. Object Oriented Programming Telematics Engineering M. Carmen Fernández Panadero mcfp@it.uc3m.es 2010 1
More informationInstance Members and Static Members
Instance Members and Static Members You may notice that all the members are declared w/o static. These members belong to some specific object. They are called instance members. This implies that these
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 informationObject Oriented Programming. I. Object Based Programming II. Object Oriented Programming
Systems programming Object Oriented Programming I. Object Based Programming II. Object Oriented Programming Telematics Engineering M. Carmen Fernández Panadero mcfp@it.uc3m.es 2010 1
More informationAssoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.
Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All Inheritance A form of software reuse in which a new class is created by absorbing an existing class s members and enriching them with
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 informationObject Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)
Object Oriented Programming Part II of II Steve Ryder Session 8352 JSR Systems (JSR) sryder@jsrsys.com New Terms in this Section API Access Modifier Package Constructor 2 Polymorphism Three steps of 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 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 informationCOMP200 INTERFACES. OOP using Java, from slides by Shayan Javed
1 1 COMP200 INTERFACES OOP using Java, from slides by Shayan Javed Interfaces 2 ANIMAL picture food sleep() roam() makenoise() eat() 3 ANIMAL picture food sleep() roam() makenoise() eat() 4 roam() FELINE
More informationAggregation. Chapter 8. Java By Abstraction Chapter 8. Outline. 8.1 What is Aggregation? Copyright 2006 Pearson Education Canada Inc.
Chapter 8 Aggregation Java By Abstraction 8- Outline 8. What is Aggregation? 8.. Definition and Terminology 8..2 The Aggregate s Constructor 8..3 Accessors and Mutators 8..4 The Client s Perspective 8..5
More informationCLASSES AND OBJECTS IN JAVA
Lesson 8 CLASSES AND OBJECTS IN JAVA (1) Which of the following defines attributes and methods? (a) Class (b) Object (c) Function (d) Variable (2) Which of the following keyword is used to declare Class
More informationCONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.
Constructor in Java 1. What are CONSTRUCTORs? Constructor in java is a special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation. It constructs
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 informationInheritance and Interfaces
Inheritance and Interfaces what is inheritance? examples & Java API examples inheriting a method overriding a method polymorphism Object tostring interfaces Ex: sorting and Comparable interface Inheritance
More informationOBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616
OBJECT ORIENTED PROGRAMMING Course 4 Loredana STANCIU loredana.stanciu@upt.ro Room B616 Inheritance A class that is derived from another class is called a subclass (also a derived class, extended class,
More informationJava for Non Majors Spring 2018
Java for Non Majors Spring 2018 Final Study Guide The test consists of 1. Multiple choice questions - 15 x 2 = 30 points 2. Given code, find the output - 3 x 5 = 15 points 3. Short answer questions - 3
More informationInheritance (Part 5) Odds and ends
Inheritance (Part 5) Odds and ends 1 Static Methods and Inheritance there is a significant difference between calling a static method and calling a non-static method when dealing with inheritance there
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 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 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 informationCS260 Intro to Java & Android 03.Java Language Basics
03.Java Language Basics http://www.tutorialspoint.com/java/index.htm CS260 - Intro to Java & Android 1 What is the distinction between fields and variables? Java has the following kinds of variables: Instance
More informationName Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?
1. Overriding Methods A subclass can modify behavior inherited from a parent class. A subclass can create a method with different functionality than the parent s method but with the same: Name Return type
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 informationMore about inheritance
Main concepts to be covered More about inheritance Exploring polymorphism method polymorphism static and dynamic type overriding dynamic method lookup protected access 4.1 The inheritance hierarchy Conflicting
More informationRelationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor
CSE 143 Object & Class Relationships Inheritance Reading: Ch. 9, 14 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches dog Dog
More informationInheritance. Transitivity
Inheritance Classes can be organized in a hierarchical structure based on the concept of inheritance Inheritance The property that instances of a sub-class can access both data and behavior associated
More informationLab 2: File Input and Output
Lab 2: File Input and Output This lab introduces how to handle files as both input and output. We re coming back to Tracery (which you implemented in Lab 1) with this assignment but instead of always reading
More informationCMSC 132: Object-Oriented Programming II. Inheritance
CMSC 132: Object-Oriented Programming II Inheritance 1 Mustang vs Model T Ford Mustang Ford Model T 2 Interior: Mustang vs Model T 3 Frame: Mustang vs Model T Mustang Model T 4 Compaq: old and new Price:
More informationBM214E Object Oriented Programming Lecture 8
BM214E Object Oriented Programming Lecture 8 Instance vs. Class Declarations Instance vs. Class Declarations Don t be fooled. Just because a variable might be declared as a field within a class that does
More informationCS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors
1 CS/ENGRD 2110 FALL 2018 Lecture 5: Local vars; Inside-out rule; constructors http://courses.cs.cornell.edu/cs2110 Announcements 2 A1 is due tomorrow If you are working with a partner: form a group on
More informationUnit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW
Prepared by: Dr. Abdallah Mohamed, AOU-KW 1 1. Introduction 2. Objects and classes 3. Information hiding 4. Constructors 5. Some examples of Java classes 6. Inheritance revisited 7. The class hierarchy
More informationCSC 1214: Object-Oriented Programming
CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:
More informationInheritance, polymorphism, interfaces
Inheritance, polymorphism, interfaces "is-a" relationship Similar things (sharing same set of attributes / operations): a group / concept Similar groups (sharing a subset of attributes / operations): a
More informationWeiss Chapter 1 terminology (parenthesized numbers are page numbers)
Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement
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 informationRelationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.
CSE 143 Java Object & Class Relationships Inheritance Reading: Ch. 9, 14 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches
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. Chapter 7. Chapter 7 1
Inheritance Chapter 7 Chapter 7 1 Introduction to Inheritance Inheritance allows us to define a general class and then define more specialized classes simply by adding new details to the more general class
More informationBBM 102 Introduction to Programming II Spring Inheritance
BBM 102 Introduction to Programming II Spring 2018 Inheritance 1 Today Inheritance Notion of subclasses and superclasses protected members UML Class Diagrams for inheritance 2 Inheritance A form of software
More information