11 Inheritance

56 %
44 %
Information about 11 Inheritance
Education

Published on February 6, 2008

Author: Michelangelo

Source: authorstream.com

Inheritance:  Inheritance Inheritance in the Animal Kingdom:  Inheritance in the Animal Kingdom animal mammal insect bear cat dog ant cockroach tic instances classes Inheritance Among Artifacts: Vehicles:  Inheritance Among Artifacts: Vehicles vehicle car truck sedan van wagon pickup semi tow bus suv Inheritance 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 File Example: 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 CheckboxMenuItem Example: ioutil Classes:  Example: ioutil Classes Object TextReader KeyboardReader FileReader Component Container JComponent JTextComponent JTextField IntegerField DoubleField Getting 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 details Using 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 class Example: 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 methods The 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 subclasses The 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 subclasses The 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 superclass The 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 superclass Finding 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)

Add a comment

Related presentations

Related pages

Chapter 11 Inheritance and Polymorphism - School of ...

123 Chapter 11 Inheritance and Polymorphism Composition is one example of code reuse. We have seen how classes can be composed with other classes to
Read more

Shining Inheritance - Episode 11 - Watch Full Episodes ...

Watch full episodes free online. Shining Inheritance - Eun Sung is transferred to the company factory while Sung Hee and Seung Mi tell Pyung Joong that Eun ...
Read more

Inheritance - Wikipedia, the free encyclopedia

Inheritance is the practice of passing on property, titles, ... The order of inheritance is set out in Numbers 27:7-11: a man's sons inherit first, ...
Read more

Use C++11 Inheritance Control Keywords to Prevent ...

For more than 30 years, C++ got along without inheritance control keywords. It wasn’t easy, to say the least. Disabling further derivation of a class was ...
Read more

11.2 — Basic inheritance in C++ « Learn C++

Now that we’ve talked about what inheritance is in an abstract sense, let’s talk about how it’s used within C++. Inheritance in C++ takes place ...
Read more

C# 11 inheritance - YouTube

1- Course C# Resala Create Desktop App + variable + Reserved Words + Built in DataType + Constant - Duration: 35:19. Adel Sabour 46,457 views
Read more

VB.NET Language - Lesson 11: Inheritance - Function X

As you may have guessed, in order to implement inheritance, you must first have a class that provides the fundamental definition or behavior you ...
Read more

11.3.2 Inheritance (C#)

11.3.2 Inheritance. 11.3.1 Value semantics. 11.3.2 Inheritance. 11.3.3 Assignment. 11.3.4 Default values. 11.3.5 Boxing and unboxing. 11.3.6 Meaning of this.
Read more

Inheritance - Seite 11 - MMOBase Forum - Computer- und ...

Registriert seit 13.11.2015 Beiträge 2 Mag ich 0 Wurde 0 mal in 0 Beiträgen positiv bewertet.
Read more

Inheritance

Hey, ich würde mich gerne bei euch Bewerben, erst einmal zu meiner Person, ich heiße Martin, bin 24 Jahre Alt, lebe nun in Portugal und arbeite für ein ...
Read more