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

Size: px
Start display at page:

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

Transcription

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

2 Inheritance Suppose we want a version of an existing class, which is slightly different from it. We want to avoid starting again from scratch We can define the new class to be a sub-class of the first class. OOP in Java : W. Milner 2005 : Slide 2

3 Terms used The original class is called the base class, the ancestor class or the super class The process of designing the sub-class from the base class is called 'sub-classing' the base class OOP in Java : W. Milner 2005 : Slide 3

4 Sub-classing The subclass inherits all members and methods of the first where needed we can write new versions of inherited methods which replace the old method we can add extra members and methods You can t loose a member or method No limit to levels of inheritance OOP in Java : W. Milner 2005 : Slide 4

5 Example Common type of sub-classing is specialization Suppose we want a type of product which is perishable When we deliver new stock, we throw away old stock not add to it First review the Product class: OOP in Java : W. Milner 2005 : Slide 5

6 public class Product public Product() lastbarcodeused++; barcode=lastbarcodeused; stocklevel=100; public Product(int initstock) lastbarcodeused++; barcode=lastbarcodeused; stocklevel=initstock; Product class definition public static int count() return lastbarcodeused; public void display() System.out.println("Barcode = "+barcode); System.out.println("Stocklevel = "+stocklevel); System.out.println("========================="); OOP in Java : W. Milner 2005 : Slide 6

7 Rest of Product public int getstocklevel() return stocklevel; public void deliver(int howmany) if (howmany<0) System.out.println("Invalid delivery"); return; else stocklevel+=howmany; private static int lastbarcodeused=0; private int barcode; protected int stocklevel; OOP in Java : W. Milner 2005 : Slide 7

8 Implementation of Perishable public class Perishable extends Product public void deliver(int howmany) stocklevel=howmany; OOP in Java : W. Milner 2005 : Slide 8

9 Protected Problem the deliver method in Perishable references the private field stocklevel not allowed Solution use access control modifier protected Excerpt from modified Product definition.. private static int lastbarcodeused=0; private int barcode; protected int stocklevel; OOP in Java : W. Milner 2005 : Slide 9

10 public class First public static void main(string[] args) Product prod = new Product(); prod.deliver(100); Perishable perish1 = new Perishable(); Perishable perish2 = new Perishable(); perish1.deliver(50); perish2.deliver(60); prod.display(); perish1.display(); perish2.display(); Using the subclass All 3 use default constructor =stocklevel 100 Barcode = 1 Stocklevel = 200 ==================== Barcode = 2 Stocklevel = 50 ==================== Barcode = 3 Stocklevel = 60 ==================== OOP in Java : W. Milner 2005 : Slide 10

11 Constructors of sub-classes Constructors are not methods They are not inherited If you don't define one the no-arg constructor of the base class is called for you see last example OOP in Java : W. Milner 2005 : Slide 11

12 super() super(); can be used as the first statement in a constructor It means the corresponding superclass constructor is called Further statements can take further action For example..suppose Perishable products have an extra store location code.. OOP in Java : W. Milner 2005 : Slide 12

13 Using super() public Perishable(int initstock, int initlocationcode) super(initstock); locationcode = initlocationcode; public Product(int initstocklevel) barcode=lastbarcodeused++; stocklevel=initstocklevel; OOP in Java : W. Milner 2005 : Slide 13

14 More on super() super() cannot be anywhere except the first line of a constructor If you don t use super(), the system executes it anyway IOW a subclass constructor first executes the no-arg constructor of the super class OOP in Java : W. Milner 2005 : Slide 14

15 Exercise Define an Employee class, with fields payrollnumber and rateofpay Define a Manager class as a sub-class of Employee. They are paid monthly define their pay() method to display their pay Define a Clerical class as a sub-class of Employee. They are hourly paid. Add an hoursworked field, and a pay() method. OOP in Java : W. Milner 2005 : Slide 15

16 Object All classes descend from the class Object public class MyClass.. Is in effect: public class MyClass extends Object.. While if you say public class MyClass extends MySuperClass Then MySuperClass, or its ancestor, descends from Object Object objects have few useful methods Except tostring(), which converts the object to a descriptive string Which is what System.out.println calls For example.. OOP in Java : W. Milner 2005 : Slide 16

