Download presentation
Presentation is loading. Please wait.
Published byJohan Maddern Modified over 10 years ago
1
Classes and SubClasses Super Parent Sub Child IS - A
2
Classes and SubClasses Person Age gender Student Is a Has a Address Grade Has a Student Inherits Everything from Person
3
Classes and SubClasses Super references parent class Constructor: public Student(Name n, Address a, int age, char g, int grade) { super(n, a, age, g); //calls parent’s constructor myGrade = gr; }
4
Classes and SubClasses Hierarchies: Keep common tasks at highest level Person Age gender Student Address Grade Has a
5
Classes and SubClasses Object Class //all classes are children of this class Highest level class. All other classes are children toString and equals are methods of Object that are often overidden *************************************************************** Object myObject Object2Object3
6
Classes and SubClasses Abstract Class: Created solely to derive other classes. Holds information and methods shared by all children. If children don’t need shared data types, an interface might be more appropriate. (more about this later) *************************************************************** Abstract Class: passes information, but can not be instantiated Vehicle Car BoatPlane
7
Classes and SubClasses Indirect access: We usually declare instance variables in a class as private. If this is the case, we will not be able to access the parent data directly from the subclasses. We have to use the accessor methods. studentOne.getName() //calls the parent getName, //unless student has //overridden that method. Returns the student’s name even though the actual method is defined in Person
8
Classes and SubClasses Polymorphism Reference to object can refer to different object subclasses. Vertibrate Horse Camel Vertibrate[] vertArray = new Vertibrate[3]; vertArray[0] = new Vertibrate(); vertArray[1] = new Horse(); vertArray[2] = new Camel(); for (int I = 0; i<vertArray.length;i++) vertArray[i].doIt(); // doIt() could do three different things depending on how it was defined in each of Vertibrate, Horse and Camel. Usually what method is called is determined at compile time, but sometimes this can not be determined until run time. This is called late binding.
9
public abstract class Graf { public void draw() // every child needs this defined { } ******************************************************** public class GrafString extends Graf { public void draw() { myGraphics2D.drawString(myString, (int) myXYPosit … } ************************************* public class GrafPoint extends Graf { public void draw() { myGraphics2D.drawString(“.”, (int) myXYPosit … } *************************************************************** // myGrafs is an ArrayList of Grafs defined by user for (int i = 0; i < myGrafs.size(); i++) myGrafs.get(i).draw(); //Don’t know what this is yet, //but we are going to draw it. Classes and SubClasses Polymorphism Reference to object can refer to different object subclasses. Graf DotPlot Histo Late binding example.
10
public interface Graf { public void draw() ; // every implementation needs this } ******************************************************** public class GrafString implements Graf { public void draw() { myGraphics2D.drawString(myString, (int) myXYPosit … } ************************************* public class GrafPoint implements Graf { public void draw() { myGraphics2D.drawString(“.”, (int) myXYPosit … } *************************************************************** // myGrafs is an ArrayList of Grafs defined by user for (int i = 0; i < myGrafs.size(); i++) myGrafs.get(i).draw(); //Don’t know what this is yet, //but we are going to draw it. Classes and SubClasses Polymorphism and interfaces. If you don’t need to pass down constructors or other methods, an interface may be more desirable than an abstract class Graf DotPlot Histo
11
Creating Subclasses Type in listings 7.1 through 7.3 and run them. Create a JAVAText object that stores the number of pages in your JAVA book and the number of definitions in the glossary.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.