logging in or signing up 11 Inheritance Michelangelo Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINTLite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 2969 Category: Education License: All Rights Reserved Like it (3) Dislike it (0) Added: February 06, 2008 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... By: sweet11 (14 month(s) ago) good Saving..... Post Reply Close Saving..... Edit Comment Close By: MrGopiprod (25 month(s) ago) please send this ppt to my mail id (gopiprod_gopal@yahoo.com ) pls pls.... i need today....pls.. Saving..... Post Reply Close Saving..... Edit Comment Close By: sumitharr (31 month(s) ago) please send me this ppt sumitharr@gmail.com Saving..... Post Reply Close Saving..... Edit Comment Close Premium member Presentation Transcript Inheritance: Inheritance Inheritance in the Animal Kingdom: Inheritance in the Animal Kingdom animal mammal insect bear cat dog ant cockroach tic instances classesInheritance Among Artifacts: Vehicles: Inheritance Among Artifacts: Vehicles vehicle car truck sedan van wagon pickup semi tow bus suvInheritance in Software: Inheritance in Software Inheritance is a means of organizing related classes Classes are organized in a hierarchy (subclasses and superclasses) Subclasses inherit the attributes and behavior of superclasses We can eliminate redundant data and code by factoring them up the hierarchy We can get behavior for free by subclassing Example: java.io File Classes: Example: java.io File Classes Object InputStream OutputStream FileInputStream FileOutputStream ObjectInputStream ObjectOutputStream FileExample: java.awt GUI Component Classes: Example: java.awt GUI Component Classes Component TextComponent MenuComponent Container Button Canvas Checkbox CheckboxGroup Choice List Scrollbar TextArea TextField MenuItem MenuBar Menu PopupMenu Window Panel Applet Dialog Frame FileDialog CheckboxMenuItemExample: ioutil Classes: Example: ioutil Classes Object TextReader KeyboardReader FileReader Component Container JComponent JTextComponent JTextField IntegerField DoubleFieldGetting Behavior for Free: Getting Behavior for Free JFrame provides behavior for any GUI-based application window To obtain this behavior and add other behavior specific to our particular application’s window, we build a subclass of JFrame by extending it import javax.swing.JFrame; public class EmployeeManagerView extends JFrame{ … }Getting Behavior for Free: Getting Behavior for Free JTextField handles input and output of strings When used with integers, the client must do type conversions both ways and also handle format exceptions We would like to have an IntegerField that works just for integer I/O and hides the messy detailsUsing JTextField: Using JTextField JTextField field = new JTextField("0"); … … String text = field.getText(); int number = Integer.parseInt(text); // Throws exception // if format is bad … field.setText("" + number);Using Both Types of Fields: Using Both Types of Fields JTextField field = new JTextField("0"); … … String text = field.getText(); int number = Integer.parseInt(text); // Throws exception // if format is bad … field.setText(("" + number); IntegerField field = new IntegerField(0); … … int number = 0; if (field.isValid()) number = field.getNumber(); … field.setNumber(number);Defining IntegerField: Defining IntegerField package ioutil; import javax.swing.*; public class IntegerField extends JTextField { public IntegerField (int num){ setText ("" + num); } public void setNumber (int num){ setText ("" + num); } … }Defining IntegerField: Defining IntegerField package ioutil; import javax.swing.*; public class IntegerField extends JTextField { … public int getNumber(){ int num; try{ num = Integer.parseInt(getText().trim()); }catch(NumberFormatException e){ num = 0; setText ("" + num); } return num; } }Defining IntegerField: Defining IntegerField package ioutil; import javax.swing.*; public class IntegerField extends JTextField { … public boolean isValid(){ try{ int num = Integer.parseInt(getText().trim()); return true; }catch(NumberFormatException e){ return false; } } }Abstract and Concrete Classes: Abstract and Concrete Classes An abstract class defines data and methods common to all subclasses, but is not instantiated A concrete class inherits some data and methods, defines others, and is instantiated Example: ioutil Classes: Example: ioutil Classes Object TextReader KeyboardReader FileReader Component Container JComponent JTextComponent JTextField IntegerField DoubleField Abstract class Concrete classExample: Geometric Shapes: Example: Geometric Shapes Rectangles, circles, and triangles have some attributes in common: A position (x,y) in coordinate space A fill attribute A color They also have some common behavior: Translate (move position)Example: Geometric Shapes: Example: Geometric Shapes In addition to the common attributes, triangles have three vertices circles have a radius Methods that vary with the type of shape: Draw Scale Test for containment of a point (x,y) The Shape Interface: The Shape Interface We specify all of the methods that should be defined for any shape in a Shape interface Any particular class, such as Rectangle, will implement this interface and include all of the Shape methodsThe Shape Interface: The Shape Interface import java.awt.Graphics; // For Graphics class public interface Shape{ public void translate(int xDistance, int yDistance); public void scale(double xFactor, double yFactor); public void draw(Graphics g); public boolean containsPoint(int x, int y); }The AbstractShape Class: The AbstractShape Class The Shape interface specifies the common behavior of all implementing classes The AbstractShape class includes code for representing the common attributes (data) and implementing the behavior (methods)The Shape Hierarchy: The Shape Hierarchy Object AbstractShape Rectangle Shape Triangle Circle Implementing Classes: Implementing Classes import java.awt.*; // For Color and Graphics classes public class Circle extends AbstractShape{ … } import java.awt.*; // For Color and Graphics classes public class Rectangle extends AbstractShape{ … } import java.awt.*; // For Color and Graphics classes public class Triangle extends AbstractShape{ … } import java.awt.*; // For Color and Graphics classes abstract public class AbstractShape implements Shape{ … }The AbstractShape Class: The AbstractShape Class import java.awt.*; // For Color and Graphics classes abstract public class AbstractShape implements Shape{ protected int x, y; protected Color color; protected boolean fill; public AbstractShape(int x, int y, Color c, boolean f){ this.x = x; this.y = y; color = c; fill = f; } } protected variables are visible to all subclassesThe AbstractShape Class: The AbstractShape Class import java.awt.*; // For Color and Graphics classes abstract public class AbstractShape implements Shape{ public void translate(int xDistance, int yDistance){ x += xDistance; y += yDistance; } // Other implemented methods abstract public void draw(Graphics g); // etc. } An abstract method must be implemented in all subclassesThe Circle Class: The Circle Class import java.awt.*; // For Color and Graphics classes public class Circle extends AbstractShape{ private int radius; public Circle(int x, int y, int r, Color c, boolean f){ super(x, y, c, f); radius = r; } public void draw(Graphics g){ <draw a circle> } } super invokes a constructor in the superclassThe Triangle Class: The Triangle Class import java.awt.*; // For Color and Graphics classes public class Triangle extends AbstractShape{ private int vx2, vy2, vx3, vy3; … public String toString(){ return super.tostring() + <build a string containing the vertices>; } } super.<method> invokes the same method in the superclassFinding the Right Method: Finding the Right Method When a message is sent to an object, the JVM looks for its method in the receiver object’s class If it’s not found there, the JVM looks in its superclass Etc., until the method is not found in Object, whereupon an exception is thrown (actually, some of these errors are caught at compile time)Using Inheritance: Using Inheritance Sometimes you’ll spot the chance to extend another class immediately (the GUI classes are good examples of this) Most of the time, you’ll develop a bunch of classes and realize only later that they have redundancies that can be factored into common superclasses (refactoring) You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
11 Inheritance Michelangelo Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINTLite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 2969 Category: Education License: All Rights Reserved Like it (3) Dislike it (0) Added: February 06, 2008 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... By: sweet11 (14 month(s) ago) good Saving..... Post Reply Close Saving..... Edit Comment Close By: MrGopiprod (25 month(s) ago) please send this ppt to my mail id (gopiprod_gopal@yahoo.com ) pls pls.... i need today....pls.. Saving..... Post Reply Close Saving..... Edit Comment Close By: sumitharr (31 month(s) ago) please send me this ppt sumitharr@gmail.com Saving..... Post Reply Close Saving..... Edit Comment Close Premium member Presentation Transcript Inheritance: Inheritance Inheritance in the Animal Kingdom: Inheritance in the Animal Kingdom animal mammal insect bear cat dog ant cockroach tic instances classesInheritance Among Artifacts: Vehicles: Inheritance Among Artifacts: Vehicles vehicle car truck sedan van wagon pickup semi tow bus suvInheritance in Software: Inheritance in Software Inheritance is a means of organizing related classes Classes are organized in a hierarchy (subclasses and superclasses) Subclasses inherit the attributes and behavior of superclasses We can eliminate redundant data and code by factoring them up the hierarchy We can get behavior for free by subclassing Example: java.io File Classes: Example: java.io File Classes Object InputStream OutputStream FileInputStream FileOutputStream ObjectInputStream ObjectOutputStream FileExample: java.awt GUI Component Classes: Example: java.awt GUI Component Classes Component TextComponent MenuComponent Container Button Canvas Checkbox CheckboxGroup Choice List Scrollbar TextArea TextField MenuItem MenuBar Menu PopupMenu Window Panel Applet Dialog Frame FileDialog CheckboxMenuItemExample: ioutil Classes: Example: ioutil Classes Object TextReader KeyboardReader FileReader Component Container JComponent JTextComponent JTextField IntegerField DoubleFieldGetting Behavior for Free: Getting Behavior for Free JFrame provides behavior for any GUI-based application window To obtain this behavior and add other behavior specific to our particular application’s window, we build a subclass of JFrame by extending it import javax.swing.JFrame; public class EmployeeManagerView extends JFrame{ … }Getting Behavior for Free: Getting Behavior for Free JTextField handles input and output of strings When used with integers, the client must do type conversions both ways and also handle format exceptions We would like to have an IntegerField that works just for integer I/O and hides the messy detailsUsing JTextField: Using JTextField JTextField field = new JTextField("0"); … … String text = field.getText(); int number = Integer.parseInt(text); // Throws exception // if format is bad … field.setText("" + number);Using Both Types of Fields: Using Both Types of Fields JTextField field = new JTextField("0"); … … String text = field.getText(); int number = Integer.parseInt(text); // Throws exception // if format is bad … field.setText(("" + number); IntegerField field = new IntegerField(0); … … int number = 0; if (field.isValid()) number = field.getNumber(); … field.setNumber(number);Defining IntegerField: Defining IntegerField package ioutil; import javax.swing.*; public class IntegerField extends JTextField { public IntegerField (int num){ setText ("" + num); } public void setNumber (int num){ setText ("" + num); } … }Defining IntegerField: Defining IntegerField package ioutil; import javax.swing.*; public class IntegerField extends JTextField { … public int getNumber(){ int num; try{ num = Integer.parseInt(getText().trim()); }catch(NumberFormatException e){ num = 0; setText ("" + num); } return num; } }Defining IntegerField: Defining IntegerField package ioutil; import javax.swing.*; public class IntegerField extends JTextField { … public boolean isValid(){ try{ int num = Integer.parseInt(getText().trim()); return true; }catch(NumberFormatException e){ return false; } } }Abstract and Concrete Classes: Abstract and Concrete Classes An abstract class defines data and methods common to all subclasses, but is not instantiated A concrete class inherits some data and methods, defines others, and is instantiated Example: ioutil Classes: Example: ioutil Classes Object TextReader KeyboardReader FileReader Component Container JComponent JTextComponent JTextField IntegerField DoubleField Abstract class Concrete classExample: Geometric Shapes: Example: Geometric Shapes Rectangles, circles, and triangles have some attributes in common: A position (x,y) in coordinate space A fill attribute A color They also have some common behavior: Translate (move position)Example: Geometric Shapes: Example: Geometric Shapes In addition to the common attributes, triangles have three vertices circles have a radius Methods that vary with the type of shape: Draw Scale Test for containment of a point (x,y) The Shape Interface: The Shape Interface We specify all of the methods that should be defined for any shape in a Shape interface Any particular class, such as Rectangle, will implement this interface and include all of the Shape methodsThe Shape Interface: The Shape Interface import java.awt.Graphics; // For Graphics class public interface Shape{ public void translate(int xDistance, int yDistance); public void scale(double xFactor, double yFactor); public void draw(Graphics g); public boolean containsPoint(int x, int y); }The AbstractShape Class: The AbstractShape Class The Shape interface specifies the common behavior of all implementing classes The AbstractShape class includes code for representing the common attributes (data) and implementing the behavior (methods)The Shape Hierarchy: The Shape Hierarchy Object AbstractShape Rectangle Shape Triangle Circle Implementing Classes: Implementing Classes import java.awt.*; // For Color and Graphics classes public class Circle extends AbstractShape{ … } import java.awt.*; // For Color and Graphics classes public class Rectangle extends AbstractShape{ … } import java.awt.*; // For Color and Graphics classes public class Triangle extends AbstractShape{ … } import java.awt.*; // For Color and Graphics classes abstract public class AbstractShape implements Shape{ … }The AbstractShape Class: The AbstractShape Class import java.awt.*; // For Color and Graphics classes abstract public class AbstractShape implements Shape{ protected int x, y; protected Color color; protected boolean fill; public AbstractShape(int x, int y, Color c, boolean f){ this.x = x; this.y = y; color = c; fill = f; } } protected variables are visible to all subclassesThe AbstractShape Class: The AbstractShape Class import java.awt.*; // For Color and Graphics classes abstract public class AbstractShape implements Shape{ public void translate(int xDistance, int yDistance){ x += xDistance; y += yDistance; } // Other implemented methods abstract public void draw(Graphics g); // etc. } An abstract method must be implemented in all subclassesThe Circle Class: The Circle Class import java.awt.*; // For Color and Graphics classes public class Circle extends AbstractShape{ private int radius; public Circle(int x, int y, int r, Color c, boolean f){ super(x, y, c, f); radius = r; } public void draw(Graphics g){ <draw a circle> } } super invokes a constructor in the superclassThe Triangle Class: The Triangle Class import java.awt.*; // For Color and Graphics classes public class Triangle extends AbstractShape{ private int vx2, vy2, vx3, vy3; … public String toString(){ return super.tostring() + <build a string containing the vertices>; } } super.<method> invokes the same method in the superclassFinding the Right Method: Finding the Right Method When a message is sent to an object, the JVM looks for its method in the receiver object’s class If it’s not found there, the JVM looks in its superclass Etc., until the method is not found in Object, whereupon an exception is thrown (actually, some of these errors are caught at compile time)Using Inheritance: Using Inheritance Sometimes you’ll spot the chance to extend another class immediately (the GUI classes are good examples of this) Most of the time, you’ll develop a bunch of classes and realize only later that they have redundancies that can be factored into common superclasses (refactoring)