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

Size: px
Start display at page:

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

Transcription

1

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

3 Class Interaction There are 3 types of class interaction. One is inheritance, which the main focus of this chapter. Another is composition, which we will look at briefly. The third is utility classes which is explained on the next slide.

4 Utility Classes You have been working with Utility Classes for a while. These are classes which are not used to create objects, but still contain several useful methods. The Math class and the Expo class are both perfect examples of this.

5

6 Inheritance Inheritance is the process of using features (both attributes and methods) from an existing class. The existing class is called the superclass and the new class, which inherits the superclass features, is called the subclass. superclass: Car subclasses: Truck, Limo & Racecar

7 Is-A and Has-A The creation of new classes with the help of existing classes makes an important distinction between two approaches. An "is-a" relationship declares a new class as a special new-and-improved case of an existing class. In Geometry, a parallelogram "is-a" quadrilateral with special properties. A has-a relationship declares a new class composed of an existing class or classes. A line "has" points, a square "has" lines, and a cube "has" squares. A truck "is-a" car, but it "has-an" engine.

8 Composition Composition occurs when the data attributes of one class are objects of another class. You do NOT say A Car is-an Engine or A Car is 4 tires but you DO say A Car has-an Engine & A car has 4 tires. class: Car Contained Objects: 1 Engine 4 Tires

9 Inheritance vs. Composition In computer science an "is-a" relationship is called inheritance and a "has-a" relationship is called composition. A TireSwing is-a Swing. A TireSwing has-a Tire.

10

11 // Java0901.java // This program presents a <Person> class and a <Student> class. // The main method calls the <showage> method with a <Student> object. // This program will not compile. Java cannot find the <showage> method. public class Java0901 public static void main(string args[]) System.out.println("\nJAVA0901\n"); Student tom = new Student(); tom.showage(); tom.showgrade(); System.out.println(); class Person private int age; public void showage() System.out.println("Person's Age is unknown right now"); class Student private int grade; public void showgrade() System.out.println("Student's Grade is unknown right now");

12 // Java0902.java // This program demonstrates fundamental inheritance with <extends>. // The <Student> is declared JAVA0902 as a subclass of the <Person> superclass. // Now the program compiles and executes correctly. Person's Age is unknown right now Student's Grade is unknown right now public class Java0902 public static void main(string args[]) System.out.println("\nJAVA0902\n"); Student tom = new Student(); tom.showage(); tom.showgrade(); System.out.println(); class Person private int age; public void showage() System.out.println("Person's Age is unknown right now"); class Student extends Person private int grade; public void showgrade() System.out.println("Student's Grade is unknown right now");

13 // Java0903.java // This program adds constructors to the <Person> &<Student> classes. Note how the <Person> // constructor is called, even though there does not appear to be a <Person> object instantiated. public class Java0903 public static void main(string args[]) System.out.println("\nJAVA0903\n"); Student tom = new Student(); System.out.println(); JAVA0903 Person Constructor Student Constructor class Person private int age; public Person() System.out.println("Person Constructor"); age = 17; class Student extends Person private int grade; public Student() System.out.println("Student Constructor"); grade = 12;

14 Inheritance and Constructors When an object of a subclass is instantiated, the constructor of the superclass is called first, followed by a call to the constructor of the subclass.

15

16 // Java0904.java // This program shows that the subclass does not have access to the private data // of the superclass. This program will not compile. public class Java0904 public static void main(string args[]) System.out.println("\nJAVA0904\n"); Student tom = new Student(); tom.showdata(); System.out.println(); class Person private int age; public Person() System.out.println("Person Constructor"); age = 17; class Student extends Person private int grade; public Student() System.out.println("Student Constructor"); grade = 12; public void showdata() System.out.println("Student's Grade is " + grade); System.out.println("Student's Age is " + age );

17 // Java0905.java // This program changes private super class data access to "protected". // The Student class can now access data from the Person class. public class Java0905 public static void main(string args[]) System.out.println("\nJAVA0905\n"); Student tom = new Student(); tom.showdata(); System.out.println(); JAVA0905 Person Constructor Student Constructor Student's Grade is 12 Student's Age is 17 class Person protected int age; public Person() System.out.println("Person Constructor"); age = 17; class Student extends Person private int grade; public Student() System.out.println("Student Constructor"); grade = 12; public void showdata() System.out.println("Student's Grade is " + grade); System.out.println("Student's Age is " + age );

