Create a Java project named week9

Size: px
Start display at page:

Download "Create a Java project named week9"

Transcription

1 Objectives of today s lab: Through this lab, students will explore a hierarchical model for object-oriented design and examine the capabilities of the Java language provides for inheritance and polymorphism. Inheritance is a Java (OO) language feature by which a new type can be declared that extends the capabilities of an existing type. is a language feature that allows the same function call to be associated with different definitions during the same program's execution, by delaying the binding of the call to run-time. [More on this in the next lab] Create a Java project named week9 Create a package named basicoop2 in the project week9

2 Exercise 1: Understanding the basic concept of Inheritance When you inherit from an existing class, you can reuse methods and fields of parent class, and further you can add new methods and fields also to your derived class. Task 1.1. Write the following program, save, compile, and run it. /* file name should be TestInheritance.java in the basicoop2 package*/ class Animal{ public void eat(){ System.out.println("Eating!"); class Cat extends Animal{ public void meow (){ System.out.println("Meowing!"); public class TestInheritance{ public static void main(string args[]){ Cat cat = new Cat(); cat.eat(); cat.meow(); Eating! Meowing!

3 Task 1.2. Modify the above program as highlighted below, save, compile, and run it. class Animal{ public Animal(){ System.out.println("Animal Constructor called!"); public void eat(){ System.out.println("Eating!"); class Cat extends Animal{ public Cat(){ System.out.println("Cat Constructor called!"); public void meow (){ System.out.println("Meowing!"); public class TestInheritance{ public static void main(string args[]){ Cat cat = new Cat(); cat.eat(); cat.meow(); Animal Constructor called! Cat Constructor called! Eating! Meowing! Task 1.3: Can you explain what s happening here and why? (Just write as a comment in the code). (Note that, you have not created any object of the Animal class but its constructor has been called)

4 Task 1.4. (Use of super) Modify the above program as highlighted below, save, compile, and run it. class Animal{ int animalage; public Animal(){ System.out.println("Animal Constructor called!"); public Animal(int animalage){ this.animalage = animalage; System.out.println("Animal Constructor called using super!"); public int getanimalage() { return animalage; public void eat(){ System.out.println("Eating!"); class Cat extends Animal{ public Cat(int catage){ super(catage); public Cat(){ System.out.println("Cat Constructor called!"); public void meow (){ System.out.println("Meowing!"); public int getcatage() { return this.animalage; public class TestInheritance{ public static void main(string args[]){ Cat cat = new Cat(2); cat.eat(); cat.meow();

5 System.out.println("Cat's age: "+cat.getcatage()); Animal Constructor called using super! Eating! Meowing! Cat's age: 2 Task 1.5: Now, in your code change the access modifier of int animalage according to the following and observe the corresponding results (a) public int animalage; Save, compile, and run the modified program (b) protected int animalage; Save, compile, and run the modified program (c) private int animalage; Save, compile, and run the modified program Note that, a subclass does not inherit the private members of its base class. However, if the base class has public or protected methods for accessing its private fields, these can also be used by the subclass. Therefore, in this case you can use the following getcatage() implementation to make it work. public int getcatage() { return this.getanimalage();

6 Task 1.6: (Use of super continued) Modify the above program according to the following: In the Cat class: Declare a String data field named color private String color; Modify the Cat(int catage) constructor to Cat(int catage, String color){ //implement the code here, you must use super to call the Animal constructor Write a getcolor() method that return color In the main method, modify the code according to the following: Cat cat = new Cat(2, "Blue"); cat.eat(); cat.meow(); System.out.println("Cat's age: "+cat.getanimalage()); System.out.println("Cat's color: "+cat.getcolor()); Animal Constructor called using super! Eating! Meowing! Cat's age: 2 Cat's color: Blue

7 Task 1.7: (Use of overriding method) Modify the above program according to the following: In the Animal class, change the print statement in the eat() method according to the following: public void eat(){ System.out.println("Animal Eating!"); In the Cat class, implement the same eat() method according to the following: public void eat(){ System.out.println("Cat Eating!"); When you do this, Netbeans editor will suggest you to Annotation, you just accept the suggestion. Now save, compile, and run the modified program. Your main() method remains the same as below: public static void main(string args[]){ Cat cat = new Cat(2, "Blue"); cat.eat(); cat.meow(); System.out.println("Cat's age: "+cat.getanimalage()); System.out.println("Cat's color: "+cat.getcolor());

8 Animal Constructor called using super! Cat Eating! Meowing! Cat's age: 2 Cat's color: Blue Observation: Note that both versions of the eat() method are members of the Cat class. However, in the output you see Cat Eating! (and not the Animal Eating!). Can you explain, why? (Just write as a comment in the code).

9 Exercise 2: Design and Implementation You should work in small groups of 2/3 students Your starting point will be to consider a simple example based on people. Suppose you are developing a software system for UWE. They have already designed, written, coded and tested a class called Person. An object in this class holds personal information about somebody using his name and address. A Person also has methods that can construct a new person from given data, print the information in a standard format, set the name and address and so on. Now UWE also wants a Lecturer class, a Student class, and a GraduateStudent class that are all different kinds of People! A UML class diagram of the Person class and its corresponding Java code implementation are shown below. Note that all the member variable are private and methods are public. -firstname: String -lastname: String -address: String Person +Person() +Person(firstName: String, lastname: String, address: String) +setfirstname(firstname: String): void +getfirstname(): String +setlastname(lastname: String): void +getlastname(): String +setaddress(address: String): void +getaddress(): String +tostring(): String

10 public class Person { private String firstname; private String lastname; private String address; public Person() { public Person(String firstname, String lastname, String address) { this.firstname = firstname; this.lastname = lastname; this.address = address; public String getfirstname() { return firstname; public void setfirstname(string firstname) { this.firstname = firstname; public String getlastname() { return lastname; public void setlastname(string lastname) { this.lastname = lastname; public String getaddress() { return address; public void setaddress(string address) { this.address = public String tostring() { return "Person's name is " + getfirstname() +" "+ getlastname() +"\n Address is "+ getaddress(); public static void main(string args[]) {

11 Person p = new Person(); System.out.println(p); p.setfirstname("abdur"); p.setlastname("rakib"); p.setaddress("uwe Frenchay Campus"); System.out.println(p); Person p1 = new Person("Jun", "Hong", "UWE Frenchay Campus"); System.out.println(p1); Task 2.1: Study the Person class diagram and the corresponding Java code. Write, save, compile, and run the above code. /* file name should be Person.java in the basicoop2 package*/ Person's name is null null Address is null Person's name is Abdur Rakib Address is UWE Frenchay Campus Person's name is Jun Hong Address is UWE Frenchay Campus Observation: carefully look at the tostring() method to understand how it works (if necessary look at the lecture slides or ask your tutor).

12 Task 2.2. Design three classes Lecturer, Student, and GraduateStudent using pen and paper (or you can use MS Word/PowerPoint editor). The classes Lecturer and Student extend Person, and GraduateStudent extends Student. Draw the complete UML class diagram considering all the four classes. Your three newly designed classes contain the following: Lecturer class: One integer data field representing lecturer ID A no-argument constructor that creates a default lecturer A constructor that creates a lecturer with the specified name, address, and the lecturer ID (note that you need to access name and address from the base class) All the appropriate set and get methods A method named tostring() that returns a string description for the lecturer Student class: One integer data field representing student ID A no-argument constructor that creates a default student A constructor that creates a student with the specified name, address, and the student ID (note that you need to access name and address from the base class) All the appropriate set and get methods A method named tostring() that returns a string description for the student GraduateStudent class: One String data field representing supervisor s name (usually, a graduate student works under an assigned supervisor) A no-argument constructor that creates a default graduate student A constructor that creates a graduate student with the specified name, address, the student ID, and the supervisor s name (note that you need to access name, address, and student ID from the base class) All the appropriate set and get methods A method named tostring() that returns a string description for the graduate student

13 Task 2.3: Implement your classes Extend your program by implementing the three new classes. (a) Implement your Lecturer class first, make the class Lecturer extend the class Person by adding extends Person to the right of the class declaration. This is an inheritance relationship, so Lecturer now immediately inherits all the public members and methods of B. When implementing its constructor that takes THREE parameters, use two parameters to call the Person constructor by using the super keyword. Your constructor should look like this public Lecturer(String firstname, String lastname, String address, int lecturerid){ //add code In the //add code section, if you place any code before the super, you should note the resulting compiler error. The problem is that the super class constructor must be called before any other code. Your tutor may explain why this must be so. If you have done this mistake, then move the call to the super class constructor to the start of the method and the error will go away. Copy the remaining lecturerid value to the member variable of the same name, using the "this" keyword. Override the tostring() method to return all the three variables. When implementing this, you must use super to call base class get methods. For example, your code should look like public String tostring() { return "Lecturer's name is " + super.getfirstname() +" "+ super.getlastname() +"\n Address is "+ super.getaddress() +"\n Lecturer ID: " +getlecturerid(); Save, compile, and run your code by creating the following object and printing its information Lecturer lecturer = new Lecturer ("Abdur", "Rakib","UWE Frenchay Campus",1234); System.out.println(lecturer); Lecturer's name is Abdur Rakib Address is UWE Frenchay Campus

14 Lecturer ID: 1234 (b) Similarly, implement other two classes. Save, compile, and run your code by creating the following objects and printing their information Lecturer lecturer = new Lecturer ("Abdur", "Rakib","UWE Frenchay Campus",1234); System.out.println(lecturer); Student student = new Student ("Peter", "Miller","UWE Frenchay Campus",5678); System.out.println(student); GraduateStudent gradstudent = new GraduateStudent ("Dan", "Fielding","UWE Frenchay Campus",1357,"Andy King"); System.out.println(gradstudent); Lecturer's name is Abdur Rakib Address is UWE Frenchay Campus Lecturer ID: 1234 Students's name is Peter Miller Address is UWE Frenchay Campus Student ID: 5678 Students's name is Dan Fielding Address is UWE Frenchay Campus Student ID: 1357 Supervisor s name: Andy King

15 Congratulations! Today, you have learned an important feature of the object-oriented programming languages. Now please help your fellow classmates who have been struggling to complete these exercises OR if you like: (i) (ii) Change the access specifier of the member variables of the Person class and play with them. Extend your program by adding new classes. For example, UnderGradaute, PermanentLecturer, and PartTimeLecturer.

Create a Java project named week10

Create a Java project named week10 Objectives of today s lab: Through this lab, students will examine how casting works in Java and learn about Abstract Class and in Java with examples. Create a Java project named week10 Create a package

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg

More information

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. Inheritance in Java 1. Inheritance 2. Types of Inheritance 3. Why multiple inheritance is not possible in java in case of class? Inheritance in java is a mechanism in which one object acquires all the

More information

PART 1. Eclipse IDE Tutorial. 1. What is Eclipse? Eclipse Java IDE

PART 1. Eclipse IDE Tutorial. 1. What is Eclipse? Eclipse Java IDE PART 1 Eclipse IDE Tutorial Eclipse Java IDE This tutorial describes the usage of Eclipse as a Java IDE. It describes the installation of Eclipse, the creation of Java programs and tips for using Eclipse.

More information

Instance Members and Static Members

Instance Members and Static Members Instance Members and Static Members You may notice that all the members are declared w/o static. These members belong to some specific object. They are called instance members. This implies that these

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management

More information

What is Inheritance?

What is Inheritance? Inheritance 1 Agenda What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain super keyword Overriding methods (most important) Hiding methods Hiding fields Type casting

More information

SSE3052: Embedded Systems Practice

SSE3052: Embedded Systems Practice SSE3052: Embedded Systems Practice Minwoo Ahn minwoo.ahn@csl.skku.edu Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu SSE3052: Embedded Systems Practice, Spring 2018, Jinkyu Jeong

More information

Spring 2019 Discussion 4: February 11, 2019

Spring 2019 Discussion 4: February 11, 2019 CS 61B Inheritance Spring 2019 Discussion 4: February 11, 2019 JUnit Tests 1.1 Think about the lab you did last week where we did JUnit testing. The following code is a few of these JUnit tests from the

More information

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4 CS32 - Week 4 Umut Oztok Jul 15, 2016 Inheritance Process of deriving a new class using another class as a base. Base/Parent/Super Class Derived/Child/Sub Class Inheritance class Animal{ Animal(); ~Animal();

More information

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

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. Inf1-OP Inheritance UML Class Diagrams UML: language for specifying and visualizing OOP software systems UML class diagram: specifies class name, instance variables, methods,... Volker Seeker, adapting

More information

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

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction Class Interaction There are 3 types of class interaction. One

More information

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

Inf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics Inf1-OP Inheritance Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics March 12, 2018 UML Class Diagrams UML: language for specifying and visualizing OOP software

More information

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors 1 CS/ENGRD 2110 FALL 2018 Lecture 5: Local vars; Inside-out rule; constructors http://courses.cs.cornell.edu/cs2110 Announcements 2 A1 is due tomorrow If you are working with a partner: form a group on

More information

Inheritance and Interfaces

Inheritance and Interfaces Inheritance and Interfaces Object Orientated Programming in Java Benjamin Kenwright Outline Review What is Inheritance? Why we need Inheritance? Syntax, Formatting,.. What is an Interface? Today s Practical

More information

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

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner. HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all

More information

More Relationships Between Classes

More Relationships Between Classes More Relationships Between Classes Inheritance: passing down states and behaviors from the parents to their children Interfaces: grouping the methods, which belongs to some classes, as an interface to

More information

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

CSEN401 Computer Programming Lab. Topics: Object Oriented Features: Abstraction and Polymorphism CSEN401 Computer Programming Lab Topics: Object Oriented Features: Abstraction and Polymorphism Prof. Dr. Slim Abdennadher 23.2.2015 c S. Abdennadher 1 Object-Oriented Paradigm: Features Easily remembered

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

Lecture 6 Introduction to Objects and Classes

Lecture 6 Introduction to Objects and Classes Lecture 6 Introduction to Objects and Classes Outline Basic concepts Recap Computer programs Programming languages Programming paradigms Object oriented paradigm-objects and classes in Java Constructors

More information

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

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass

More information

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

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass Encapsulation Again Inheritance Encapsulation and Inheritance Inf1-OOP Inheritance and Interfaces Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics March 9, 2015 The Object

More information

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

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics Inf1-OOP Inheritance and Interfaces Ewan Klein, Perdita Stevens School of Informatics January 12, 2013 Encapsulation Again Inheritance Encapsulation and Inheritance The Object Superclass Flat vs. Nested

More information

First IS-A Relationship: Inheritance

First IS-A Relationship: Inheritance First IS-A Relationship: Inheritance The relationships among Java classes form class hierarchy. We can define new classes by inheriting commonly used states and behaviors from predefined classes. A class

More information

Lab05: Inheritance and polymorphism

Lab05: Inheritance and polymorphism Lab05: Inheritance and polymorphism Inheritance Inheritance creates a new class definition by building upon an existing definition (you extend the original class) The new class can, in turn, can serve

More information

Exam Duration: 2hrs and 30min Software Design

Exam Duration: 2hrs and 30min Software Design Exam Duration: 2hrs and 30min. 433-254 Software Design Section A Multiple Choice (This sample paper has less questions than the exam paper The exam paper will have 25 Multiple Choice questions.) 1. Which

More information

Objectives: Lab Exercise 1 Part 1. Sample Run. Part 2

Objectives: Lab Exercise 1 Part 1. Sample Run. Part 2 Objectives: king Saud University College of Computer &Information Science CSC111 Lab Object I All Sections - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

More information

Object Orientated Programming Details COMP360

Object Orientated Programming Details COMP360 Object Orientated Programming Details COMP360 The ancestor of every action is a thought. Ralph Waldo Emerson Three Pillars of OO Programming Inheritance Encapsulation Polymorphism Inheritance Inheritance

More information

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors 1 CS/ENGRD 2110 SPRING 2017 Lecture 5: Local vars; Inside-out rule; constructors http://courses.cs.cornell.edu/cs2110 Announcements 2 1. Writing tests to check that the code works when the precondition

More information

1.00 Lecture 13. Inheritance

1.00 Lecture 13. Inheritance 1.00 Lecture 13 Inheritance Reading for next time: Big Java: sections 10.5-10.6 Inheritance Inheritance allows you to write new classes based on existing (super or base) classes Inherit super class methods

More information

Inheritance. Transitivity

Inheritance. Transitivity Inheritance Classes can be organized in a hierarchical structure based on the concept of inheritance Inheritance The property that instances of a sub-class can access both data and behavior associated

More information

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

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A. HAS-A Relationship Association is a weak relationship where all objects have their own lifetime and there is no ownership. For example, teacher student; doctor patient. If A uses B, then it is an aggregation,

More information

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

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes. a and Interfaces Class Shape Hierarchy Consider the following class hierarchy Shape Circle Square Problem AND Requirements Suppose that in order to exploit polymorphism, we specify that 2-D objects must

More information

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

More About Objects. Zheng-Liang Lu Java Programming 255 / 282 More About Objects Inheritance: passing down states and behaviors from the parents to their children. Interfaces: requiring objects for the demanding methods which are exposed to the outside world. Polymorphism

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

Classes and Inheritance Extending Classes, Chapter 5.2

Classes and Inheritance Extending Classes, Chapter 5.2 Classes and Inheritance Extending Classes, Chapter 5.2 Dr. Yvon Feaster Inheritance Inheritance defines a relationship among classes. Key words often associated with inheritance are extend and implements.

More information

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

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner. HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all

More information

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1 Java and OOP Part 3 Extending classes OOP in Java : W. Milner 2005 : Slide 1 Inheritance Suppose we want a version of an existing class, which is slightly different from it. We want to avoid starting again

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Objectives To review the concepts and terminology of object-oriented programming To discuss some features of objectoriented design 1-2 Review: Objects In Java and other Object-Oriented

More information

OVERRIDING. 7/11/2015 Budditha Hettige 82

OVERRIDING. 7/11/2015 Budditha Hettige 82 OVERRIDING 7/11/2015 (budditha@yahoo.com) 82 What is Overriding Is a language feature Allows a subclass or child class to provide a specific implementation of a method that is already provided by one of

More information

Example: Count of Points

Example: Count of Points Example: Count of Points 1 class Point { 2... 3 private static int numofpoints = 0; 4 5 Point() { 6 numofpoints++; 7 } 8 9 Point(int x, int y) { 10 this(); // calling the constructor with no input argument;

More information

Software and Programming 1

Software and Programming 1 Software and Programming 1 Week 9 Lab - Use of Classes and Inheritance 8th March 2018 SP1-Lab9-2018.ppt Tobi Brodie (Tobi@dcs.bbk.ac.uk) 1 Lab 9: Objectives Exercise 1 Student & StudentTest classes 1.

More information

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

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information

More information

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

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor CSE 143 Object & Class Relationships Inheritance Reading: Ch. 9, 14 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches dog Dog

More information

CIS 110: Introduction to Computer Programming

CIS 110: Introduction to Computer Programming CIS 110: Introduction to Computer Programming Lecture 22 and 23 Objects, objects, objects ( 8.1-8.4) 11/28/2011 CIS 110 (11fa) - University of Pennsylvania 1 Outline Object-oriented programming. What is

More information

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth Outline CIS 110: Introduction to Computer Programming Lecture 22 and 23 Objects, objects, objects ( 8.1-8.4) Object-oriented programming. What is an object? Classes as blueprints for objects. Encapsulation

More information

Objectives. Inheritance. Inheritance is an ability to derive a new class from an existing class. Creating Subclasses from Superclasses

Objectives. Inheritance. Inheritance is an ability to derive a new class from an existing class. Creating Subclasses from Superclasses Objectives Inheritance Students should: Understand the concept and role of inheritance. Be able to design appropriate class inheritance hierarchies. Be able to make use of inheritance to create new Java

More information

Chapter 5. Inheritance

Chapter 5. Inheritance Chapter 5 Inheritance Objectives Know the difference between Inheritance and aggregation Understand how inheritance is done in Java Learn polymorphism through Method Overriding Learn the keywords : super

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

Week 7 Inheritance. Written by Alexandros Evangelidis. Adapted from Joseph Gardiner. 10 November 2015

Week 7 Inheritance. Written by Alexandros Evangelidis. Adapted from Joseph Gardiner. 10 November 2015 Week 7 Inheritance Written by Alexandros Evangelidis. Adapted from Joseph Gardiner 10 November 2015 1 This Week By the end of the tutorial, we should have covered: Inheritance What is inheritance? How

More information

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

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs. Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar

More information

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces Prof. Hwansoo Han T.A. Minseop Jeong T.A. Wonseok Choi 1 In Previous Lecture We

More information

Class Relationships. Lecture 18. Based on Slides of Dr. Norazah Yusof

Class Relationships. Lecture 18. Based on Slides of Dr. Norazah Yusof Class Relationships Lecture 18 Based on Slides of Dr. Norazah Yusof 1 Relationships among Classes Association Aggregation Composition Inheritance 2 Association Association represents a general binary relationship

More information

INTERFACE WHY INTERFACE

INTERFACE WHY INTERFACE INTERFACE WHY INTERFACE Interfaces allow functionality to be shared between objects that agree to a contract on how the software should interact as a unit without needing knowledge of how the objects accomplish

More information

Inheritance Introduction. 9.1 Introduction 361

Inheritance Introduction. 9.1 Introduction 361 www.thestudycampus.com Inheritance 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected Members 9.4 Relationship Between Superclasses and Subclasses 9.4.1 Creating and Using a CommissionEmployee

More information

CS1150 Principles of Computer Science Objects and Classes

CS1150 Principles of Computer Science Objects and Classes CS1150 Principles of Computer Science Objects and Classes Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Object-Oriented Thinking Chapters 1-8

More information

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

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008 Overview Lecture 7: Inheritance and GUIs Written by: Daniel Dalevi Inheritance Subclasses and superclasses Java keywords Interfaces and inheritance The JComponent class Casting The cosmic superclass Object

More information

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

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding Java Class Design Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding eugeny.berkunsky@gmail.com http://www.berkut.mk.ua Objectives Implement encapsulation Implement inheritance

More information

WEEK 13 EXAMPLES: POLYMORPHISM

WEEK 13 EXAMPLES: POLYMORPHISM WEEK 13 EXAMPLES: POLYMORPHISM CASE STUDY: PAYROLL SYSTEM USING POLYMORPHISM Use the principles of inheritance, abstract class, abstract method, and polymorphism to design a payroll project for a car lot.

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Objectives To review the concepts and terminology of object-oriented programming To discuss some features of objectoriented design 1-2 Review: Objects In Java and other Object-Oriented

More information

Informatik II (D-ITET) Tutorial 6

Informatik II (D-ITET) Tutorial 6 Informatik II (D-ITET) Tutorial 6 TA: Marian George, E-mail: marian.george@inf.ethz.ch Distributed Systems Group, ETH Zürich Exercise Sheet 5: Solutions and Remarks Variables & Methods beginwithlowercase,

More information

Week 11: Class Design

Week 11: Class Design Week 11: Class Design 1 Most classes are meant to be used more than once This means that you have to think about what will be helpful for future programmers There are a number of trade-offs to consider

More information

Abstract class & Interface

Abstract class & Interface Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2124) Lab 3 Abstract class & Interface Eng. Mohammed Abdualal Abstract class 1. An abstract

More information

Inheritance (Part 2) Notes Chapter 6

Inheritance (Part 2) Notes Chapter 6 Inheritance (Part 2) Notes Chapter 6 1 Object Dog extends Object Dog PureBreed extends Dog PureBreed Mix BloodHound Komondor... Komondor extends PureBreed 2 Implementing Inheritance suppose you want to

More information

Answer ALL Questions. Each Question carries ONE Mark.

Answer ALL Questions. Each Question carries ONE Mark. SECTION A (10 MARKS) Answer ALL Questions. Each Question carries ONE Mark. 1 (a) Choose the correct answer: (5 Marks) i. Which of the following is not a valid primitive type : a. char b. double c. int

More information

Week 5-1: ADT Design

Week 5-1: ADT Design Week 5-1: ADT Design Part1. ADT Design Define as class. Every obejects are allocated in heap space. Encapsulation : Data representation + Operation Information Hiding : Object's representation part hides,

More information

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda

More information

Accelerating Information Technology Innovation

Accelerating Information Technology Innovation Accelerating Information Technology Innovation http://aiti.mit.edu Cali, Colombia Summer 2012 Lesson 09 Inheritance What is Inheritance? In the real world: We have general terms for objects in the real

More information

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

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence. Constructor in Java 1. What are CONSTRUCTORs? Constructor in java is a special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation. It constructs

More information

CSC207 Week 3. Larry Zhang

CSC207 Week 3. Larry Zhang CSC207 Week 3 Larry Zhang 1 Announcements Readings will be posted before the lecture Lab 1 marks available in your repo 1 point for creating the correct project. 1 point for creating the correct classes.

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 8(a): Abstract Classes Lecture Contents 2 Abstract base classes Concrete classes Dr. Amal Khalifa, 2014 Abstract Classes and Methods

More information

QUESTIONS FOR AVERAGE BLOOMERS

QUESTIONS FOR AVERAGE BLOOMERS MANTHLY TEST JULY 2017 QUESTIONS FOR AVERAGE BLOOMERS 1. How many types of polymorphism? Ans- 1.Static Polymorphism (compile time polymorphism/ Method overloading) 2.Dynamic Polymorphism (run time polymorphism/

More information

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

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: has a CSE143 Sp Student. CSE 143 Java Object & Class Relationships Inheritance Reading: Ch. 9, 14 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches

More information

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

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: has a CSC Employee. Supervisor CSC 143 Object & Class Relationships Inheritance Reading: Ch. 10, 11 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches dog

More information

Arrays Classes & Methods, Inheritance

Arrays Classes & Methods, Inheritance Course Name: Advanced Java Lecture 4 Topics to be covered Arrays Classes & Methods, Inheritance INTRODUCTION TO ARRAYS The following variable declarations each allocate enough storage to hold one value

More information

Java Magistère BFA

Java Magistère BFA Java 101 - Magistère BFA Lesson 3: Object Oriented Programming in Java Stéphane Airiau Université Paris-Dauphine Lesson 3: Object Oriented Programming in Java (Stéphane Airiau) Java 1 Goal : Thou Shalt

More information

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

(800) Toll Free (804) Fax   Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days Course Description This course introduces the Java programming language and how to develop Java applications using Eclipse 3.0. Students learn the syntax of the Java programming language, object-oriented

More information

Programming Exercise 14: Inheritance and Polymorphism

Programming Exercise 14: Inheritance and Polymorphism Programming Exercise 14: Inheritance and Polymorphism Purpose: Gain experience in extending a base class and overriding some of its methods. Background readings from textbook: Liang, Sections 11.1-11.5.

More information

EECS 1001 and EECS 1030M, lab 01 conflict

EECS 1001 and EECS 1030M, lab 01 conflict EECS 1001 and EECS 1030M, lab 01 conflict Those students who are taking EECS 1001 and who are enrolled in lab 01 of EECS 1030M should switch to lab 02. If you need my help with switching lab sections,

More information

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS121/IS223. Object Reference Variables. Dr Olly Gotel CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223

More information

CLASS DESIGN. Objectives MODULE 4

CLASS DESIGN. Objectives MODULE 4 MODULE 4 CLASS DESIGN Objectives > After completing this lesson, you should be able to do the following: Use access levels: private, protected, default, and public. Override methods Overload constructors

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism Recitation 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University Project 5 Due Wed, Oct. 22 at 10 pm. All questions on the class newsgroup. Make use of lab

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management

More information

Programming overview

Programming overview Programming overview Basic Java A Java program consists of: One or more classes A class contains one or more methods A method contains program statements Each class in a separate file MyClass defined in

More information

Chapter 12: Inheritance

Chapter 12: Inheritance Chapter 12: Inheritance Objectives Students should Understand the concept and role of inheritance. Be able to design appropriate class inheritance hierarchies. Be able to make use of inheritance to create

More information

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

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4 Structural Programming and Data Structures Winter 2000 CMPUT 102: Inheritance Dr. Osmar R. Zaïane Course Content Introduction Objects Methods Tracing Programs Object State Sharing resources Selection Repetition

More information

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

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance Structural Programming and Data Structures Winter 2000 CMPUT 102: Dr. Osmar R. Zaïane Course Content Introduction Objects Methods Tracing Programs Object State Sharing resources Selection Repetition Vectors

More information

CS 201, Fall 2016 Sep 28th Exam 1

CS 201, Fall 2016 Sep 28th Exam 1 CS 201, Fall 2016 Sep 28th Exam 1 Name: Question 1. [5 points] Write code to prompt the user to enter her age, and then based on the age entered, print one of the following messages. If the age is greater

More information

CS 112 Introduction to Programming. (Spring 2012)

CS 112 Introduction to Programming. (Spring 2012) CS 112 Introduction to Programming (Spring 2012) Lecture #32: Inheritance and Class Hierarchy Zhong Shao Department of Computer Science Yale University Office: 314 Watson http://flint.cs.yale.edu/cs112

More information

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

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5, Informatik II Tutorial 6 Mihai Bâce mihai.bace@inf.ethz.ch 05.04.2017 Mihai Bâce April 5, 2017 1 Overview Debriefing Exercise 5 Briefing Exercise 6 Mihai Bâce April 5, 2017 2 U05 Some Hints Variables &

More information

Class, Variable, Constructor, Object, Method Questions

Class, Variable, Constructor, Object, Method Questions Class, Variable, Constructor, Object, Method Questions http://www.wideskills.com/java-interview-questions/java-classes-andobjects-interview-questions https://www.careerride.com/java-objects-classes-methods.aspx

More information

Example: Count of Points

Example: Count of Points Example: Count of Points 1 public class Point { 2... 3 private static int numofpoints = 0; 4 5 public Point() { 6 numofpoints++; 7 } 8 9 public Point(int x, int y) { 10 this(); // calling Line 5 11 this.x

More information

Fundamentals of Object Oriented Programming

Fundamentals of Object Oriented Programming INDIAN INSTITUTE OF TECHNOLOGY ROORKEE Fundamentals of Object Oriented Programming CSN- 103 Dr. R. Balasubramanian Associate Professor Department of Computer Science and Engineering Indian Institute of

More information

Exercise: Singleton 1

Exercise: Singleton 1 Exercise: Singleton 1 In some situations, you may create the only instance of the class. 1 class mysingleton { 2 3 // Will be ready as soon as the class is loaded. 4 private static mysingleton Instance

More information

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

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. OOPs Concepts 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. Type Casting Let us discuss them in detail: 1. Data Hiding: Every

More information

INHERITANCE Mrs. K.M. Sanghavi

INHERITANCE Mrs. K.M. Sanghavi INHERITANCE Mrs. K.M. Sanghavi Inheritance in Java Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind inheritance in java

More information

Lecture 18 CSE11 Fall 2013 Inheritance

Lecture 18 CSE11 Fall 2013 Inheritance Lecture 18 CSE11 Fall 2013 Inheritance What is Inheritance? Inheritance allows a software developer to derive a new class from an existing one write code once, use many times (code reuse) Specialization

More information

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Inheritance Three main programming mechanisms that constitute object-oriented programming (OOP) Encapsulation

More information

Java Fundamentals (II)

Java Fundamentals (II) Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class

More information