17 Object example Object someobject= new Object(); System.out.println(someObject); Output: OOP in Java : W. Milner 2005 : Slide 17

18 Changing and using tostring In Perishable definition.... public String tostring() return "Perishable ID="+barcode+" Loc="+locationCode+" stock="+stocklevel;.. in use.. Perishable p1 = new Perishable(20,30); Perishable p2 = new Perishable(20,45); System.out.println(p1); System.out.println(p2); Output: Perishable ID=0 Loc=30 stock=20 Perishable ID=1 Loc=45 stock=20 calls tostring() of Perishable objects OOP in Java : W. Milner 2005 : Slide 18

19 final methods A method declared as final in a superclass cannot be altered in a subclass For example.. OOP in Java : W. Milner 2005 : Slide 19

20 Defining a method as final In Product.. public final void display() System.out.println("Barcode = "+barcode); System.out.println("Stocklevel = "+stocklevel); System.out.println("========================="); In Perishable.. public void display().... In use: Perishable p1 = new Perishable(20,30); Output on next slide OOP in Java : W. Milner 2005 : Slide 20

21 Trying to override final - compile time error C:\Walter\JavaProgs\Perishable.java:19: display() in Perishable cannot override display() in Product; overridden method is final public void display() ^ 1 error OOP in Java : W. Milner 2005 : Slide 21

22 Final classes and why A class declared as final cannot be subclassed Methods and classes are usually declared as final for security Otherwise a subclass of a standard superclass might be defined, with.. Unpleasant overridden methods But at run-time a subclass object would look like the superclass Eg the String class is final for this reason OOP in Java : W. Milner 2005 : Slide 22

23 Abstract classes Superclasses which are 'general' can be declared abstract Used when subclasses will all implement the same method in different ways, but The superclass is too general to actually implement the method itself For example.. OOP in Java : W. Milner 2005 : Slide 23

24 Example abstract class Suppose we had a superclass called Shape And subclasses called Triangle, Rectangle, Square and so on. Each would need a draw method But we could not program the draw method of a Shape instance So the draw method of Shape is declared abstract As is the class as a whole This means Shape cannot be instantiated OOP in Java : W. Milner 2005 : Slide 24

25 Example abstract class public abstract class Shape public Shape(int initheight, int initwidth) width=initwidth; height=initheight; public abstract void draw(); protected int width; protected int height; OOP in Java : W. Milner 2005 : Slide 25

26 public class Rectangle extends Shape public Rectangle(int h, int w) super(h,w); Subclass of abstract class public void draw() for (int i=0; i<height; i++) for (int j=0; j<width; j++) System.out.print("*"); System.out.println(); OOP in Java : W. Milner 2005 : Slide 26

27 Using the subclass Rectangle r = new Rectangle(4,5); r.draw(); OOP in Java : W. Milner 2005 : Slide 27

28 Copy the Shape class Exercise Define a Triangle class by subclassing Shape define the draw method How would you deal with a Square class? OOP in Java : W. Milner 2005 : Slide 28

Java and OOP. Part 2 Classes and objects

Java and OOP. Part 2 Classes and objects Java and OOP Part 2 Classes and objects 1 Objects OOP programs make and use objects An object has data members (fields) An object has methods The program can tell an object to execute some of its methods

More information

Java Programming Lecture 7

Java Programming Lecture 7 Java Programming Lecture 7 Alice E. Fischer Feb 16, 2015 Java Programming - L7... 1/16 Class Derivation Interfaces Examples Java Programming - L7... 2/16 Purpose of Derivation Class derivation is used

More information

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario The Story So Far... Classes as collections of fields and methods. Methods can access fields, and

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

INHERITANCE. Spring 2019

INHERITANCE. Spring 2019 INHERITANCE Spring 2019 INHERITANCE BASICS Inheritance is a technique that allows one class to be derived from another A derived class inherits all of the data and methods from the original class Suppose

More information

Object-Oriented Concepts

Object-Oriented Concepts JAC444 - Lecture 3 Object-Oriented Concepts Segment 2 Inheritance 1 Classes Segment 2 Inheritance In this segment you will be learning about: Inheritance Overriding Final Methods and Classes Implementing

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. Lecture 11 COP 3252 Summer May 25, 2017

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017 Inheritance Lecture 11 COP 3252 Summer 2017 May 25, 2017 Subclasses and Superclasses Inheritance is a technique that allows one class to be derived from another. A derived class inherits all of the data

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

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1 Inheritance & Polymorphism Recap Inheritance & Polymorphism 1 Introduction! Besides composition, another form of reuse is inheritance.! With inheritance, an object can inherit behavior from another object,