18 public, private & protected Attributes & methods declared public can be accessed by methods declared both outside and inside the class. Attributes & methods declared private can only be accessed by methods declared inside the class. Attributes & methods declared protected can be accessed by methods declared inside the class or subclass.

19

20 // Java0906.java This program demonstrates inheritance at three levels. public class Java0906 public static void main(string args[]) System.out.println("\nJAVA0906\n"); Cat cat = new Cat("Tiger"); System.out.println(); System.out.println("Animal type: " + cat.gettype()); System.out.println("Animal weight: " + cat.getweight()); System.out.println("Animal age: " + cat.getage()); System.out.println(); JAVA0906 class Animal protected int age; public Animal() System.out.println("Animal Constructor Called"); age = 15; public int getage() return age; Animal Constructor Called Mammal Constructor Called Cat Constructor Called Animal type: Tiger Animal weight: 500 Animal age: 5 class Mammal extends Animal public int weight; public Mammal() weight = 500; System.out.println( "Mammal Constructor Called"); public int getweight() return weight; class Cat extends Mammal private String type; public Cat() type = "Tiger"; System.out.println( "Cat Constructor Called"); public String gettype() return type;

21 Multi-Level Inheritance & Multiple Inheritance The previous program showed an example of Multi-Level Inheritance. Multiple Inheritance is something different. It occurs when one subclass inherits from two or more superclasses. This feature is available in C++. It is NOT available in Java.

22 Multi-Level Inheritance Multiple Inheritance Animal Reptile Extinct Mammal Dinosaur Dog Terrier

23

24 Inheritance Demonstrated with GridWorld-1 In GridWorld, if you click on an Actor object, you will see all of the methods available from the Actor class.

25 Inheritance Demonstrated with GridWorld-2 If you click on a Bug object, you will see all of the Bug methods. You will also see all of the methods Bug inherits from Actor.

26 Inheritance Demonstrated with GridWorld-3 Here, Multi-Level Inheritance is demonstrated because an OctagonBug (Spider) inherits from a Bug, and a Bug inherits from an Actor.

27

28 // Java0907.java The Fish class, Stage #1 // The Fish1 class can only draw a fish at a fixed starting location. import java.awt.*; import java.applet.*; public class Java0907 extends Applet public void paint(graphics g) Fish1 f1 = new Fish1(); f1.drawfish(g); class Fish1 protected int x; // center X coordinate of the fish protected int y; // center Y coordinate of the fish protected int direction; // one of 4 directions fish is facing: 0-N, 90-E, 180-S, 270-W public Fish1() x = 500; y = 300; direction = 0;

29 public void drawfish(graphics g) Expo.setColor(g,Expo.black); switch (direction) case 0: Expo.fillOval(g,x,y,15,30); Expo.fillPolygon(g,x,y+30,x-15,y+40,x+15,y+40); break; case 90: Expo.fillOval(g,x,y,30,15); Expo.fillPolygon(g,x-30,y,x-40,y-15,x-40,y+15); break; case 180: Expo.fillOval(g,x,y,15,30); Expo.fillPolygon(g,x,y-30,x-15,y-40,x+15,y-40); break; case 270: Expo.fillOval(g,x,y,30,15); Expo.fillPolygon(g,x+30,y,x+40,y-15,x+40,y+15); break; default: System.out.println("ERROR!!! Direction must be 0,90,180,270");

30 // Java0908.java The Fish class, Stage #2 // The Fish2 class inherits the drawfish method. // Additionally, it adds a method to erase the fish and turn the fish. import java.awt.*; import java.applet.*; public class Java0908 extends Applet public void paint(graphics g) Fish2 f2 = new Fish2(); f2.drawfish(g); f2.turnfish(g); f2.turnfish(g); f2.turnfish(g); f2.turnfish(g); class Fish2 extends Fish1 public void turnfish(graphics g) Expo.delay(1000); erasefish(g); direction += 90; if (direction == 360) direction = 0; drawfish(g);

