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

Slides:



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

Inheritance Writing and using Classes effectively.
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Inheritance Inheritance is the process of using features (both attributes and methods) from an existing class. The existing class is called the superclass.
LECTURE 07 Programming using C# Inheritance
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Programming Languages and Paradigms Object-Oriented Programming.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
// Java0802.java // CardDeck Case Study #02 // Variables, called attributes or data fields, are added to the class. public class Java0802 { public static.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
// Java0601.java // This program demonstrates that the methods of a class are not always // accessible, like they were with the class. In this case an.
Exposure Java-A 2006 Chapter 4 Slides Using Methods and Parameters
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CSC 142 Computer Science II Zhen Jiang West Chester University
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Inheritance Inheritance is the process of using features (both attributes and methods) from an existing class. The existing class is called the superclass.
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction.
Inheritance Inheritance is the process of using features (both attributes and methods) from an existing class. The existing class is called the superclass.
Question of the Day  There are two escalators at each subway stop. One going up & one down.  Whenever an escalator needs to be fixed, they almost always.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
Exposure Java 2011 APCS Edition
PreAP Computer Science Quiz Key
Chapter 2 Creating a Java Application and Applet.
Question of the Day  There are two escalators at each subway stop. One going up & one down.  Whenever an escalator needs to be fixed, they almost always.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Class Interaction Polymorphism.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
BY:- TOPS Technologies
The 4 Stages of Program Design  Cryptic Programming Stage  Unstructured, Spaghetti-Programming Stage  Structured Programming Stage  Object Oriented.
Review of Math Class Methods abssqrt minmax powround ceilfloor.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
The 4 Stages of Program Design  Cryptic Programming Stage  Unstructured, Spaghetti-Programming Stage  Structured Programming Stage  Object Oriented.
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction.
Modern Programming Tools And Techniques-I
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Objects as a programming concept
Exposure Java 2013 APCS Edition Chapter 12 Slides Focus on OOP:
ATS Application Programming: Java Programming
Exposure Java 2013 APCS Edition Chapter 9 Slides Focus on OOP:
Chapter 10 Thinking in Objects
Exposure Java 2015 AP®CS Edition
Section 8.7 The Consequences of Scope.
MSIS 670 Object-Oriented Software Engineering
Pre-AP® Computer Science Quiz Key
Pre-AP® Computer Science Quiz
Section 9.1 Introduction.
PreAP Computer Science Quiz
Chapter 11 Inheritance and Polymorphism Part 1
Java Programming: From the Ground Up
Chapter 11 Inheritance and Encapsulation and Polymorphism
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism 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 class and a class. // The main method calls the method with a object. // This program will not compile. Java cannot find the 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. // The is declared as a subclass of the superclass. // Now the program compiles and executes correctly. 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 & classes. Note how the // constructor is called, even though there does not appear to be a object instantiated. public class Java0903 { public static void main(String args[]) { System.out.println("\nJAVA0903\n"); Student tom = new Student(); 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; } }

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(); } 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(); } class Animal { protected int age; public Animal() { System.out.println("Animal Constructor Called"); age = 15; } public int getAge() { return age; } } 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 Mammal Dog Terrier Reptile Dinosaur Extinct

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