More information

Full file at Chapter 2 - Inheritance and Exception Handling

Full file at   Chapter 2 - Inheritance and Exception Handling Chapter 2 - Inheritance and Exception Handling TRUE/FALSE 1. The superclass inherits all its properties from the subclass. ANS: F PTS: 1 REF: 76 2. Private members of a superclass can be accessed by a

More information

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

The software crisis. code reuse: The practice of writing program code once and using it in many contexts. Inheritance The software crisis software engineering: The practice of conceptualizing, designing, developing, documenting, and testing largescale computer programs. Large-scale projects face many issues:

More information

Advanced Placement Computer Science. Inheritance and Polymorphism

Advanced Placement Computer Science. Inheritance and Polymorphism Advanced Placement Computer Science Inheritance and Polymorphism What s past is prologue. Don t write it twice write it once and reuse it. Mike Scott The University of Texas at Austin Inheritance, Polymorphism,

More information

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

The software crisis. code reuse: The practice of writing program code once and using it in many contexts. Inheritance The software crisis software engineering: The practice of conceptualizing, designing, developing, documenting, and testing largescale computer programs. Large-scale projects face many issues:

More information

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Big software. code reuse: The practice of writing program code once and using it in many contexts. Inheritance Big software software engineering: The practice of conceptualizing, designing, developing, documenting, and testing largescale computer programs. Large-scale projects face many issues: getting

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

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

CMSC 132: Object-Oriented Programming II. Inheritance

CMSC 132: Object-Oriented Programming II. Inheritance CMSC 132: Object-Oriented Programming II Inheritance 1 Mustang vs Model T Ford Mustang Ford Model T 2 Interior: Mustang vs Model T 3 Frame: Mustang vs Model T Mustang Model T 4 Compaq: old and new Price:

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

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions Name CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions 1. a. (25 points) A rational number is a number that can be represented by a pair of integers a numerator and a denominator.

More information

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance Admin CS 112 Introduction to Programming q Class project Inheritance Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu 2 Recap: OOP Analysis

More information

More on Objects in JAVA TM

More on Objects in JAVA TM More on Objects in JAVA TM Inheritance : Definition: A subclass is a class that extends another class. A subclass inherits state and behavior from all of its ancestors. The term superclass refers to a

More information

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

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance? CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 5: Inheritance & Polymorphism Lecture Contents 2 What is Inheritance? Super-class & sub class Protected members Creating subclasses

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

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

IST311. Advanced Issues in OOP: Inheritance and Polymorphism IST311 Advanced Issues in OOP: Inheritance and Polymorphism IST311/602 Cleveland State University Prof. Victor Matos Adapted from: Introduction to Java Programming: Comprehensive Version, Eighth Edition

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 5: Inheritance & Polymorphism Lecture Contents 2 What is Inheritance? Super-class & sub class Protected members Creating subclasses

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

BBM 102 Introduction to Programming II Spring Abstract Classes and Interfaces

BBM 102 Introduction to Programming II Spring Abstract Classes and Interfaces BBM 102 Introduction to Programming II Spring 2017 Abstract Classes and Interfaces 1 Today Abstract Classes Abstract methods Polymorphism with abstract classes Example project: Payroll System Interfaces

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 8(b): Abstract classes & Polymorphism Lecture Contents 2 Abstract base classes Concrete classes Polymorphic processing Dr. Amal Khalifa,

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

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

Inheritance Motivation

Inheritance Motivation Inheritance Inheritance Motivation Inheritance in Java is achieved through extending classes Inheritance enables: Code re-use Grouping similar code Flexibility to customize Inheritance Concepts Many real-life

More information

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8 Inheritance Notes Chapter 6 and AJ Chapters 7 and 8 1 Inheritance you know a lot about an object by knowing its class for example what is a Komondor? http://en.wikipedia.org/wiki/file:komondor_delvin.jpg

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

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

Polymorphism CSCI 201 Principles of Software Development