31 public void erasefish(graphics g) Expo.setColor(g,Expo.white); switch (direction) case 0: Expo.fillOval(g,x,y,15,30); Expo.fillPolygon(g,x,y+30,x-15,y+40,x+15,y+40); break; case 90: Expo.fillOval(g,x,y,30,15); Expo.fillPolygon(g,x-30,y,x-40,y-15,x-40,y+15); break; case 180: Expo.fillOval(g,x,y,15,30); Expo.fillPolygon(g,x,y-30,x-15,y-40,x+15,y-40); break; case 270: Expo.fillOval(g,x,y,30,15); Expo.fillPolygon(g,x+30,y,x+40,y-15,x+40,y+15); break; default: System.out.println("ERROR!!! Direction must be 0,90,180,270");

32 // Java0909.java The Fish class, Stage #3 // The Fish3 class adds method movefish, which moves the Fish object to a specified coordinate. import java.awt.*; import java.applet.*; public class Java0909 extends Applet public void paint(graphics g) Fish3 f3 = new Fish3(); f3.drawfish(g); f3.movefish(g,800,200); f3.turnfish(g); f3.movefish(g,300,500); f3.turnfish(g); class Fish3 extends Fish2 public void movefish(graphics g, int newx, int newy) Expo.delay(1000); erasefish(g); x = newx; y = newy; drawfish(g);

33 // Java0910.java The Fish class, Stage #4 // The Fish4 class redefines the movefish method. Fish objects will now move without erasing themselves. // Additionally, the Fish4 class adds a constructor, which draws a Fish object at a specified starting location. import java.awt.*; import java.applet.*; public class Java0910 extends Applet public void paint(graphics g) int xpos = 100; int ypos = 100; int direction = 90; Fish4 f4 = new Fish4(xPos,yPos,direction); for (int k = 1; k <= 8; k++) xpos += 80; f4.movefish(g,xpos,ypos); class Fish4 extends Fish3 public Fish4(Graphics g, int xpos, int ypos, int dir) x = xpos; y = ypos; direction = dir; drawfish(g); public void movefish(graphics g, int newx, int newy) Expo.delay(1000); x = newx; y = newy; drawfish(g);

34

35 Square Fish Challenge Change Program Java0910.java so that it displays a square of fish. To make the square fit, change the number of fish in the for loop from 8 to 6. NOTE: This will be similar to the GridWorld labs. for (int k = 1; k <= 6; k++) xpos += 80; f4.movefish(g,xpos,ypos);

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

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

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

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

Sorting. Sorting. Selection sort

Sorting. Sorting. Selection sort Sorting 1 Sorting Given a linear list of comparable objects of the same class (or values of the same type), we wish to sort (or reärrange) the objects in the increasing order. For simplicity, let s just

More information

JAVA MOCK TEST JAVA MOCK TEST II

JAVA MOCK TEST JAVA MOCK TEST II http://www.tutorialspoint.com JAVA MOCK TEST Copyright tutorialspoint.com This section presents you various set of Mock Tests related to Java Framework. You can download these sample mock tests at your

More information

Chapter 3: Inheritance and Polymorphism

Chapter 3: Inheritance and Polymorphism Chapter 3: Inheritance and Polymorphism Overview Inheritance is when a child class, or a subclass, inherits, or gets, all the data (properties) and methods from the parent class, or superclass. Just like

More information

IS-A / HAS-A. I IS-A Teacher and I HAS-A Laptop

IS-A / HAS-A. I IS-A Teacher and I HAS-A Laptop IS-A / HAS-A I IS-A Teacher and I HAS-A Laptop 1 Corresponding Book Sections Pearson Custom Computer Science: Chapter 1, Sections 9, 12 2 IS-A A class has an IS-A relationship to a class type if it is

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

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism Abstract Classes

More information

COMP 110/L Lecture 20. Kyle Dewey

COMP 110/L Lecture 20. Kyle Dewey COMP 110/L Lecture 20 Kyle Dewey Outline super in methods abstract Classes and Methods Polymorphism super in Methods Recap You ve seen super in constructors... Recap You ve seen super in constructors...

More information

STATIC, ABSTRACT, AND INTERFACE

STATIC, ABSTRACT, AND INTERFACE STATIC, ABSTRACT, AND INTERFACE Thirapon Wongsaardsakul STATIC When variable is a static type, java allocates memory for it at loading time. Class loader Byte code verifier Static variable has been loaded

