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

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

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II

Inheritance -- Introduction

CLASSES AND OBJECTS IN JAVA

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

PROGRAMMING LANGUAGE 2

What is Inheritance?

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

Java Object Oriented Design. CSC207 Fall 2014

Practice for Chapter 11

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

ITI Introduction to Computing II

CS200: Advanced OO in Java

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

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

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

Microsoft Visual Basic 2005: Reloaded

QUESTIONS FOR AVERAGE BLOOMERS

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

ITI Introduction to Computing II

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Chapter 7. Inheritance

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

ECE 122. Engineering Problem Solving with Java

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

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

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

Chapter 10 Classes Continued. Fundamentals of Java

CS-202 Introduction to Object Oriented Programming

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

Chapter 6 Introduction to Defining Classes

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

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

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

Polymorphism and Inheritance

What about Object-Oriented Languages?

Type Hierarchy. Lecture 6: OOP, autumn 2003

Inheritance, Polymorphism, and Interfaces

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

C++ Important Questions with Answers

Introduction to Programming Using Java (98-388)

Lecture 2: Java & Javadoc

Chapter 14 Abstract Classes and Interfaces

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

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Inheritance and Interfaces

Chapter 2: Java OO II X I A N G Z H A N G

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

CS111: PROGRAMMING LANGUAGE II

15CS45 : OBJECT ORIENTED CONCEPTS

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Inheritance (Outsource: )

JAVA MOCK TEST JAVA MOCK TEST II

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Lecture 18 CSE11 Fall 2013 Inheritance

CS260 Intro to Java & Android 03.Java Language Basics

More On inheritance. What you can do in subclass regarding methods:

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

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

Inheritance and Polymorphism

Programming Exercise 14: Inheritance and Polymorphism

Inheritance and Polymorphism

Inheritance and Polymorphism. CS180 Fall 2007

Data Abstraction. Hwansoo Han

Object Oriented Issues in VDM++

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. January 11, 2018


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

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

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

Introduction to Inheritance

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

The major elements of the object-oriented model

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

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

1 Shyam sir JAVA Notes

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Inheritance. Unit 8. Summary. 8.1 Inheritance. 8.2 Inheritance: example. Inheritance Overriding of methods and polymorphism The class Object

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Comp 249 Programming Methodology

9/10/2018 Programming Data Structures Inheritance

Programming II (CS300)

Understanding Inheritance and Interfaces

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Programming II (CS300)

Transcription:

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 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 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 8 Super() used to call the super-class constructor The super method can be called with parameters that match a super-class's constructor If the super-class constructor is not explicitly called, the compiler will add super() as the first instruction in the constructor of the sub-class 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

Polymorphism 9 Polymorphism literally means many forms. Polymorphism includes overriding and overloading Overloading creating methods with same name but different parameters Overriding re-defining the body of a method of superclass in a subclass to change the behavior of that method

Inheritance and Class Hierarchy 10 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 11 Partial Overriding Partial overriding involves calling a base class method by using super public class GoodWorker extends Worker { } @Override public void dowork( ){ super.dowork( ); // Work like a Worker drinkcoffee( ); // Take a break super.dowork( ); // Work like a Worker some more }

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 Private methods declared in a super class are by default final and cannot be overridden Final Class A final class cannot be extended Examples: String, System are final classes

Abstract Classes 13 An abstract class 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.

Interfaces 14 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 An interface can extend another interface

Interfaces 15 An interface should NOT be declared as private. Otherwise, it won t be used. An interface is a collection of constants and method declarations If a variable is defined in an interface, it should be initialized and will be final (constant) public interface Taxable { } double taxrate = 0.06; // taxrate will automatically be considered final ( a // constant) since it is declared in an interface //double taxdoublerate; // This declaration is incorrect since an interface // cannot contain data fields. It must contain either constants or signatures // of public methods public double calculatetax(); // methods that returns the current tax value

Interfaces 16 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 Example #1 17 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 Example #2 18 Practice Example #2 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 19 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 20 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