Polymorphism CSCI 201 Principles of Software Development Polymorphism CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D. jeffrey.miller@usc.edu Program Outline USC CSCI 201L Polymorphism Based on the inheritance hierarchy, an object with a compile-time

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

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

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed CREATED BY: Muhammad Bilal Arslan Ahmad Shaad JAVA Chapter No 5 Instructor: Muhammad Naveed Muhammad Bilal Arslan Ahmad Shaad Chapter No 5 Object Oriented Programming Q: Explain subclass and inheritance?

More information

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

The Essence of OOP using Java, Nested Top-Level Classes. Preface The Essence of OOP using Java, Nested Top-Level Classes Baldwin explains nested top-level classes, and illustrates a very useful polymorphic structure where nested classes extend the enclosing class and

More information

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN CSSE 220 Day 15 Inheritance Check out DiscountSubclasses from SVN Discount Subclasses Work in pairs First look at my solution and understand how it works Then draw a UML diagram of it DiscountSubclasses

More information

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code COMPUTER SCIENCE DEPARTMENT PICNIC Welcome to the 2016-2017 Academic year! Meet your faculty, department staff, and fellow students in a social setting. Food and drink will be provided. When: Saturday,

More information

Chapter 10 Classes Continued. Fundamentals of Java

Chapter 10 Classes Continued. Fundamentals of Java Chapter 10 Classes Continued Objectives Know when it is appropriate to include class (static) variables and methods in a class. Understand the role of Java interfaces in a software system and define an

More information

Programming in C# Inheritance and Polymorphism

Programming in C# Inheritance and Polymorphism Programming in C# Inheritance and Polymorphism C# Classes Classes are used to accomplish: Modularity: Scope for global (static) methods Blueprints for generating objects or instances: Per instance data

More information

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Islamic University of Gaza Faculty of Engineering Computer Engineering Department Student Mark Islamic University of Gaza Faculty of Engineering Computer Engineering Department Question # 1 / 18 Question # / 1 Total ( 0 ) Student Information ID Name Answer keys Sector A B C D E A B

More information

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at  Interfaces. Today Book-keeping Inheritance Subscribe to sipb-iap-java-students Interfaces Slides and code at http://sipb.mit.edu/iap/java/ The Object class Problem set 1 released 1 2 So far... Inheritance Basic objects,

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

Inheritance, Polymorphism, and Interfaces

Inheritance, Polymorphism, and Interfaces Inheritance, Polymorphism, and Interfaces Chapter 8 Inheritance Basics (ch.8 idea) Inheritance allows programmer to define a general superclass with certain properties (methods, fields/member variables)

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

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

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine Homework 6 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 21, 2013 Question 1 What is the output of the following C++ program? #include #include using namespace

More information

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

Computer Science II (20073) Week 1: Review and Inheritance Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system

More information

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object Oriented Programming. Java-Lecture 11 Polymorphism Object Oriented Programming Java-Lecture 11 Polymorphism Abstract Classes and Methods There will be a situation where you want to develop a design of a class which is common to many classes. Abstract class

More information

OBJECT ORİENTATİON ENCAPSULATİON

OBJECT ORİENTATİON ENCAPSULATİON OBJECT ORİENTATİON Software development can be seen as a modeling activity. The first step in the software development is the modeling of the problem we are trying to solve and building the conceptual

More information

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

Topic 5 Polymorphism.  Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code. Topic 5 Polymorphism " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code. 1 Polymorphism Another feature of OOP literally having many forms object variables in

More information

JAVA Programming Language Homework I - OO concept

JAVA Programming Language Homework I - OO concept JAVA Programming Language Homework I - OO concept Student ID: Name: 1. Which of the following techniques can be used to prevent the instantiation of a class by any code outside of the class? A. Declare

More information

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

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass? 1. Overriding Methods A subclass can modify behavior inherited from a parent class. A subclass can create a method with different functionality than the parent s method but with the same: Name Return type

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

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

6.170 Recitation #5: Subtypes and Inheritance

6.170 Recitation #5: Subtypes and Inheritance 6.170 Recitation #5: Subtypes and Inheritance What is true subtyping? True Subtyping is not exactly the same as Inheritance. As seen in an earlier lecture, class A is a true subtype of class B if and only