More information

ICS 4U. Introduction to Programming in Java. Chapter 10 Notes

ICS 4U. Introduction to Programming in Java. Chapter 10 Notes ICS 4U Introduction to Programming in Java Chapter 10 Notes Classes and Inheritance In Java all programs are classes, but not all classes are programs. A standalone application is a class that contains

More information

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

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed 1 1 COMP200 INTERFACES OOP using Java, from slides by Shayan Javed Interfaces 2 ANIMAL picture food sleep() roam() makenoise() eat() 3 ANIMAL picture food sleep() roam() makenoise() eat() 4 roam() FELINE

More information

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

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism Programming using C# LECTURE 07 Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism What is Inheritance? A relationship between a more general class, called the base class

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

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

Create a Java project named week9

Create a Java project named week9 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.

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

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

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

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism Abstract

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

Polymorphism. Arizona State University 1

Polymorphism. Arizona State University 1 Polymorphism CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 15 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

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

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

CS 116 Week 8 Page 1

CS 116 Week 8 Page 1 CS 116 Week 8: Outline Reading: 1. Dale, Chapter 11 2. Dale, Lab 11 Objectives: 1. Mid-term exam CS 116 Week 8 Page 1 CS 116 Week 8: Lecture Outline 1. Mid-term Exam CS 116 Week 8 Page 2 CS 116 Week 8:

More information

Practice for Chapter 11

Practice for Chapter 11 Practice for Chapter 11 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Object-oriented programming allows you to derive new classes from existing

More information

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

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

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

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014 Lesson 10B Class Design By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Encapsulation Inheritance and Composition is a vs has a Polymorphism Information Hiding Public

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

IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.

IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance. PART 17 17. Inheritance Inheritance can be defined as the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.

More information

Final Examination Review

Final Examination Review Pre-AP Computer Science Final Examination Review Multiple Choice Test 1. Which of the following are Java integer data types? byte, short, integer and longinteger shortint, int and longint byteint, shortint,

More information

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

IT101. Inheritance, Encapsulation, Polymorphism and Constructors IT101 Inheritance, Encapsulation, Polymorphism and Constructors OOP Advantages and Concepts What are OOP s claims to fame? Better suited for team development Facilitates utilizing and creating reusable

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

CS 251 Intermediate Programming Inheritance

CS 251 Intermediate Programming Inheritance CS 251 Intermediate Programming Inheritance Brooke Chenoweth University of New Mexico Spring 2018 Inheritance We don t inherit the earth from our parents, We only borrow it from our children. What is inheritance?

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

C10: Garbage Collection and Constructors

C10: Garbage Collection and Constructors CISC 3120 C10: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/5/2018 CUNY Brooklyn College 1 Outline Recap OOP in Java: composition &

More information

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School Class Hierarchy and Interfaces David Greenstein Monta Vista High School Inheritance Inheritance represents the IS-A relationship between objects. an object of a subclass IS-A(n) object of the superclass

More information

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

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

Chapter 15: Object Oriented Programming

Chapter 15: Object Oriented Programming Chapter 15: Object Oriented Programming Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey How do Software Developers use OOP? Defining classes to create objects UML diagrams to

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

3D Graphics Programming Mira Costa High School - Class Syllabus,

3D Graphics Programming Mira Costa High School - Class Syllabus, 3D Graphics Programming Mira Costa High School - Class Syllabus, 2009-2010 INSTRUCTOR: Mr. M. Williams COURSE GOALS and OBJECTIVES: 1 Learn the fundamentals of the Java language including data types and

More information

Basics of Object Oriented Programming. Visit for more.

Basics of Object Oriented Programming. Visit   for more. Chapter 4: Basics of Object Oriented Programming Informatics Practices Class XII (CBSE Board) Revised as per CBSE Curriculum 2015 Visit www.ip4you.blogspot.com for more. Authored By:- Rajesh Kumar Mishra,

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

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a CBOP3203 An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a page. When you use a Java technology-enabled

More information

CSCE3193: Programming Paradigms

CSCE3193: Programming Paradigms CSCE3193: Programming Paradigms Nilanjan Banerjee University of Arkansas Fayetteville, AR nilanb@uark.edu http://www.csce.uark.edu/~nilanb/3193/s10/ Programming Paradigms 1 Java Packages Application programmer

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

