public UndergradStudent(String n, String m, String p) { programme = p; super(n, m);

Similar documents
Inheritance CSC9Y4. Inheritance

Inheritance and Polymorphism

Practice for Chapter 11

OOP in Java Review. CS356 Object-Oriented Design and Programming October 1, 2014

OVERRIDING. 7/11/2015 Budditha Hettige 82

Inheritance & Polymorphism

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

CS Programming I: Inheritance

Inheritance (Outsource: )

Chapter 14 Abstract Classes and Interfaces

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

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

First IS-A Relationship: Inheritance

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

Exam Duration: 2hrs and 30min Software Design

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

Inf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics

Object Oriented C# Classes. The OO features of C# are very similar to Java. We ll point the exceptions along the way. 1. A simple C# class.

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

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Example: Count of Points

ITI Introduction to Computing II

Distributed Systems Recitation 1. Tamim Jabban

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

Instance Members and Static Members

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

ITI Introduction to Computing II

7. C++ Class and Object

Java Object Oriented Design. CSC207 Fall 2014

JAVA MOCK TEST JAVA MOCK TEST II

Review for the. Final Exam. Final Exam. December 8, Final Format. Final Format. Ways to get an A on the Final. A Crash Course in Java

Programming II (CS300)

Polymorphism. Arizona State University 1

Lecture 18 CSE11 Fall 2013 Inheritance

Inheritance. Transitivity

STATIC, ABSTRACT, AND INTERFACE

OBJECT ORIENTED PROGRAMMING IN JAVA

Object Oriented Programming. Java-Lecture 11 Polymorphism

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics

Polymorphism: Inheritance Interfaces

Overriding Variables: Shadowing

Object Oriented Programming: Based on slides from Skrien Chapter 2

Programming Exercise 14: Inheritance and Polymorphism

More Relationships Between Classes

CS 61B Discussion 4: Inheritance Fall 2018

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

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

Object-Oriented Programming More Inheritance

Type Hierarchy. Lecture 6: OOP, autumn 2003

Object Orientated Analysis and Design. Benjamin Kenwright

What is Inheritance?

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

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

Exercise: Singleton 1

COMP 110/L Lecture 20. Kyle Dewey

Asking For Help. BIT 115: Introduction To Programming 1

Need to store a list of shapes, each of which could be a circle, rectangle, or triangle

Arrays Classes & Methods, Inheritance

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

QUESTIONS FOR AVERAGE BLOOMERS

UML IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Inheritance, Polymorphism, and Interfaces

Inheritance and Polymorphism. CS180 Fall 2007

CS 251 Intermediate Programming Inheritance

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Darrell Bethea June 6, 2011

POLYTECHNIC OF NAMIBIA SCHOOL OF COMPUTING AND INFORMATICS DEPARTMENT OF COMPUTER SCIENCE

CS111: PROGRAMMING LANGUAGE II

C++ Important Questions with Answers

About 1. Chapter 1: Getting started with oop 2. Remarks 2. Examples 2. Introduction 2. OOP Introduction 2. Intoduction 2. OOP Terminology 3.

PROGRAMMING LANGUAGE 2

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

CS 215 Software Design Sample Midterm Questions

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

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Week 5-1: ADT Design

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

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

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

Inheritance. COMP Week 12

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

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

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

Full file at Chapter 2 - Inheritance and Exception Handling

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Informatik II Tutorial 6. Subho Shankar Basu

Chapter 5. Inheritance

Spring 2019 Discussion 4: February 11, 2019

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

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

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Object Orientated Programming Details COMP360

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction

Reusing Classes. Hendrik Speleers

Q&A. DEMO Version

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Transcription:

Tutorial 3: Inheritance Part A Topic: Inheritance 1. Consider the following class definition. class Student { private String name; private String matric_no; a. Write the definition of an empty class named UndergradStudent which is a subclass of Student class. b. State the reason why UndergradStudent class cannot make direct access to the attributes name and matric_no in Student class, although the attributes are inherited by the UndergradStudent class. c. Make the necessary changes to the two attributes so that they can be accessed by the UndergradStudent class. Topic: Constructor and super() 2. Every constructor defined in a subclass must call one of the constructors of its superclass (True/False). 3. Consider the following class definition. class Student { private String name; private String matric_no; public Student(String n, String m) { name = n; matric_no = m; class UndergradStudent extends Student { private String programme; public UndergradStudent(String n, String m, String p) { programme = p; super(n, m); 1

class PostgradStudent extends Student { private String supervisor; public PostgradStudent(String s) { supervisor = s; a. State the reason why the code inside the constructor of UndergradStudent class will cause a compilation error. b. Make necessary changes to the constructor of UndergradStudent class to solve the compilation error. c. State the reason why the code inside the constructor of PostgradStudent class will cause a compilation error. d. Make necessary changes to the constructor of PostgradStudent class to solve the compilation error. Topic: Method Overriding and super Keyword 4. What does method overriding mean? 5. Write the output of the following Java program. public class ClassA { public void dothis() { System.out.println("doThis in ClassA"); public class ClassB extends ClassA { public void dothis() { System.out.println("doThis in ClassB"); public class ClassC extends ClassA { public class ClassD extends ClassB { public void dothis() { System.out.println("doThis in ClassD"); public class ClassE extends ClassB { public class ClassF extends ClassE { 2

public class Test { ClassC c = new ClassC(); ClassD d = new ClassD(); ClassF f = new ClassF(); c.dothis(); d.dothis(); f.dothis(); 6. Write the output of the execution of the following classes. public class A { public void m1() { System.out.println("m1 in A"); public class B extends A { public void m1() { System.out.println("m1 in B"); super.m1(); public class C extends A { public class Test { B b = new B(); C c = new C(); b.m1(); c.m1(); 3

Topic: Method Overloading vs. Method Overriding 7. Consider the following classes and describe the relationship between the following methods: public class A { protected void m1() { System.out.println("m1 in A"); private void m2() { System.out.println("m2 in A"); public class B extends A { public void m1() { System.out.println("m1 in B"); public void m1(string x) { System.out.println("m1(String) in B"); public void m2() { System.out.println("m2 in B"); a. m1 in B and m1 in A b. m1(string) in B and m1 in A c. m2 in B and m2 in A d. Write the output of the following: public class TestAB { A a1 = new A(); A a2 = new B(); B b = new B(); a1.m1(); a2.m1(); b.m1(); b.m1("astring"); b.m2(); 4

Topic: Polymorphism 8. Pak Ahmad has a small farm, and the following Figure 1 represents the animals (a dog, a chicken and a duck) that he has at his farm. Figure 1 All animals at Pak Ahmad s farm can walk and run, and although the dog also can jump, the chicken and the duck have the advantage because as birds, they also can fly. However, the duck has the advantage over the chicken because the duck also can swim. Consider the following class definitions, and answer the following questions. class Animal { protected String animal_type; public Animal(String t) { animal_type = t; System.out.println(animal_type + " can walk and run"); class Bird extends Animal { public Bird(String t) { super(t); super.move(); System.out.println(animal_type + " also can fly"); a. Write the definition for the Chicken class. 5

b. Name the feature in OOP that allows a subclass to replace the implementation of a method in the super class with different implementation. c. Consider the following definitions for move method in the Dog class and Duck class. Dog class: super.move(); System.out.println(animal_type + " also can jump"); Duck class: super.move(); System.out.println(animal_type + " also can swim"); Write the output of the following code. class Test { Animal[] animals = {new Dog(), new Duck(), new Chicken(); for (int i=0; i<animals.length; i++) animals[i].move(); // Line 1 i. Output at Line 1 when i is 0: ii. Output at Line 1 when i is 1: iii. Output at Line 1 when i is 2: 6

Part B Problem Description We are developing a Course Registration System for a school, and to do this we can define a superclass called Person to store common properties for person such as name and address, and subclasses Student and Teacher for their specific properties. For students, we need to maintain the courses taken and their respective marks; add a course with mark, print all courses taken by students and their average mark. A student takes no more than 5 courses for the entire program. For teachers, we need to maintain the courses taught currently, and able to add or remove a course taught. A teacher teaches not more than 5 courses concurrently. Tasks 1. Draw a UML class diagram to model the classes in the system. 2. Define a suitable class with methods to implement the functionalities for each class. 7