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

Similar documents
Inheritance and Polymorphism

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

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

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

Sorting. Sorting. Selection sort

JAVA MOCK TEST JAVA MOCK TEST II

Chapter 3: Inheritance and Polymorphism

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

What is Inheritance?

Programming II (CS300)

COMP 110/L Lecture 20. Kyle Dewey

STATIC, ABSTRACT, AND INTERFACE

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

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

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

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

OVERRIDING. 7/11/2015 Budditha Hettige 82

Create a Java project named week9

Object Orientated Programming Details COMP360

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

Week 11: Class Design

Programming II (CS300)

Full file at Chapter 2 - Inheritance and Exception Handling

Polymorphism. Arizona State University 1

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

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

CS 116 Week 8 Page 1

Practice for Chapter 11

Create a Java project named week10

CSC 1214: Object-Oriented Programming

Abstract class & Interface

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

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.

Final Examination Review

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Lecture 6 Introduction to Objects and Classes

CS 251 Intermediate Programming Inheritance

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

C10: Garbage Collection and Constructors

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

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

Chapter 15: Object Oriented Programming

Chapter 5. Inheritance

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

Basics of Object Oriented Programming. Visit for more.

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

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

CSCE3193: Programming Paradigms

Week 5-1: ADT Design

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

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

INHERITANCE: EXTENDING CLASSES

Inheritance (an intuitive description)

Introduction to Programming

Object oriented programming Concepts

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

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

CS 11 java track: lecture 3

Chapter 2: Java OOP I

Chapter 10 Classes Continued. Fundamentals of Java

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

Object-Oriented Programming More Inheritance

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

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

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

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

Programming Exercise 14: Inheritance and Polymorphism

Java Fundamentals (II)

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

Midterm assessment - MAKEUP Fall 2010

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

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

Object-Oriented Concepts

Object Orientated Analysis and Design. Benjamin Kenwright

1.00 Lecture 13. Inheritance

Crash Course Review Only. Please use online Jasmit Singh 2

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

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

Inheritance and Interfaces

Design Pattern and Software Architecture: IV. Design Pattern

Java Object Oriented Design. CSC207 Fall 2014

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

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

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

Arrays Classes & Methods, Inheritance

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

Introduction to Computing II (ITI 1121) Midterm Examination

Inheritance CSC9Y4. Inheritance

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

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

Object Oriented Programming: Based on slides from Skrien Chapter 2

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

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

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

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

Lecture 7: Classes and Objects CS2301

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

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

C++ Inheritance and Encapsulation

Transcription:

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 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.

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.

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

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.

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

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.

// 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");

// 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");

// 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;

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.

// 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 );

// 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 );

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.

// 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;

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.

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

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.

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.

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.

// 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;

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");

// 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);

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");

// 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);

// 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);

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);