Programming II (CS300)

Similar documents
Programming II (CS300)

Chapter 5 Object-Oriented Programming

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Inheritance and Polymorphism

CMSC 132: Object-Oriented Programming II

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

CMSC 132: Object-Oriented Programming II

Programming II (CS300)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Chapter 10 Classes Continued. Fundamentals of Java

Inheritance -- Introduction

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

What is Inheritance?

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Java Object Oriented Design. CSC207 Fall 2014

CLASSES AND OBJECTS IN JAVA

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CS313D: ADVANCED PROGRAMMING LANGUAGE

OVERRIDING. 7/11/2015 Budditha Hettige 82

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Object Oriented Programming. Java-Lecture 11 Polymorphism

PROGRAMMING LANGUAGE 2

CS200: Advanced OO in Java

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

ITI Introduction to Computing II

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Microsoft Visual Basic 2005: Reloaded

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

ITI Introduction to Computing II

Polymorphism and Inheritance

CS-202 Introduction to Object Oriented Programming

JAVA MOCK TEST JAVA MOCK TEST II

Data Structures (list, dictionary, tuples, sets, strings)

Inheritance (Outsource: )

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Inheritance and Interfaces

CS 11 java track: lecture 3

CS313D: ADVANCED PROGRAMMING LANGUAGE

Inheritance, Polymorphism, and Interfaces

Polymorphism. return a.doublevalue() + b.doublevalue();

Practice for Chapter 11

ECE 122. Engineering Problem Solving with Java

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Chapter 7. Inheritance

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

And Even More and More C++ Fundamentals of Computer Science

Introduction to Object-Oriented Programming

Polymorphism and Interfaces. CGS 3416 Spring 2018

Programming Exercise 14: Inheritance and Polymorphism

Inheritance CSC 123 Fall 2018 Howard Rosenthal

The Essence of Object Oriented Programming with Java and UML. Chapter 2. The Essence of Objects. What Is an Object-Oriented System?

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Full file at Chapter 2 - Inheritance and Exception Handling

Inheritance and Polymorphism

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

QUESTIONS FOR AVERAGE BLOOMERS

Chapter 11 Classes Continued

Chapter 6 Introduction to Defining Classes

C++ Important Questions with Answers

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

Chapter 14 Abstract Classes and Interfaces

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Cloning Enums. Cloning and Enums BIU OOP

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CO Java SE 8: Fundamentals

Object-Oriented Design. March 2005 Object Oriented Design 1

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Testing Object-Oriented Software. COMP 4004 Fall Notes Adapted from Dr. A. Williams

What about Object-Oriented Languages?

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Data Abstraction: The Walls

Motivations. Objectives. object cannot be created from abstract class. abstract method in abstract class

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Understanding Inheritance and Interfaces

INHERITANCE. Spring 2019

Computer Science II (20073) Week 1: Review and Inheritance

CS111: PROGRAMMING LANGUAGE II

Fundamentals of Object Oriented Programming

Programming II (CS300)

Subclassing, con.nued Method overriding, virtual methods, abstract classes/methods. COMP 401, Spring 2015 Lecture 9 2/19/2015

Chapter 12. OOP: Creating Object- Oriented Programs. McGraw-Hill. Copyright 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Programming II (CS300)

Lecture Notes on Programming Languages

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

CH. 2 OBJECT-ORIENTED PROGRAMMING

Inheritance. The Java Platform Class Hierarchy

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

Object-Oriented Programming

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Transcription:

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 Classes Interfaces Practice Examples Keep in Mind

Introduction 3 Fundamental principles of OOP (1/2) Encapsulation Grouping the data (fields variables) and the operations that apply to them (methods) into one unit called a class, while hiding the implementation details Abstraction Providing the essential (relevant) features of an object while hiding the implementation details

Introduction 4 Fundamental Principles of OOP (2/2) Principles allowing classes to express the similarities among objects that share some, but not all, of their structure and behavior Inheritance Principle used to reuse code among related classes Inheritance models the IS-A relationship Polymorphism refers to the Ability of a reference variable to take different forms Provision of the same interface for objects of different forms/shapes

Inheritance and Class Hierarchy 5 In JAVA, the class Object, is the root of the class hierarchy Every class has Object as the superclass. The extends clause is used to declare that a class is derived from another class. Inheritance allows us to derive classes from a base class without disturbing the implementation of the base class. In an IS-A relationship, the derived class (or sub-class) is a (variation of the) base class (or super-class) Multiple inheritance is not allowed in Java

Inheritance and Class Hierarchy 6 public class B extends A { }... <changes and additions> Several classes can be declared as subclasses of the same superclass. Inheritance can also extend over several generations of classes. For instance class E is considered as a sub-class of class A.

Inheritance and Class Hierarchy 7 Example of Class Hierarchy Throwable class (java.lang.throwable) is the base class for all Exception classes