More information

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg Ad hoc-polymorphism Outline Method overloading Sub-type Polymorphism Method overriding Dynamic

More information

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

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Java Inheritance Written by John Bell for CS 342, Spring 2018 Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Review Which of the following is true? A. Java classes may either

More information

BBM 102 Introduction to Programming II Spring 2017

BBM 102 Introduction to Programming II Spring 2017 BBM 102 Introduction to Programming II Spring 2017 Abstract Classes and Interfaces Instructors: Ayça Tarhan, Fuat Akal, Gönenç Ercan, Vahid Garousi Today Abstract Classes Abstract methods Polymorphism

More information

Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Polymorphism 1 / 19 Introduction to Object-Oriented Programming Today we ll learn how to combine all the elements of object-oriented programming in the design of a program that handles a company payroll.

More information

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

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14 8.1 Inheritance superclass 8.1 Class Diagram for Words! Inheritance is a fundamental technique used to create and organize reusable classes! The child is- a more specific version of parent! The child inherits

More information

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

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name: CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, 2017 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : If a child overrides

More information

Polymorphism and Interfaces. CGS 3416 Spring 2018

Polymorphism and Interfaces. CGS 3416 Spring 2018 Polymorphism and Interfaces CGS 3416 Spring 2018 Polymorphism and Dynamic Binding If a piece of code is designed to work with an object of type X, it will also work with an object of a class type that

More information

Inheritance (Part 5) Odds and ends

Inheritance (Part 5) Odds and ends Inheritance (Part 5) Odds and ends 1 Static Methods and Inheritance there is a significant difference between calling a static method and calling a non-static method when dealing with inheritance there

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 4(b): Subclasses and Superclasses OOP OOP - Inheritance Inheritance represents the is a relationship between data types (e.g. student/person)

More information

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017 Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 11: Inheritance and Polymorphism Part 1 Instructor: AbuKhleif, Mohammad Noor Sep 2017 Instructor AbuKhleif, Mohammad

More information

PROGRAMMING LANGUAGE 2

PROGRAMMING LANGUAGE 2 31/10/2013 Ebtsam Abd elhakam 1 PROGRAMMING LANGUAGE 2 Java lecture (7) Inheritance 31/10/2013 Ebtsam Abd elhakam 2 Inheritance Inheritance is one of the cornerstones of object-oriented programming. It

More information

Chapter 14 Abstract Classes and Interfaces

Chapter 14 Abstract Classes and Interfaces Chapter 14 Abstract Classes and Interfaces 1 What is abstract class? Abstract class is just like other class, but it marks with abstract keyword. In abstract class, methods that we want to be overridden

More information

CS260 Intro to Java & Android 03.Java Language Basics

CS260 Intro to Java & Android 03.Java Language Basics 03.Java Language Basics http://www.tutorialspoint.com/java/index.htm CS260 - Intro to Java & Android 1 What is the distinction between fields and variables? Java has the following kinds of variables: Instance

More information

Inheritance and object compatibility

Inheritance and object compatibility Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not the other way around Examples: reference/pointer can

More information

Inheritance -- Introduction

Inheritance -- Introduction Inheritance -- Introduction Another fundamental object-oriented technique is called inheritance, which, when used correctly, supports reuse and enhances software designs Chapter 8 focuses on: the concept

More information

CS 113 PRACTICE FINAL

CS 113 PRACTICE FINAL CS 113 PRACTICE FINAL There are 13 questions on this test. The value of each question is: 1-10 multiple choice (4 pt) 11-13 coding problems (20 pt) You may get partial credit for questions 11-13. If you

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 4(b): Inheritance & Polymorphism Lecture Contents What is Inheritance? Super-class & sub class The object class Using extends keyword

More information

Object Oriented Programming 2015/16. Final Exam June 28, 2016

Object Oriented Programming 2015/16. Final Exam June 28, 2016 Object Oriented Programming 2015/16 Final Exam June 28, 2016 Directions (read carefully): CLEARLY print your name and ID on every page. The exam contains 8 pages divided into 4 parts. Make sure you have

