Exposure Java 2011 APCS Edition

Slides:



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

Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
Inheritance Writing and using Classes effectively.
Topic 9 more graphics Based on slides bu Marty Stepp and Stuart Reges from
AP Computer Science DYRT Quiz Key
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.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
(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
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Intro to OOP with Java, C. Thomas Wu
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Review of Math Class Methods abssqrt minmax powround ceilfloor.
// Java0802.java // CardDeck Case Study #02 // Variables, called attributes or data fields, are added to the class. public class Java0802 { public static.
Arranging the border values of methods. import java.awt.*; import java.applet.Applet; public class Applet2 extends Applet { public void paint(Graphics.
// 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 Inheritance is the process of using features (both attributes and methods) from an existing class. The existing class is called the superclass.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
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.
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction.
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.
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
Inheritance and Access Control CS 162 (Summer 2009)
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.
PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Visual Classes 1 Class: Bug 5 Objects: All Bug Objects.
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.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
// 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");
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
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.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
BY:- TOPS Technologies
The 4 Stages of Program Design  Cryptic Programming Stage  Unstructured, Spaghetti-Programming Stage  Structured Programming Stage  Object Oriented.
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.
Advanced Programming in Java
Objects as a programming concept
Building Java Programs
Week 8 Lecture -3 Inheritance and Polymorphism
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:
Exposure Java 2015 AP®CS Edition
Section 8.7 The Consequences of Scope.
Pre-AP® Computer Science Quiz Key
Pre-AP® Computer Science Quiz
Advanced Programming Behnam Hatami Fall 2017.
Exposure Java 2015 AP®CS Edition
Section 9.1 Introduction.
PreAP Computer Science Quiz
Topic 9 More Graphics Based on slides bu Marty Stepp and Stuart Reges from
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Exposure Java 2011 APCS Edition Chapter 9 Slides Inheritance and Composition PowerPoint Presentation created by: Mr. John L. M. Schram and Mr. Leon Schram authors of Exposure Java

Section 9.2 “Is-A” & “Has-A” Relationships

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" vehicle, but it "has-an" engine. In computer science an "is-a" relationship involves inheritance and a "has-a" relationship involves composition.

Section 9.3 Inheritance Program Examples

// Java0901.java // This program demonstrates fundamental inheritance with <extends>. There are no constructors yet, // which results in Java handling the construction and assigning default values to the attributes. 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 extends Person private int grade; public void showGrade() System.out.println("Student's Grade is unknown right now");

// Java0902.java // This program adds constructors to the <Person> and <Student> classes. // Note how the <Person> constructor is called, even though there does not appear // to be a <Person> object instantiated. public class Java0902 { public static void main(String args[]) System.out.println("\nJAVA0902\n"); Student tom = new Student(); tom.showData(); System.out.println(); } class Person private int age; public Person() { System.out.println("Person Constructor"); age = 17; } public int getAge() { return age; } class Student extends Person private int grade; public Student() { System.out.println("Student Constructor"); grade = 12; } public int getGrade() { return grade; } public void showData() System.out.println("Age: " + getAge()); System.out.println("Grade: " + getGrade());

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.

// Java0903.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 Java0903 { public static void main(String args[]) System.out.println("\nJAVA0903\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);

// Java0904.java // This program changes private member data to "protected" data. // The Student class can now access data from the Person class. 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 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 only be accessed by methods declared inside the class or subclass, and yes it can also be accessed from anywhere in the same package.

// Java0905.java // This program uses a parameter constructor to pass information to the Student constructor. // Person still calls a default (no parameter) constructor. public class Java0905 { public static void main(String args[]) System.out.println("\nJAVA0905\n"); Student tom = new Student(12); tom.showData(); System.out.println(); } class Person protected int age; public Person() { System.out.println("Person Default Constructor"); age = 17; } public int getAge() { return age; } class Student extends Person protected int grade; public Student(int g) { System.out.println("Student Parameter Constructor"); grade = g; } public int getGrade() { return grade; } public void showData() System.out.println("Student's Grade is " + grade); System.out.println("Student's Age is " + age);

// Java0906.java This program demonstrates how to pass information to the superclass by using <super>. public class Java0906 { public static void main(String args[]) System.out.println("\nJAVA0906\n"); Student tom = new Student(12,17); tom.showData(); } class Person protected int age; public Person(int a) { System.out.println("Person Parameter Constructor"); age = a; } public int getAge() { return age; } class Student extends Person protected int grade; public Student(int g, int a) super(a); // this must be the first call grade = g; System.out.println("Student Parameter Constructor"); public int getGrade() { return grade; } public void showData() System.out.println("Student's Grade is " + getGrade()); System.out.println("Student's Age is " + getAge());

// Java0907.java // In this program both the <Person> class and the <Student> class each have a method with the same identifier. // The program returns the grade information twice and does not know that the <getData> method of the <Person> // class should also be used. public class Java0907 { public static void main(String args[]) System.out.println("\nJAVA0907\n"); Student tom = new Student(12,17); tom.showData(); System.out.println(); } class Person protected int age; public Person(int a) { System.out.println("Person Parameter Constructor"); age = a; } public int getData() { return age; } class Student extends Person protected int grade; public Student(int g, int a) { super(a); grade = g; System.out.println("Student Parameter Constructor"); } public int getData() { return grade; } public void showData() System.out.println("Student's Grade is " + getData()); System.out.println("Student's Age is " + getData());

// Java0908.java // This program solves the problem of Java0907.java. // The Java keyword <super> is used to indicate that the getData method of the superclass is intended. public class Java0908 { public static void main(String args[]) System.out.println("\nJAVA0907\n"); Student tom = new Student(12,17); tom.showData(); System.out.println(); } class Person protected int age; public Person(int a) { System.out.println("Person Parameter Constructor"); age = a; } public int getData() { return age; } class Student extends Person protected int grade; public Student(int g, int a) { super(a); grade = g; System.out.println("Student Parameter Constructor"); } public int getData() { return grade; } public void showData() System.out.println("Student's Grade is " + getData()); System.out.println("Student's Age is " + super.getData());

// Java0909.java // This program demonstrates inheritance at three levels. public class Java0909 { public static void main(String args[]) System.out.println("\nJAVA0909\n"); Cat tiger = new Cat("Tiger",500,5); System.out.println(); System.out.println("Animal type: " + tiger.getType()); System.out.println("Animal weight: " + tiger.getWeight()); System.out.println("Animal age: " + tiger.getAge()); } class Animal protected int age; public Animal(int a) System.out.println("Animal Constructor Called"); age = a; public int getAge() { return age; } class Mammal extends Animal protected int weight; public Mammal(int w, int a) super(a); weight = w; System.out.println( "Mammal Constructor Called"); public int getWeight() { return weight; } class Cat extends Mammal protected String type; public Cat(String t, int w, int a) super(w,a); type = t; "Cat Constructor Called"); public String getType() { return type; }

Using super The keyword super used as the first statement in a constructor passes information to the super class constructor, like super(a); The same keyword super used in front of a method indicates that a method of the superclass needs to be called, like super.getData(); Information can be passed up to multiple inheritance levels, but it can only be passed one level at one time.

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

Section 9.4 Composition Program Examples

Composition Composition occurs when the data attributes of one class are objects of another class. You do NOT say “A Car is-an Engine” but you DO say “A Car has-an Engine” class: Car Contained Classes: Engine Tire

Can it be both? It is possible for a program to use both inheritance and composition. You can say “A TireSwing is-a Swing”. You can also say, “A TireSwing has-a Tire.” Note: The same Tire can be used for a Car and a Swing. superclass: Swing subclass: TireSwing Contained Class: Tire

// Java0910.java // This program uses an <Engine> object in a "has-a" composition relationship. public class Java0910 { public static void main(String args[]) System.out.println("\nJAVA0910\n"); Car car = new Car("Ford",350); System.out.println(); car.getData(); } class Engine private int horsePower; public Engine(int hp) System.out.println( "Engine Constructor Called"); horsePower = hp; public int getHorsePower() return horsePower; class Car private String type; private Engine engine; public Car(String t, int hp) System.out.println("Car Constructor Called"); type = t; engine = new Engine(hp); public void getData() System.out.println("Car Type: " + type); System.out.println("Horse Power: " + engine.getHorsePower());

// Java0911.java // This program uses multiple classes // not in an "is-a" inheritance relationship, // but a "has-a" composition relationship public class Java0911 { public static void main(String args[]) System.out.println("\nJAVA0911\n"); Car car = new Car("Ford",6,350,"Red"); System.out.println(); car.getData(); } class Wheel private int wheelCount; public Wheel(int wc) System.out.println( "Wheel Constructor Called"); wheelCount = wc; public int getWheelCount() { return wheelCount; class Car private String type; private Wheel wheel; private Engine engine; private Paint paint; public Car(String t, int wc, int hp, String pc) "Car Constructor Called"); type = t; wheel = new Wheel(wc); engine = new Engine(hp); paint = new Paint(pc); public void getData() System.out.println("Car Type: " + type); System.out.println("Wheel Count: " + wheel.getWheelCount()); System.out.println("Horse Power: " + engine.getHorsePower()); System.out.println("Paint Color: " + paint.getPaintColor()); class Engine private int horsePower; public Engine(int hp) "Engine Constructor Called"); horsePower = hp; public int getHorsePower() { return horsePower; class Paint private String paintColor; public Paint(String pc) "Paint Constructor Called"); paintColor = pc; public String getPaintColor() { return paintColor;

Section 9.5 Jack-O'-lantern Case Study

Jack-O'-lantern Case Study We will now look at a simple 3-step Case Study that will ultimately draw a Jack-O’-lantern. The end result is shown below: Inspired by Kristen Graber - Berkner HS student 2005

// Java0912.java // JackO'lantern Case Study, Stage #1 // This program shows the <Pumpkin> class. import java.awt.*; import java.applet.*; public class Java0912 extends Applet { public void paint(Graphics g) Pumpkin p = new Pumpkin(g); } class Pumpkin public Pumpkin(Graphics g) g.setColor(Color.orange); g.fillOval(100,100,600,450); g.setColor(new Color(50,200,50)); g.fillRect(390,30,20,80);

// Java0913.java // JackO'lantern Case Study, Stage #2 // This program creates the <Face> class import java.awt.*; import java.applet.*; public class Java0913 extends Applet { public void paint(Graphics g) Face f = new Face(g); } class Face public Face(Graphics g) drawEyes(g); drawNose(g); drawMouth(g); public void drawEyes(Graphics g) g.setColor(Color.black); g.fillOval(200,200,100,100); g.fillOval(500,200,100,100); public void drawNose(Graphics g) Polygon nose = new Polygon(); nose.addPoint(350,340); nose.addPoint(450,340); nose.addPoint(400,270); g.fillPolygon(nose); public void drawMouth(Graphics g) Polygon mouth = new Polygon(); mouth.addPoint(300,400); mouth.addPoint(200,350); mouth.addPoint(250,450); mouth.addPoint(400,500); mouth.addPoint(550,450); mouth.addPoint(600,350); mouth.addPoint(500,400); g.fillPolygon(mouth);

// Java0914.java JackO'lantern Case Study, Stage #3 // This program demonstrates both inheritance and composition. // A JackO'lantern is-a Pumpkin. A JackO'lantern has-a face. import java.awt.*; import java.applet.*; public class Java0914 extends Applet { public void paint(Graphics g) JackOlantern jack = new JackOlantern(g); } class JackOlantern extends Pumpkin private Face f; public JackOlantern(Graphics g) super(g); f = new Face(g); class Pumpkin public Pumpkin(Graphics g) g.setColor(Color.orange); g.fillOval(100,100,600,450); g.setColor(new Color(50,200,50)); g.fillRect(390,30,20,80); class Face // same as the last program

Section 9.6 The Train Case Study

Train Case Study We will now look at a longer 6-step Case Study that will ultimately draw a Train. The end result is shown below: Inspired by Greg Muzljakovich and Amy Ho – BHS students 2005

// Java0915.java Train case study, Stage #1 // This program begins the <TrainCar> class. // The train car is blue and it is always drawn in the same place. import java.awt.*; import java.applet.*; public class Java0915 extends Applet { public void paint(Graphics g) TrainCar tc = new TrainCar(g); } class TrainCar public TrainCar(Graphics g) g.setColor(Color.blue); g.fillRect(325,250,150,100); g.setColor(Color.black); g.fillOval(330,325,50,50); g.fillOval(420,325,50,50);

// Java0916.java // Train case study, Stage #2 // This program improves the TrainCar class by allowing a train car to be any color. import java.awt.*; import java.applet.*; public class Java0916 extends Applet { public void paint(Graphics g) TrainCar tc = new TrainCar(g,Color.green); } class TrainCar private Color carColor; public TrainCar(Graphics g, Color cc) carColor = cc; g.setColor(carColor); g.fillRect(325,250,150,100); g.setColor(Color.black); g.fillOval(330,325,50,50); g.fillOval(420,325,50,50);

// Java0917.java // Train case study, Stage #3 // This program further improves the <TrainCar> class by // allowing train cars to be drawn in different locations. import java.awt.*; import java.applet.*; public class Java0917 extends Applet { public void paint(Graphics g) TrainCar tc1 = new TrainCar(g,Color.blue,70); TrainCar tc2 = new TrainCar(g,Color.green,240); TrainCar tc3 = new TrainCar(g,Color.yellow,410); TrainCar tc4 = new TrainCar(g,Color.magenta,580); } class TrainCar private Color carColor; public TrainCar(Graphics g, Color cc, int x) carColor = cc; g.setColor(carColor); g.fillRect(x,250,150,100); g.setColor(Color.black); g.fillOval(x+5,325,50,50); g.fillOval(x+95,325,50,50);

// Java0918.java Train case study, Stage #4 // This program adds the <Locomotive> class. // A <Locomotive> is-a <TrainCar> import java.awt.*; import java.applet.*; public class Java0918 extends Applet { public void paint(Graphics g) Locomotive loc = new Locomotive(g,Color.blue,70); } class TrainCar private Color carColor; public TrainCar(Graphics g, Color cc, int x) carColor = cc; g.setColor(carColor); g.fillRect(x,250,150,100); g.setColor(Color.black); g.fillOval(x+5,325,50,50); g.fillOval(x+95,325,50,50); class Locomotive extends TrainCar private int x; public Locomotive(Graphics g, Color cc, int x1) super(g,cc,x1); x = x1; drawScoop(g); drawFunnel(g); public void drawScoop(Graphics g) Polygon scoop = new Polygon(); scoop.addPoint(x,300); scoop.addPoint(x,350); scoop.addPoint(x-50,350); g.fillPolygon(scoop); public void drawFunnel(Graphics g) Polygon funnel = new Polygon(); funnel.addPoint(x+20,250); funnel.addPoint(x+20,220); funnel.addPoint(x,200); funnel.addPoint(x,190); funnel.addPoint(x+60,190); funnel.addPoint(x+60,200); funnel.addPoint(x+40,220); funnel.addPoint(x+40,250); g.fillPolygon(funnel);

// Java0919.java Train case study, Stage #5 // This program adds the <Caboose> class. // A caboose is a red train car with windows and a raised roof import java.awt.*; import java.applet.*; public class Java0919 extends Applet { public void paint(Graphics g) Caboose cab = new Caboose(g,580); } class TrainCar private Color carColor; public TrainCar(Graphics g, Color cc, int x) carColor = cc; g.setColor(carColor); g.fillRect(x,250,150,100); g.setColor(Color.black); g.fillOval(x+5,325,50,50); g.fillOval(x+95,325,50,50); class Caboose extends TrainCar private int x; public Caboose(Graphics g, int x1) super(g,Color.red,x1); x = x1; drawWindows(g); drawTop(g); public void drawWindows(Graphics g) g.setColor(Color.white); g.fillRect(x+30,280,30,30); g.fillRect(x+90,280,30,30); public void drawTop(Graphics g) g.setColor(Color.red); g.fillRect(x+30,235,90,15); g.fillRect(x+25,235,100,5);

NOTE: For this Train to fit // Java0920.java Train case study, Stage #6 // This program finally adds the <Train> class // A train has train cars. The first train car is the locomotive. The last train car is the Caboose. import java.awt.*; import java.applet.*; public class Java0920 extends Applet { public void paint(Graphics g) Train t = new Train(g,55); } class Train private Locomotive loc; private TrainCar tc1; private TrainCar tc2; private TrainCar tc3; private TrainCar tc4; private Caboose cab; public Train(Graphics g, int x) loc = new Locomotive(g,Color.blue,x); tc1 = new TrainCar(g,Color.green,x+160); tc2 = new TrainCar(g,Color.yellow,x+320); tc3 = new TrainCar(g,Color.magenta,x+480); tc4 = new TrainCar(g,Color.cyan,x+640); cab = new Caboose(g,x+800); NOTE: For this Train to fit the Width of the applet window was changed to 1016.

Section 9.7 Super Class Identifiers

// Java0921.java // This program demonstrates inheritance of three // sub classes with the same <Animal> superclass. public class Java0921 { public static void main(String args[]) Cat tiger = new Cat("Tiger"); System.out.println(tiger.getType()); Bird eagle = new Bird("Eagle"); System.out.println(eagle.getType()); Fish shark = new Fish("Shark"); System.out.println(shark.getType()); } class Animal public Animal() System.out.println( "Animal constructor called"); class Cat extends Animal protected String catType; public Cat(String ct) { catType = ct; } public String getType() { return catType; } class Bird extends Animal protected String birdType; public Bird(String bt) { birdType = bt; } public String getType() { return birdType; } class Fish extends Animal protected String fishType; public Fish(String ft) { fishType = ft; } public String getType() { return fishType; }

// Java0922.java // This program demonstrates that it is // possible to use the super class identifier // <Animal> to declare each sub class object. public class Java0922 { public static void main(String args[]) Animal tiger = new Cat("Tiger"); Animal eagle = new Bird("Eagle"); Animal shark = new Fish("Shark"); } class Animal public Animal() System.out.println( "Animal constructor called"); class Cat extends Animal protected String catType; public Cat(String ct) System.out.println("Cat constructor called"); catType = ct; class Bird extends Animal protected String birdType; public Bird(String bt) System.out.println("Bird constructor called"); birdType = bt; class Fish extends Animal protected String fishType; public Fish(String ft) System.out.println("Fish constructor called"); fishType = ft;

// Java0923.java // This program adds <getType> methods for each // one of the three <Animal> sub classes. // The program does not compile, because // <getType> is not an <Animal> method. public class Java0923 { public static void main(String args[]) Animal tiger = new Cat("Tiger"); System.out.println(tiger.getType()); Animal eagle = new Bird("Eagle"); System.out.println(eagle.getType()); Animal shark = new Fish("Shark"); System.out.println(shark.getType()); } class Animal public Animal() System.out.println( "Animal constructor called"); class Cat extends Animal protected String catType; public Cat(String ct) { catType = ct; } public String getType() { return catType; } class Bird extends Animal protected String birdType; public Bird(String bt) { birdType = bt; } public String getType() { return birdType; } class Fish extends Animal protected String fishType; public Fish(String ft) { fishType = ft; } public String getType() { return fishType; }

// Java0924.java // This program solves the problem of the previous // program. Each <getType> method is "cast" to // the appropriate class. public class Java0924 { public static void main(String args[]) Animal tiger = new Cat("Tiger"); System.out.println(((Cat)tiger).getType()); Animal eagle = new Bird("Eagle"); System.out.println(((Bird)eagle).getType()); Animal shark = new Fish("Shark"); System.out.println(((Fish)shark).getType()); } class Animal public Animal() System.out.println( "Animal constructor called"); class Cat extends Animal protected String catType; public Cat(String ct) { catType = ct; } public String getType() { return catType; } class Bird extends Animal protected String birdType; public Bird(String bt) { birdType = bt; } public String getType() { return birdType; } class Fish extends Animal protected String fishType; public Fish(String ft) { fishType = ft; } public String getType() { return fishType; }

// Java0925.java // This program demonstrates that it is possible // to use the super class identifier // <Object> to declare each sub class object. public class Java0925 { public static void main(String args[]) Object tiger = new Cat("Tiger"); Object eagle = new Bird("Eagle"); Object shark = new Fish("Shark"); } class Cat extends Object protected String catType; public Cat(String ct) System.out.println("Cat constructor called"); catType = ct; class Bird extends Object protected String birdType; public Bird(String bt) System.out.println("Bird constructor called"); birdType = bt; class Fish extends Object protected String fishType; public Fish(String ft) System.out.println("Fish constructor called"); fishType = ft;

// Java0926.java // This program demonstrates that the <Object> // super class can be used like the <Animal> super // class with the proper type casting. // It also shows that it is not necessary // to use "extends Object". public class Java0926 { public static void main(String args[]) Object tiger = new Cat("Tiger"); System.out.println(((Cat)tiger).getType()); Object eagle = new Bird("Eagle"); System.out.println(((Bird)eagle).getType()); Object shark = new Fish("Shark"); System.out.println(((Fish)shark).getType()); } class Cat protected String catType; public Cat(String ct) { catType = ct; } public String getType() { return catType; } class Bird protected String birdType; public Bird(String bt) { birdType = bt; } public String getType() { return birdType; } class Fish protected String fishType; public Fish(String ft) { fishType = ft; } public String getType() { return fishType; }

“The mother of all classes” All classes automatically inherit from The Object Class Math String System Rational All other classes