Introduction to Java. March 1, 2001 CBRSS and the John M. Olin Institute for Strategic Studies Lars-Erik Cederman

Introduction to Java. March 1, 2001 CBRSS and the John M. Olin Institute for Strategic Studies Lars-Erik Cederman Introduction to Java March 1, 2001 CBRSS and the John M. Olin Institute for Strategic Studies Lars-Erik Cederman Outline Java overview simple structures object-orientation roulette example How to compile

More information

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes Java Curriculum for AP Computer Science, Student Lesson A20 1 STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes INTRODUCTION:

More information

INHERITANCE: EXTENDING CLASSES

INHERITANCE: EXTENDING CLASSES INHERITANCE: EXTENDING CLASSES INTRODUCTION TO CODE REUSE In Object Oriented Programming, code reuse is a central feature. In fact, we can reuse the code written in a class in another class by either of

More information

Inheritance (an intuitive description)

Inheritance (an intuitive description) Inheritance (an intuitive description) Recall the Orange class properties found in Orange are also shared with other Fruits (e.g. Apple, Banana, Pineapple) We associate behavior as well as state with with

More information

Introduction to Programming

Introduction to Programming Introduction to Programming SS 2010 Adrian Kacso, Univ. Siegen adriana.dkacsoa@duni-siegena.de Tel.: 0271/740-3966, Office: H-B 8406 Stand: June 21, 2010 Betriebssysteme / verteilte Systeme Introduction

More information

Object oriented programming Concepts

Object oriented programming Concepts Object oriented programming Concepts Naresh Proddaturi 09/10/2012 Naresh Proddaturi 1 Problems with Procedural language Data is accessible to all functions It views a program as a series of steps to be

More information

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

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring

