1 / 42

Chapter 9 Slides

Exposure Java-A 2007. Chapter 9 Slides. Inheritance and Composition. PowerPoint Presentation created by: Mr. John L. M. Schram. From Materials Created by Mr. Leon Schram. Inheritance. Inheritance is the process of using features (both attributes and methods) from an existing class.

yachi
Download Presentation

Chapter 9 Slides

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exposure Java-A 2007 Chapter 9 Slides Inheritance and Composition PowerPoint Presentation created by: Mr. John L. M. Schram From Materials Created by Mr. Leon Schram

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

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

  4. // Java0901.java // This program demonstrates fundamental inheritance with <extends>. // There are no constructors yet, and the private data has no value. 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"); } }

  5. // 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()); } }

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

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

  8. // 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); } }

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

  10. // 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); } }

  11. // 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()); } }

  12. // 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()); } }

  13. // 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()); } }

  14. // Java0909.java // This program demonstrates inheritance at three levels.

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

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

  17. Reptile Extinct Animal Mammal Dinosaur Dog Terrier

  18. class: Car Contained Classes: Engine Tire 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”

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

  20. // Java0910.java // This program uses an <Engine> object in a "has-a" composition relationship.

  21. Jack-O'-lantern Case Study

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

  23. // 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); } }

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

  25. The Train Case Study

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

  27. // 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); } }

  28. // 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); } }

  29. // 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); } }

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

  31. Super Class Identifiers

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

More Related