More information

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed 1 1 COMP200 ABSTRACT CLASSES OOP using Java, from slides by Shayan Javed Abstract Classes 2 3 From the previous lecture: public class GeometricObject { protected String Color; protected String name; protected

More information

Comp 249 Programming Methodology

Comp 249 Programming Methodology Comp 249 Programming Methodology Chapter 7 - Inheritance Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted,

More information

Rules and syntax for inheritance. The boring stuff

Rules and syntax for inheritance. The boring stuff Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for

More information

Inheritance 2. Inheritance 2. Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria

Inheritance 2. Inheritance 2. Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria Inheritance 2 Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria Wolfgang.Schreiner@risc.jku.at http://www.risc.jku.at Wolfgang Schreiner RISC

More information

CS 11 java track: lecture 3

CS 11 java track: lecture 3 CS 11 java track: lecture 3 This week: documentation (javadoc) exception handling more on object-oriented programming (OOP) inheritance and polymorphism abstract classes and interfaces graphical user interfaces

More information

National University. Faculty of Computer Since and Technology Object Oriented Programming

National University. Faculty of Computer Since and Technology Object Oriented Programming National University Faculty of Computer Since and Technology Object Oriented Programming Lec (8) Exceptions in Java Exceptions in Java What is an exception? An exception is an error condition that changes

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

CH. 2 OBJECT-ORIENTED PROGRAMMING

CH. 2 OBJECT-ORIENTED PROGRAMMING CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED

More information

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface. Abstract and final classes [Horstmann, pp. 490 491] An abstract class is kind of a cross between a class and an interface. In a class, all methods are defined. In an interface, methods are declared rather

More information

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

1. Which of the following is the correct expression of character 4? a. 4 b. 4 c. '\0004' d. '4' Practice questions: 1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4' 2. Will System.out.println((char)4) display 4? a. Yes b. No 3. The expression "Java

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

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

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L Inheritance Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 9.4 1 Inheritance Inheritance allows a software developer to derive

More information

8359 Object-oriented Programming with Java, Part 2. Stephen Pipes IBM Hursley Park Labs, United Kingdom

8359 Object-oriented Programming with Java, Part 2. Stephen Pipes IBM Hursley Park Labs, United Kingdom 8359 Object-oriented Programming with Java, Part 2 Stephen Pipes IBM Hursley Park Labs, United Kingdom Dallas 2003 Intro to Java recap Classes are like user-defined types Objects are like variables of

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

About This Lecture. Data Abstraction - Interfaces and Implementations. Outline. Object Concepts. Object Class, Protocol and State.

About This Lecture. Data Abstraction - Interfaces and Implementations. Outline. Object Concepts. Object Class, Protocol and State. Revised 01/09/05 About This Lecture Slide # 2 Data Abstraction - Interfaces and Implementations In this lecture we will learn how Java objects and classes can be used to build abstract data types. CMPUT

More information

Questions Answer Key Questions Answer Key Questions Answer Key

Questions Answer Key Questions Answer Key Questions Answer Key Benha University Term: 2 nd (2013/2014) Class: 2 nd Year Students Subject: Object Oriented Programming Faculty of Computers & Informatics Date: 26/4/2014 Time: 1 hours Exam: Mid-Term (A) Name:. Status:

More information

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1 Inheritance Reuse of code Extension and intension Class specialization and class extension Inheritance The protected keyword revisited Inheritance and methods Method redefinition The composite design pattern

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

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Table of Contents 1 Reusing Classes... 2 1.1 Composition... 2 1.2 Inheritance... 4 1.2.1 Extending Classes... 5 1.2.2 Method Overriding... 7 1.2.3

More information

Name: CS 159 Practice Final Fall 2015

Name: CS 159 Practice Final Fall 2015 Name: CS 159 Practice Final Fall 2015 CS 159, Fall 2015 Final Exam Section 02 Page 2 of 16 1. Choose the best answer for each of the following multiple choice questions. (a) (2 points) What will happen

More information

Name: CS 159 Practice Final Fall 2015

Name: CS 159 Practice Final Fall 2015 Name: CS 159 Practice Final Fall 2015 CS 159, Fall 2015 Final Exam Section 02 Page 2 of 17 1. Choose the best answer for each of the following multiple choice questions. (a) (2 points) What will happen

More information

Reusing Classes. Hendrik Speleers

Reusing Classes. Hendrik Speleers Hendrik Speleers Overview Composition Inheritance Polymorphism Method overloading vs. overriding Visibility of variables and methods Specification of a contract Abstract classes, interfaces Software development

More information