More information

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course : Advanced Java Concepts + Additional Questions from Earlier Parts of the Course 1. Given the following hierarchy: class Alpha {... class Beta extends Alpha {... class Gamma extends Beta {... In what order

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

Chapter 2: Java OOP I

Chapter 2: Java OOP I Chapter 2: Java OOP I Yang Wang wyang AT njnet.edu.cn Outline OO Concepts Class and Objects Package Field Method Construct and Initialization Access Control OO Concepts Object Oriented Methods Object An

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

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

Object-Oriented Programming More Inheritance

Object-Oriented Programming More Inheritance Object-Oriented Programming More Inheritance Ewan Klein School of Informatics Inf1 :: 2009/10 Ewan Klein (School of Informatics) OOP: More Inheritance Inf1 :: 2009/10 1 / 45 1 Inheritance Flat Hierarchy

More information

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution Ch 5 Arrays Multiple Choice 01. An array is a (A) (B) (C) (D) data structure with one, or more, elements of the same type. data structure with LIFO access. data structure, which allows transfer between

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

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

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011 MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011 1 Goals of the Lecture Continue a review of fundamental object-oriented concepts 2 Overview of OO Fundamentals

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

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

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 OOP Three main programming mechanisms that constitute object-oriented programming (OOP) Encapsulation Inheritance

More information

Midterm assessment - MAKEUP Fall 2010

Midterm assessment - MAKEUP Fall 2010 M257 MTA Faculty of Computer Studies Information Technology and Computing Date: /1/2011 Duration: 60 minutes 1-Version 1 M 257: Putting Java to Work Midterm assessment - MAKEUP Fall 2010 Student Name:

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

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

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

Object Orientated Analysis and Design. Benjamin Kenwright

Object Orientated Analysis and Design. Benjamin Kenwright Notation Part 2 Object Orientated Analysis and Design Benjamin Kenwright Outline Review What do we mean by Notation and UML? Types of UML View Continue UML Diagram Types Conclusion and Discussion Summary

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

Crash Course Review Only. Please use online Jasmit Singh 2

Crash Course Review Only. Please use online Jasmit Singh 2 @ Jasmit Singh 1 Crash Course Review Only Please use online resources @ Jasmit Singh 2 Java is an object- oriented language Structured around objects and methods A method is an action or something you

More information

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003 Outline Object Oriented Programming Autumn 2003 2 Course goals Software design vs hacking Abstractions vs language (syntax) Java used to illustrate concepts NOT a course about Java Prerequisites knowledge

More information

Classes, Objects, and OOP in Java. June 16, 2017

Classes, Objects, and OOP in Java. June 16, 2017 Classes, Objects, and OOP in Java June 16, 2017 Which is a Class in the below code? Mario itsame = new Mario( Red Hat? ); A. Mario B. itsame C. new D. Red Hat? Whats the difference? int vs. Integer A.

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

Design Pattern and Software Architecture: IV. Design Pattern

Design Pattern and Software Architecture: IV. Design Pattern Design Pattern and Software Architecture: IV. Design Pattern AG Softwaretechnik Raum E 3.165 Tele.. 60-3321 hg@upb.de IV. Design Pattern IV.1 Introduction IV.2 Example: WYSIWYG Editor Lexi IV.3 Creational

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

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

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

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

Need to store a list of shapes, each of which could be a circle, rectangle, or triangle CS112-2012S-23 Abstract Classes and Interfaces 1 23-0: Drawing Example Creating a drawing program Allow user to draw triangles, circles, rectanlges, move them around, etc. Need to store a list of shapes,

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

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

Introduction to Computing II (ITI 1121) Midterm Examination

Introduction to Computing II (ITI 1121) Midterm Examination Introduction to Computing II (ITI 1121) Midterm Examination Instructors: Sherif G. Aly, Nathalie Japkowicz, and Marcel Turcotte February 2015, duration: 2 hours Identification Last name: First name: Student

More information

Inheritance CSC9Y4. Inheritance

Inheritance CSC9Y4. Inheritance Inheritance CSC9Y4 1 Inheritance We start with a superclass from which a subclass can be derived. New attributes and methods can be defined in a subclass and existing methods re-defined. How do we know

More information

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable? Peer Instruction 8 Classes and Objects How can multiple methods within a Java class read and write the same variable? A. Allow one method to reference a local variable of the other B. Declare a variable

More information

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University Day 3 COMP 1006/1406A Summer 2016 M. Jason Hinek Carleton University today s agenda assignments 1 was due before class 2 is posted (be sure to read early!) a quick look back testing test cases for arrays

More information

Object Oriented Programming: Based on slides from Skrien Chapter 2

Object Oriented Programming: Based on slides from Skrien Chapter 2 Object Oriented Programming: A Review Based on slides from Skrien Chapter 2 Object-Oriented Programming (OOP) Solution expressed as a set of communicating objects An object encapsulates the behavior and

More information

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012 More OO Fundamentals CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012 1 Goals of the Lecture Continue a review of fundamental object-oriented concepts 2 Overview of OO Fundamentals

More information

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

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5 Objective Questions BCA Part III page 1 of 5 1. Java is purely object oriented and provides - a. Abstraction, inheritance b. Encapsulation, polymorphism c. Abstraction, polymorphism d. All of the above

More information

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution Ch 5 Arrays Multiple Choice Test 01. An array is a ** (A) data structure with one, or more, elements of the same type. (B) data structure with LIFO access. (C) data structure, which allows transfer between

More information

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP Critique this Code string const & findsmallest(vector const &v) string smallest = v[0]; for (unsigned int i = 0; i < v.size(); i++) if (v[i] < smallest) smallest = v[i]; return smallest; Hint:

More information

Lecture 7: Classes and Objects CS2301

Lecture 7: Classes and Objects CS2301 Lecture 7: Classes and Objects NADA ALZAHRANI CS2301 1 What is OOP? Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly

More information

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

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value Paytm Programming Sample paper: 1) A copy constructor is called a. when an object is returned by value b. when an object is passed by value as an argument c. when compiler generates a temporary object

More information

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

Chapter 15: Inheritance, Polymorphism, and Virtual Functions Chapter 15: Inheritance, Polymorphism, and Virtual Functions 15.1 What Is Inheritance? What Is Inheritance? Provides a way to create a new class from an existing class The new class is a specialized version

More information

C++ Inheritance and Encapsulation

C++ Inheritance and Encapsulation C++ Inheritance and Encapsulation Private and Protected members Inheritance Type Public Inheritance Private Inheritance Protected Inheritance Special method inheritance 1 Private Members Private members

More information