Inheritance and Class Hierarchy 8 Example (Throwable class and its sub-classes) All exception objects belong to a subclass of the standard class java.lang.throwable The above diagram shows how the different classes and subclasses of exceptions and errors are arranged This diagram is called class hierarchy. It shows the relationship among various types of exception

Inheritance and Class Hierarchy 9 Example of Class Hierarchy (Throwable class and its subclasses) Throwable has two direct subclasses, Error and Exception. Each one of them in turn has many other predefined subclasses. Subclasses of the class Exception represent exceptions that can be caught A RuntimeException generally indicates a bug in the program, that the programmer should fix rather than handle it with a trycatch block Exception subclasses that require mandatory exception-handling are shown in green in the above diagram. They represent checked exceptions The programmer can create new exception subclasses to represent new types of exception

Inheritance and Class Hierarchy 10 General description of a subclass public class SubClass extends SuperClass{ // Any members that are not listed are inherited unchanged // except for constructor. // public section (public members) // * Constructor(s) if default is not acceptable // * SuperClass's methods whose definitions will change in SubClass // These methods will be overridden. The new definition will be // applied to objects of the subclass class. // * Additional public methods // New Methods will be defined here } // private section (private members) // * Additional data fields (generally private) // * Additional private methods [optional]

Inheritance and Class Hierarchy 11 Super used to call the super-class constructor The super method can be called with parameters that match a super-class's constructor Access Modifiers private Visible to the class, where they are defined, only protected Visible to the package and all subclasses public Visible to the world

Inheritance and Class Hierarchy 12 Final Method A final method is invariant over the inheritance hierarchy and cannot be overridden It is a good practice to declare a method as final if it should not be overridden by a subclass Final Class A final class cannot be extended Examples: String, System are final classes

Inheritance and Class Hierarchy 13 Overriding a method Methods in the superclass can be overridden in the subclasses The overridden subclass method should have the same signature as it is defined in the super-class have the same return type not add exceptions to the throws list declared in the super-class method using throws clause should not reduce visibility (for instance if the method in the super-class is declared as public, it should not be overridden as a protected or private helper class

Inheritance and Class Hierarchy 14 Partial Overriding Partial overriding involves calling a base class method by using super

Polymorphism 15 Polymorphism literally means many forms.

Polymorphism 16 Examples

Abstract Classes 17 An abstract class and cannot be constructed, even though it may declare and implement constructors A class with at least one abstract method must be an abstract class. An abstract method is a method that declares functionality that all derived class objects must eventually implement An abstract class may contain zero or many abstract methods.

Abstract Classes 18

Interfaces 19 The interface is an abstract class that contains no implementation details. An interface can be implemented by a class A class that implements an interface provides implementation details for all the abstract methods declared in that interface We do not use the keyword abstract to declare the abstract methods of an interface. It is implicit. A class that implements an interface behaves as if it had extended an abstract class specified by that interface

Interfaces 20 Difference between an interface and an abstract class The interface is not allowed to provide any implementation details either in the form of data fields or implemented methods An abstract class can provide implementation details in the form of data fields or implemented methods even though it cannot be instantiated. A class can implement different interfaces. But can extend only one abstract class

Practice Examples 21 Practice Example #5.1 @see interface FormatTraceFile.java @see class StandartFormatTraceFile implements FormatTraceFile StandartFormatTraceFile.java Input file: trace.txt

Practice Examples 22 Practice Example #5.2 @see interface Comparable.java @see abstract class Rectangle (Ractangle.java) @see class RectanglePlus extends Rectangle implements Comparable RectanglePlus.java

Practice Example #3 23 Relationship between derived, base classes, and implemented interfaces @see Pet class (Pet.java) @see Animal interface (Animal.java) @see Cat class that extends Pet class and implements Animal interface (Cat.java)

Practice Examples 24 Practice Example #5.3 Object Cloning Shallow Copy versus Deep Copy Cloning is a process of creating an exact copy of an existing object in the memory. In java, clone() method of java.lang.object class is used for cloning an object Only objects which implement Cloneable interface are eligible for cloning process. @see ShallowCopyInJava and DeepCopyInJava classes

Keep in Mind 25 Classes should hide their data and implementation of their methods A derived class inherits all data members from the base class and may add more data members. In java, a class can extend only one super-class In java, a class can implement many different interfaces Inheritance IS-A relationship: the derived class is a (variation of the) base class. Composition HAS-A relationship: a class A has an instance of the another class B.

Keep in Mind 26 The derived class inherits all methods from the base class. It may accept or redefine them. It also can define new methods. A protected class variable field is visible to the derived class and also classes in the same package. Declaring data members as protected or public violates the spirit of encapsulation and information hiding It would better to declare data members as private and write accessor and mutator methods. An abstract class cannot be constructed. It serves to specify the functionality of derived classes Final methods may not be overridden. Final classes may not be extended