Inheritance CSC 123 Fall 2018 Howard Rosenthal
|
|
- Marian Bell
- 5 years ago
- Views:
Transcription
1 Inheritance CSC 123 Fall 2018 Howard Rosenthal
2 Lesson Goals Defining what inheritance is and how it works Single Inheritance Is-a Relationship Class Hierarchies Syntax of Java Inheritance The super Reference Method Overriding Much of the information in this section is adapted from: Java Illuminated 5 TH Edition, Anderson, Julie and Franceschi Herve, Jones and Bartlett, 2019 Starting Out With Objects From Control Structures Through Objects, Gaddis, Tony, Pearson Publishing,
3 The Three Pillars Of Object Oriented Programming There are three basic concepts used in object-oriented Encapsulation the organizing of data and behavior into objects Inheritance and Polymorphism are described in this and subsequent sections. We will also discuss the use of Interfaces, which greatly enhance the concept of inheritance 3
4 What Is Inheritance? Object oriented languages have a feature called inheritance, that enables you to define new classes based upon an existing class. The new classes are similar to the existing class, but have additional member variables and methods. This makes programming easier because you can build upon an existing class instead of starting out from scratch. Inheritance is part of the reason for the enormous success of modern software. Programmers are able to build upon previous work and to continuously improve and upgrade existing software. 4
5 Why Inheritance? Before object oriented programming if you had the source code for a class, you could copy the code and change it to do what you wanted. This approach had two major problems: It was hard to stay organized. Say that you already have several dozen classes which you need to keep and that you need additional classes based on the original ones. Also, say that you need several classes based on the new classes. You will end up with dozens of source files which are all versions of other source files that have been changed in various ways. Now say that a bug has been found in one of the source files. Some source files based on it need fixing; others, perhaps not. Without careful planning you will end up with an disorganized, inconsistent, buggy mess. You needed to study the original code. Say that you have a complicated class that basically does what you want, but you need a small modification. If you edit the source code, even to make a small change, you risk breaking something. So you must study the original code to be sure that your changes are correct which may not be easy. 5
6 Basic Principles Of Inheritance A common form of reuse of classes is inheritance. We can organize classes into hierarchies of functionality. The class at the top of the hierarchy (superclass) defines instance variables and methods common to all classes in the hierarchy. We derive a subclass, which inherits behavior and fields from the superclass. 6
7 Java and Inheritance The class that is used to define a new class is called a parent class (or superclass or base class.) The class based on the parent class is called a child class (or subclass or derived class.) In diagrams, the arrow points from the child to the parent. In Java, (unlike with humans) children inherit characteristics from just one parent. This is called single inheritance. Some languages allow a child to inherit from more than one parent. This is called multiple inheritance. With multiple inheritance, it is sometimes hard to tell which parent contributed what characteristics to the child (as with humans). Java avoids these problems by using single inheritance. 7
8 A Few Common Synonyms There are three sets of phrases for describing inheritance relationships: parent / child base class / derived class superclass / subclass Programmers use all three sets interchangeably. 8
9 Two Simple Questions (1) Can a parent class have more than one child class? Can a parent class inherit characteristics from its child class? 9
10 Two Simple Questions (2) Can a parent class have more than one child class? Yes, a parent can have any number of children. But child can have only one parent. Can a parent class inherit characteristics from its child class? No. Inheritance goes in only one direction. 10
11 Objects And Classes (1) The picture shows a parent class and a child class, and some objects that have been constructed from each. These objects are shown as rectangles. In the picture, "Joe's car," "Mary's Ford," and "Bob's Ford" represent objects. The cloudy classes represent designs, not objects. Objects are constructed as a program runs. An object is constructed by following a description in a class file that was created by compiling a Java source program. How many types of automobile are there in the diagram? How many automobile objects are there in the diagram? 11
12 Objects And Classes (2) How many types of automobile are there in the diagram? 2 How many automobile objects are there in the diagram? 3 12
13 Hierarchies (1) We can organize classes into hierarchies of functionality. The class at the top of the hierarchy (superclass) defines instance variables and methods common to all classes in the hierarchy. We derive a subclass, which inherits behavior and fields from the superclass. 13
14 Hierarchies (2) This picture shows a hierarchy of classes. It shows that "Ford is-a automobile," "Nissan is-a automobile," and that "VW is-a automobile." It also shows that "Sentra is-a Nissan." In a hierarchy, each class has at most one parent but might have several children classes. The class at the top of the hierarchy has no parent. This class is called the root of the hierarchy. 14
15 Visualizing A Simple Hierarchy In UML In UML diagrams, arrows point from the subclass to the superclass. 15
16 Superclasses and Subclasses A superclass can have multiple subclasses. Subclasses can be superclasses of other subclasses. A subclass can inherit directly from only one superclass. All classes in Java inherit from the Object class (to be discussed later). A big advantage of inheritance is that we can write code that is common to multiple classes once and reuse it in subclasses. A subclass can define new instance variables and methods, some of which may override (hide) those of a superclass. 16
17 Specifying Inheritance The syntax for defining a subclass is to use the extends keyword in the class header, as in accessmodifier class SubclassName extends SuperclassName { // class definition new variables, constructors and other methods (new or overriding) } The superclass name specified after the extends keyword is called the direct superclass. As mentioned, a subclass can have many superclasses, but only one direct superclass. 17
18 A Simple Example With Inheritance Let s look at the following files: TestVideoStore.java, Video.java, Movie.java The class Movie is a subclass of Video. An object of type Movie has these members: member member member title length avail director rating tostring() inherited from Video inherited from Video inherited from Video defined in Movie defined in Movie inherited from Video gettitle() getlength() getavailabl e() getdirector () getrating() inherited from Video inherited from Video inherited from Video defined in Movie defined in Movie settitle() setlength() setavailable () inherited from Video inherited from Video inherited from Video 18
19 Using A Super Constructor (1) Look at he constructor for class Movie. The class definition for Video has a constructor that initializes the instance variables of Video objects. The class Movie has a constructor that initializes the instance variables of Movie objects. The statement super(ttl, lngth) invokes a constructor of the parent to initialize some variables of the child. There are two constructors in the parent. The one that is invoked is the one that matches the argument list in super(ttl, lngth). The next two statements initialize variables that only Movie has. Important Note: super() must be the first statement in the subclass's constructor. super() will be inserted into any constructor of the subclass as the first statement if you do not do not explicitly use a super constructor. If you have more than one constructor in the subclass you can use the super() with or without parameter as appropriate. 19
20 Using A Super Constructor (2) An example. If the code looks like this: public Movie( String ttl, int lngth, String dir, String rtng ) { settitle( ttl ); // initialize inherited variables setlength( lngth ); setavailable( true ); director = dir; // initialize Movie variables rating = rtng; } Then the following insert takes place: public Movie( String ttl, int lngth, String dir, String rtng ) { super(); // inserted by compiler: parent's no-argument constructor settitle( ttl ); // initialize inherited variables setlength( lngth ); setavailable( true ); director = dir; // initialize Movie variables rating = rtng; } Note: In our program the class definition for Video (the superclass) lacks a noargument constructor. The proposed constructor (above) calls for such a constructor so it would cause a syntax error. 20
21 A Few Notes About The Default Constructor The programmer can explicitly write a no-argument constructor for a class. If the programmer does not write any constructors for a class, then a no-argument constructor (called the default constructor) is automatically supplied. If the programmer writes even one constructor for a class then the default constructor is not automatically supplied. So: if you write a constructor for a class, then you must also write a no-argument constructor if one is expected. Look at Test2VideoStore.java 21
22 Inheritance Rules for Constructors 22
23 Overriding A Parent Method A child's method overrides a parent's method when it has the same signature as a parent method. Remember that the signature of a method is the name of the method and its parameter list. When overriding a method with the same signature you can change the return type under certain conditions. Overriding method can have different return type but this new type must be A non-primitive and A subclass of what base class s overridden method is returning (i.e. co-variant Return Type). Example if a reference to a Video is returned in the superclass a reference to a Movie can be returned in the subclass method that is overriding Now the parent has its method, and the child has its own method with the same signature. Now look at Test3VideoStore.java and Movie3.java to see the overriding of tostring() in Movie You can also incorporate super in the process to print out the common parts the same way. 23
24 Inheritance Rules For Overwritten Methods 24
25 private Variables and Methods In A Superclass Superclass members declared as private are not inherited, although they are part of the subclass. Thus, a title, length and avail instance variable is allocated to all Movie objects, but methods of the Movie class cannot directly access title, length or avail directly. To set or get the value of title, length or avail, the Movie methods must call the inherited getxxx methods, which are public methods. This simplifies maintenance because the Video class enforces the data validation rules for title, length or avail. 25
26 protected Variables and Methods In A Superclass protected members are inherited by subclasses (like public members), while still being hidden from client classes (like private members). This means that the variables and methods that are protect can be directly accessed Also, any class in the same package as the superclass can directly access a protected field, even if that class is not a subclass. Disadvantage: Because more than one class can directly access a protected field, protected access compromises encapsulation and complicates maintenance. For that reason, we prefer to use private, rather than protected, for our instance variables. 26
27 A Few More Notes On protected Members Declaring fields as private preserves encapsulation. Subclass methods call superclass methods to set the values of the fields, and the superclass methods enforce the validation rules for the data. However, calling methods incurs processing overhead. Declaring fields as protected allows them to be accessed directly by subclass methods. Classes outside the hierarchy and package must use accessors and mutators for protected fields. Advantage: protected fields can be accessed directly by subclasses, so there is no method-invocation overhead. Disadvantage: Maintenance is complicated because the subclass also needs to enforce validation rules. Recommendation: Define protected fields only when high performance is necessary. Avoid directly setting the values of protected fields in the subclass. 27
28 Inheritance Rules For Classes and Subclasses 28
29 The Object Class All classes have a parent class (have a super class) except one. The class at the top of the Java class hierarchy is called Object. If a class definition does not extend a parent class then it automatically has Object as a parent class. If a class definition does extend a parent class, then if the parent class does not extend a parent it automatically has Object as a parent class. And so on. Ultimately all classes have Object as an ancestor. This means that all classes in Java share some common characteristics. Those characteristics are defined in Object. For example, all classes have a tostring() method because the class Object defines that method so all classes get it by inheritance. Of course, usually when you write a class you override the tostring() method. Constructors for our classes have not mentioned Object. According to the rule, the compiler automatically does this: public Video( String ttl, int lngth ) { super(); // use the super class's constuctor title = ttl; length = lngth; avail = true; } And Video automatically has what is in red done: class Video extends Object 29
30 The Java Hierarchy for Video 30
31 Programming Exercise 1 Extending A class (1) Extending the Game class Download the Game class from Lesson 12 Classwork and read its structure Create a subclass PCBasedGame Add three new instance values minimum megabytes of RAM required, minimum number of disk megabytes required, (both as type int), and required gigahertz speed of processor (as a double) Create accessors and mutators for each new instance variable Create an overriding tostring() method and equals() method equals method is true if each pair of instance variables are equal Write a PCBasedClient class with the main program that does the following Reads in the description, required RAM MBs, disk MBs and Gigahertz speed for a PCBasedGame object pcbg1. Create a pcbg1 object Use tostring to print out pcbg1 Reads in the description, required RAM MBs, disk MBs and Gigahertz speed for a PCBasedGame object pcbg2. Create a pcbg2 object Use tostring to print out pcbg2 Use equals to check equality between the two games Use accessors and mutators to set all pcbg2 instance variable values to pcbg1 values Now use equals again to check between the two games 31
32 Programming Exercise 1 Extending A class (2) Please enter the game description for game one: Monopoly Please enter the required RAM MBs and Disk Size MBs for game one as type int: Please enter the required CPU speed in Gigahertz for game one as a double: 32.7 Game Description: Monopoly Minimum configuration: RAM: 24 MB; hard disk: 120 MB; CPU GHZ Please enter the game description for game two: Scrabble Please enter the required RAM MBs and Disk Size MBs for game two as type int: Please enter the required CPU speed in Gigahertz for game two as a double: 45.2 Game Description: Monopoly Minimum configuration: RAM: 24 MB; hard disk: 120 MB; CPU GHZ pcbg1 and pcbg2 are not equal pcbg1 and pcbg2 are now equal 32
Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal
Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Define and discuss abstract classes Define and discuss abstract methods Introduce polymorphism Much of the information
More informationReviewing Java Implementation Of OO Principles CSC Spring 2019 Howard Rosenthal
Reviewing Java Implementation Of OO Principles CSC 295-01 Spring 2019 Howard Rosenthal Course References Materials for this course have utilized materials in the following documents. Additional materials
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 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 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 informationInterfaces CSC 123 Fall 2018 Howard Rosenthal
Interfaces CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Defining an Interface How a class implements an interface Using an interface as a data type Default interfaces Implementing multiple interfaces
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 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 informationA Formal Presentation On Writing Classes With Rules and Examples CSC 123 Fall 2018 Howard Rosenthal
A Formal Presentation On Writing Classes With Rules and Examples CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Defining a Class Defining Instance Variables Writing Methods The Object Reference this The
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 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 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 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 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 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 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 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 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 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 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 informationProgramming II (CS300)
1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism 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 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 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 informationChapter 10 Classes Continued. Fundamentals of Java
Chapter 10 Classes Continued Objectives Know when it is appropriate to include class (static) variables and methods in a class. Understand the role of Java interfaces in a software system and define an
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 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 information9/10/2018 Programming Data Structures Inheritance
9/10/2018 Programming Data Structures Inheritance 1 Email me if the office door is closed 2 Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same
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 informationInheritance and Polymorphism
Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L 10.1 10.3 1 Interface Hierarchies Inheritance can
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 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 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 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 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 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 informationChapter 6 Reflection
Table of Contents Date(s) Title/Topic Page #s 3/11 Chapter 6 Reflection/Corrections 94 Chapter 7: Inheritance 95 7.1 Creating Subclasses 96 Chapter 6 Reflection look over your Ch 6 Test and write down
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 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 informationENCAPSULATION AND POLYMORPHISM
MODULE 3 ENCAPSULATION AND POLYMORPHISM Objectives > After completing this lesson, you should be able to do the following: Use encapsulation in Java class design Model business problems using Java classes
More informationInheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Inheritance Chapter 8 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Class Inheritance: Deriving classes Method overriding Class hierarchies Abstract classes Visibility and inheritance
More informationASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java
Write a program in Java to create a player class. Inherit the classes Cricket_player, Football_player and Hockey_player from player class. The objective of this assignment is to learn the concepts of inheritance
More information3. Can an abstract class include both abstract methods and non-abstract methods? D
Assignment 13 Abstract Classes and Polymorphism CSC 123 Fall 2018 Answer Sheet Short Answers. Multiple Choice 1. What is an abstract class? C A. An abstract class is one without any child classes. B. An
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 informationECE 122. Engineering Problem Solving with Java
ECE 122 Engineering Problem Solving with Java Lecture 17 Inheritance Overview Problem: Can we create bigger classes from smaller ones without having to repeat information? Subclasses: a class inherits
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 informationComp 249 Programming Methodology
Comp 249 Programming Methodology Chapter 7 - Inheritance Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted,
More informationChapter 4 Defining Classes I
Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the
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 informationChapter 15: Object Oriented Programming
Chapter 15: Object Oriented Programming Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey How do Software Developers use OOP? Defining classes to create objects UML diagrams to
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 informationInheritance and Interfaces
Inheritance and Interfaces Object Orientated Programming in Java Benjamin Kenwright Outline Review What is Inheritance? Why we need Inheritance? Syntax, Formatting,.. What is an Interface? Today s Practical
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 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 informationOOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.
OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.
More information8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14
8.1 Inheritance superclass 8.1 Class Diagram for Words! Inheritance is a fundamental technique used to create and organize reusable classes! The child is- a more specific version of parent! The child inherits
More informationINHERITANCE AND EXTENDING CLASSES
INHERITANCE AND EXTENDING CLASSES Java programmers often take advantage of a feature of object-oriented programming called inheritance, which allows programmers to make one class an extension of another
More informationCS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University
CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Inheritance Three main programming mechanisms that constitute object-oriented programming (OOP) Encapsulation
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 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 informationHandout 9 OO Inheritance.
Handout 9 CS603 Object-Oriented Programming Fall 2016 Page 1 of 11 Handout 9 OO Inheritance. All classes in Java form a hierarchy. The top of the hierarchy is class Object Example: classicalarchives.com
More informationCS313D: ADVANCED PROGRAMMING LANGUAGE
CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 4(b): Inheritance & Polymorphism Lecture Contents What is Inheritance? Super-class & sub class The object class Using extends keyword
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 informationDefining Classes and Methods
Defining Classes and Methods Chapter 5 Modified by James O Reilly Class and Method Definitions OOP- Object Oriented Programming Big Ideas: Group data and related functions (methods) into Objects (Encapsulation)
More informationInheritance (Part 2) Notes Chapter 6
Inheritance (Part 2) Notes Chapter 6 1 Object Dog extends Object Dog PureBreed extends Dog PureBreed Mix BloodHound Komondor... Komondor extends PureBreed 2 Implementing Inheritance suppose you want to
More informationInheritance. A key OOP concept
Inheritance A key OOP concept Setting the scene Why do we need inheritance? Inheritance enables you to define a new class based upon an existing class. The new class is similar to the existing class, but
More informationInheritance, and Polymorphism.
Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to
More informationCS11 Introduction to C++ Fall Lecture 7
CS11 Introduction to C++ Fall 2012-2013 Lecture 7 Computer Strategy Game n Want to write a turn-based strategy game for the computer n Need different kinds of units for the game Different capabilities,
More information11/19/2014. Inheritance. Chapter 7: Inheritance. Inheritance. Inheritance. Java Software Solutions for AP* Computer Science A 2nd Edition
Chapter 7: Inheritance Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William Loftus, and Cara Cocking Inheritance Inheritance allows a software developer
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 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 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 informationInheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes
Inheritance Inheritance is the mechanism of deriving new class from old one, old class is knows as superclass and new class is known as subclass. The subclass inherits all of its instances variables and
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 informationCOMP 110/L Lecture 19. Kyle Dewey
COMP 110/L Lecture 19 Kyle Dewey Outline Inheritance extends super Method overriding Automatically-generated constructors Inheritance Recap -We talked about object-oriented programming being about objects
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 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 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 informationInheritance and Polymorphism. CS180 Fall 2007
Inheritance and Polymorphism CS180 Fall 2007 Definitions Inheritance object oriented way to form new classes from pre-existing ones Superclass The parent class If class is final, cannot inherit from this
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 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 informationWhat are the characteristics of Object Oriented programming language?
What are the various elements of OOP? Following are the various elements of OOP:- Class:- A class is a collection of data and the various operations that can be performed on that data. Object- This is
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 informationStarting Out with Java: From Control Structures Through Objects Sixth Edition
Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 10 Inheritance Chapter Topics (1 of 2) 10.1 What Is Inheritance? 10.2 Calling the Superclass Constructor 10.3 Overriding
More informationObject Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia
Object Oriented Programming in Java Jaanus Pöial, PhD Tallinn, Estonia Motivation for Object Oriented Programming Decrease complexity (use layers of abstraction, interfaces, modularity,...) Reuse existing
More informationData Abstraction. Hwansoo Han
Data Abstraction Hwansoo Han Data Abstraction Data abstraction s roots can be found in Simula67 An abstract data type (ADT) is defined In terms of the operations that it supports (i.e., that can be performed
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 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 informationCompuScholar, Inc. 9th - 12th grades
CompuScholar, Inc. Alignment to the College Board AP Computer Science A Standards 9th - 12th grades AP Course Details: Course Title: Grade Level: Standards Link: AP Computer Science A 9th - 12th grades
More informationZhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson
Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Introduction History, Characteristics of Java language Java Language Basics Data types, Variables, Operators and Expressions Anatomy of a Java Program
More informationComputer Science 210: Data Structures
Computer Science 210: Data Structures Summary Today writing a Java program guidelines on clear code object-oriented design inheritance polymorphism this exceptions interfaces READING: GT chapter 2 Object-Oriented
More informationChapter 10: Inheritance
Chapter 10: Inheritance Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter Topics Chapter 10 discusses the following main topics: What Is Inheritance?
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 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 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 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 informationUNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED
UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED EXERCISE 11.1 1. static public final int DEFAULT_NUM_SCORES = 3; 2. Java allocates a separate set of memory cells in each instance
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 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 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 information