Multiple Inheritance, Abstract Classes, Interfaces

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

Written by John Bell for CS 342, Spring 2018

Introduction to OO Concepts

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Object-Oriented Programming

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

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

1 Shyam sir JAVA Notes

Introduction to Java Written by John Bell for CS 342, Spring 2018

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

Introduction to Programming Using Java (98-388)

Assigned Date: August 27, 2014 Due Date: September 7, 2015, 11:59 PM

Full file at Chapter 2 - Inheritance and Exception Handling

Inheritance, Polymorphism, and Interfaces

Inheritance and Polymorphism

Index COPYRIGHTED MATERIAL

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Compaq Interview Questions And Answers

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

JAVA MOCK TEST JAVA MOCK TEST IV

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?

Generics method and class definitions which involve type parameters.

Java Object Oriented Design. CSC207 Fall 2014

Fall Problem Set 1

CO Java SE 8: Fundamentals

20 Most Important Java Programming Interview Questions. Powered by

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

Inheritance -- Introduction

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

Data Structures and Other Objects Using C++

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

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

Object-Oriented Design. March 2005 Object Oriented Design 1

Chapter 9 Inheritance

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Abstract Classes and Interfaces

CS 221 Review. Mason Vail

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

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

ECE 122. Engineering Problem Solving with Java

Midterm Exam CS 251, Intermediate Programming March 6, 2015

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

OVERRIDING. 7/11/2015 Budditha Hettige 82

CSEN401 Computer Programming Lab. Topics: Object Oriented Features: Abstraction and Polymorphism

CompSci 125 Lecture 20. Inheritance: Introduction, Overrides UML: Introduction to Class Diagrams

Java Programming Training for Experienced Programmers (5 Days)

Certification In Java Language Course Course Content

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

CS 162, Lecture 25: Exam II Review. 30 May 2018

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Object. OutputStream write(int) write(byte[]) write(byte[], int, int) FilterOutputStream write(int) write(byte[]) write(byte[], int, int)

Practice for Chapter 11

Inheritance and Polymorphism

Come & Join Us at VUSTUDENTS.net

Declarations and Access Control SCJP tips

CS365 Midterm -- Spring 2019

Classes, objects, methods and properties

Exception-Handling Overview

ITI Introduction to Computing II

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

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

6.Introducing Classes 9. Exceptions

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Handout 9 OO Inheritance.

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

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

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

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

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

ITI Introduction to Computing II

Java SE 8 Programming

Logistics. Final Exam on Friday at 3pm in CHEM 102

CLASS DESIGN. Objectives MODULE 4

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

Inheritance. Chapter 7. Chapter 7 1

Get Unique study materials from

Classes and Inheritance Extending Classes, Chapter 5.2

CS313D: ADVANCED PROGRAMMING LANGUAGE

1Z0-808 oracle. Number: 1Z0-808 Passing Score: 800 Time Limit: 120 min.

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

! labs last week. ! still time to work through lab 7 (midterm correction) ! can earn back up to 5 out of 70 points

Super-Classes and sub-classes

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Java SE 8 Programming

More Relationships Between Classes

A base class (superclass or parent class) defines some generic behavior. A derived class (subclass or child class) can extend the base class.

Inheritance and Testing Spring 2018 Exam Prep 4: February 11, 2019

CH. 2 OBJECT-ORIENTED PROGRAMMING

Chapter 13. Inheritance and Polymorphism. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Inheritance and Substitution (Budd chapter 8, 10)

C++ Important Questions with Answers

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

15CS45 : OBJECT ORIENTED CONCEPTS

QUESTIONS FOR AVERAGE BLOOMERS

CS 251 Intermediate Programming Inheritance

Transcription:

Multiple Inheritance, Abstract Classes, Interfaces Written by John Bell for CS 342, Spring 2018 Based on chapter 8 of The Object-Oriented Thought Process by Matt Weisfeld, and other sources. Frameworks Frameworks provide structure and support suitable for use in a variety of ( related ) programs. For example, most Microsoft Office programs have similar toolbars, menus, key commands and general look-and-feel and behaviors. The Java API includes GUI classes suitable for many apps, such as windows, buttons, menus, dialogs, Specialty frameworks may apply to a particular domain, such as e-commerce. ( shopping carts,... ) JUnit is a well-known framework for conducting ( automated ) unit tests of Java code. See also xunit. 1

Multiple Inheritance Languages such as C++ allow classes to inherit from multiple sources, which can be quite useful: Multiple Inheritance Problems 1. How to store inherited fields double gpa double int gpa or gpa? 2

Multiple Inheritance Problems 2. How to store and resolve multiply inherited fields double gpa int hours double double hours int gpa??? Multiple Inheritance Problems 3. How to store inherited fields from a common ancestor ( The diamond problem. ) Human String name double gpa double int??? gpa 3

Abstract Methods form a Contract Abstract methods are declared as required components of a class, but not provided. This forms an obligation to provide these methods, in the form of a contract. Abstract classes cannot be instantiated. Descendants of abstract classes must either fulfill the obligations of the contracts declared in all of their ancestors, or else they are also abstract. A class with ONLY abstract methods is termed pure abstract. Inheriting from Abstract Classes Inheriting from abstract classes can alleviate ( some of ) the problems of multiple inheritance. Since the methods were not provided in ancestor classes, they are not inherited in descendant classes, alleviating some of the storage and resolution problems. Providing the required methods can satisfy the contractual obligations of multiple ancestors with a single method, provided there are no conflicts in argument signatures. ( Overriding, not overloading. ) This also satisfies the same obligation inherited through multiple ancestor paths ( the diamond problem. ) 4

Interfaces Interfaces are like pure abstract classes - They declare contractual obligations for implementers without providing any implementations. UML Distilled by Fowler diagrams interfaces as: ( StarUML does not diagram interfaces well. ) Benefits of Interfaces Interfaces define common required properties ( methods ) that different classes must provide, without incurring the full overhead of inheritance. Classes implementing a common interface are not related by either parent-child or sibling relationships. They merely share some common characteristics. Interfaces support polymorphism, with a common set of method signatures for all implementing classes. 5

Interfaces in Java Java does not allow multiple inheritance, to avoid all the problems discussed earlier. However a Java class may implement as many Interfaces as it wishes. Since there are now no actual methods or fields inherited, there are no conflicts. An interface can extend another interface (but only 1). All Interface methods are public and abstract. And all the fields are public, static, and final. Interfaces can t be instantiated, but variables of an Interface type can refer to any implementing class. ( someobject instanceof someinterface ) will return true if someobject implements someinterface. New in Java 8 Java 8 now allows Interfaces to define default methods, only available when no other is provided. Overriding a default method makes the default unavailable. ( default not reachable with super. ) In the event of conflicts between multiply inherited default methods, all default methods become null and void. In this case the class is required to provide the method(s), just as if there had not been any defaults to begin with. Java 8 also now allows static methods in Interfaces, callable only through the Interface name, and not inherited. 6

A few common Java Interfaces Throwable - Any class that implements the Throwable Interface can be thrown from a try{ } block and caught in a catch( ) { } block. It is known that anything caught in a catch block must implement the Throwable interface. Runnable - The Java Runnable Interface specifies the run( ) method, required for any class to be run in a thread. Serializable - Implemented by classes whose state can be transmitted in a serial stream of data. 7