Tutorial 7. If it compiles, what does it print? Inheritance 1. Inheritance 2

Similar documents
Midterm Exam 5 April 20, 2015

Inheritance -- Introduction

Chapter 5 Object-Oriented Programming

Inheritance and Polymorphism

CS Week 15 Page 1

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

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

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

JAVA. Lab-9 : Inheritance

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

Practice for Chapter 11

PROGRAMMING LANGUAGE 2

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

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

Questions Answer Key Questions Answer Key Questions Answer Key

1 Shyam sir JAVA Notes

Method Resolution Approaches. Dynamic Dispatch

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

Arrays Classes & Methods, Inheritance

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

Questions Answer Key Questions Answer Key Questions Answer Key

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

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

Chapter 14 Abstract Classes and Interfaces

Java: introduction to object-oriented features

Java Fundamentals (II)

Programming II (CS300)

CSCE3193: Programming Paradigms

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

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

CS260 Intro to Java & Android 03.Java Language Basics

Lesson 10. Work with Interfaces and Abstract Classes. Copyright all rights reserved

CO Java SE 8: Fundamentals

Class, Variable, Constructor, Object, Method Questions

Tutorial 8 Date: 15/04/2014

What is Inheritance?

ECE 122. Engineering Problem Solving with Java

CS-202 Introduction to Object Oriented Programming

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

C++ Important Questions with Answers

Chapter 5. Inheritance

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

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

CS Programming I: Inheritance

Spring 2019 Discussion 4: February 11, 2019

CMSC 132: Object-Oriented Programming II

CS105 C++ Lecture 7. More on Classes, Inheritance

Java Object Oriented Design. CSC207 Fall 2014

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

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

Inheritance (Part 2) Notes Chapter 6

Basics of Object Oriented Programming. Visit for more.

Chapter 6 Reflection

Questions Answer Key Questions Answer Key Questions Answer Key

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object-Oriented Programming More Inheritance

Instance Members and Static Members

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

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Object Orientated Programming Details COMP360

OVERRIDING. 7/11/2015 Budditha Hettige 82

Object Oriented Programming. Java-Lecture 11 Polymorphism

Chapter 10: Inheritance

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

Super-Classes and sub-classes

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

The Essence of OOP using Java, Nested Top-Level Classes. Preface

Full file at Chapter 2 - Inheritance and Exception Handling

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

CS 251 Intermediate Programming Inheritance

More about inheritance

C++ Inheritance and Encapsulation

First IS-A Relationship: Inheritance

Inheritance and Polymorphism. CS180 Fall 2007

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Lecture 2: Java & Javadoc

Object-Oriented Programming (OOP) Fundamental Principles of OOP

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

Rules and syntax for inheritance. The boring stuff

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

Java Magistère BFA

Inheritance and object compatibility

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

VIRTUAL FUNCTIONS Chapter 10

Type Hierarchy. Lecture 6: OOP, autumn 2003

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

Programming overview

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

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

Inheritance, Polymorphism, and Interfaces

15CS45 : OBJECT ORIENTED CONCEPTS

C++ Inheritance and Encapsulation

Chapter 11: Inheritance

Introductory Programming Inheritance, sections

COMP 249: Object Oriented Programming II. Tutorial 2: Intro to Inheritance

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Transcription:

Tutorial 7 If it compiles, what does it print? Here are some exercises to practice inheritance concepts, specifically, overriding, overloading, abstract classes, constructor chaining and polymorphism. Inheritance 1 class Vehicle { Bike b = new Bike(); Inheritance 2 class Vehicle { System.out.println("rollroll"); System.out.println("pedalpedal");

Bike b = new Bike(); Inheritance 3 class Vehicle { super.drive(); System.out.println("pedalpedal"); Bike b = new Bike(); Inheritance 4 System.out.println("rollroll"); System.out.println("pedalpedal");

Vehicle c = new Vehicle(); Inheritance 5 public abstract void drive(); Bike b = new Bike(); Inheritance 6 System.out.println("rollroll"); System.out.println("pedalpedal"); Vehicle c = new Car();

Inheritance 7 private void drive() { System.out.println("rollroll"); System.out.println("pedalpedal"); Inheritance 8 public void drive(int count) { System.out.println("rollroll " + count); public void drive(int count) { System.out.println("pedalpedal " + count); c.drive(10);

b.drive(5); Overloading 1 class Addition{ public int add(int a, int b){ int sum = a+b; return sum; public int add(int a, int b, int c){ int sum = a+b+c; return sum; public double add(double a, double b, double c){ double sum = a+b+c; return sum; public static void main (String[] args) { Addition ob = new Addition(); int sum1 = ob.add(1,2); System.out.println(sum1); int sum2 = ob.add(1,2,3); System.out.println(sum2); double sum3 = ob.add(1.0,2.0,3.0); System.out.println(sum3); Overloading 2 class Birthday { public void greet(string name, int age){ System.out.println("Happy " + age + ". birthday, " + name + "!"); public void greet(int age, String name){ System.out.println("All the best for your " + age + ". birthday, " + name + "!"); public static void main (String[] args) { Birthday b = new Birthday(); b.greet("jack", 5);

b.greet(7, "Jill"); Overloading 3 class Addition{ public int add(int a, int b){ int sum = a+b; return sum; public double add(int a, int b){ int sum = a+b; return sum; public static void main (String[] args) { Addition ob = new Addition(); int sum1 = ob.add(1,2); System.out.println(sum1); double sum2 = ob.add(1,2); System.out.println(sum2); Inheritance 9 private String noise; public Vehicle() { noise = "drive"; Vehicle c = new Car();

Inheritance 10 private String noise; public Vehicle() { noise = "drive"; System.out.println(noise); System.out.println("pedal" + noise); Vehicle c = new Car(); Inheritance 11 protected String noise; public Vehicle() { noise = "drive"; System.out.println(noise);

System.out.println("pedal" + noise); Vehicle c = new Car(); Inheritance 12 protected String noise; public Vehicle(String noise) { this.noise = noise; System.out.println(noise); System.out.println("pedal" + noise); Vehicle c = new Car("roll"); Inheritance 13

protected String noise; public Vehicle(String noise) { this.noise = noise public Car(String noise) { this.noise = noise; System.out.println(noise); public Bike(String noise) { this.noise = noise; System.out.println(noise + noise + noise); Vehicle c = new Car("roll"); Vehicle b = new Bike("pedal"); Inheritance 14 protected String noise; public Vehicle(String noise) { this.noise = noise;

public Car(String noise) { super(noise); System.out.println(noise); public Bike(String noise) { super(noise); this.noise = "zoom"; System.out.println(noise + noise + noise); Vehicle c = new Car("roll"); Vehicle b = new Bike("pedal"); Solutions Inheritance 1. Compiles and prints "drivedrive" twice because Bike and Car inherit drive from Vehicle 2. Compiles and prints "rollroll" and "pedalpedal" because Bike and Car override drive 3. Compiles and prints "drivedrive" and "pedalpedal" because Car's drive method calls Vehicle's drive method. 4. Does not compile because abstract classes cannot be instantiated. 5. Does not compile because abstract methods must be implemented / overridden in the subclass. 6. Compiles and prints "rollroll" and "pedalpedal" because polymorphism allows subclass instances to be referenced by supertype variables. 7. Does not compile because drive is called on a variable of type Vehicle which has no public access for its method drive. Even though it is overridden by the created instance Bike, the API visible at runtime is the one from the variables type. The execution of c.drive() compiles and would print "rollroll". Access modifiers in overridden methods need to be at least as visible as in their superclass but can have higher visibility in a subclass.

8. Does not compile because the method drive is not overridden but overloaded due to the change in the parameter list. This is allowed and the classes Vehicle, Car and Bike compile just fine. Also, the call c.drive(10) in the Main class is fine. However, the call b.drive(5) does not work because b is of type Vehicle. Vehicle does not have access to a method that was overloaded in one of its subclasses even if it is referencing the Bike subclass under the hood. 9. Prints "drive" twice because the super classes constructor is called after the base classes constructor, drive is inherited by Car and Bike and the inherited version has access to the private member of Vehicle. 10. Does not compile because subclasses don't have access to private members of superclass. 11. Compiles because access to protected members is allowed in subclasses. It prints "drive" and "pedaldrive". 12. Does not compile because subclass Car has no one argument constructor. Constructors are not inherited! 13. Does not compile because subclasses don't call the superclasses' one argument constructor. The constructor chain must be maintained throughout the class hierarchy. 14. Compiles and prints "roll" and "zoomzoomzoom" because the superclass constructors are executed before the subclass constructors, hence the super() call needs to be the very first line in a subclass constructor. Overloading 1. Compiles and prints 3, 6 and 6.00000 because add is overloaded once by using more parameters and once by using different parameter types. 2. Compiles and prints "Happy 5. birthday, Jack!" and "All the best for your 7. birthday, Jill!" because greet is overloaded by swapping parameter types around. 3. Does not compile because only changing the return type is not enough for overloading.