Overview Lecture 7: Inheritance and GUIs Written by: Daniel Dalevi Inheritance Subclasses and superclasses Java keywords Interfaces and inheritance The JComponent class Casting The cosmic superclass Object and its methods Inheritance A mechanism for extending classes by adding and fields and methods. Example Animal (Parent) extends is an Dog Bird is a - relationship Subclass (Child) bark() fly() The Bird extends Animal The Bird is an Animal The Bird is a subclass of Animal The Animal is a superclass of Dog and Bird 1
The super class: can contain both abstract and non-abstract methods. is more general than its subclasses can be instantiated if no methods are abstract abstract void method1() void method2() int method3() String method4() method5() Inheritance public class SuperClassName public abstract void method1(); public void method2() // contains implementation for method2 // instance fields private int myfield; public class SubClassName extends SuperClassName public void method1() // contains implementation for method1 // instance fields JAVA keywords Inheritance and interfaces Keyword public private protected final static abstract Description Accessible for all classes. Accessible for the class and its inner classes. Accessible for the class, its sub-classes, inner classes and classes within the same package. Used to declare constants. Used to declare class methods and class variables. Used when declare methods that must be implemented by any class that implements an interface or any class that extends another class. Interfaces do not contain any implementation (all methods are abstract and empty). A superclass, on the other hand, can contain both abstract and non-abstract methods. The methods in the super class may contain a default implementation that can be redefined in the subclasses. The redefinition of a method in a subclass is referred to as overriding the method. 2
In Java all classes extend Object A Part of the Hierarchy of JComponent in Swing (see book page 384). JComponent JPanel JTextComponent JLabel AbstractButton JTextField JTextArea JToggleButton JButton (Picture form http://java.sun.com/ ) For example, a JComponent is an Object. JCheckBox JRadioButton Revisit: Frames and Components Need two standard classes: JComponent (This is used to draw on) JFrame (The window that is displayed) Subclasses override paintcomponent method ( which is abstract ). The add-method in the JFrame class can add any component (your own and standards that extend JComponent). Invoking methods from the superclass Sometimes you may want to call a method as it is defined in the superclass This is done using the super keyword. Remember how this could be used to refer to variables and methods of the class. To call the constructor of the superclass use super() or super( parameters ) 3
Converting between subclass and superclass types Possible to convert subclass references directly to superclass references. Bird bird = new Bird(); Animal animal = bird; The other way around needs casting Animal animal = new Bird(); Bird bird = (Bird) animal; The instanceof operator can be used to test if an object is an instance of a type. if( animal instanceof Bird ) Bird bird = (Bird) animal; The RandomDistribution example RandomDistribution void setseed( int s ) abstract double getrandomnumber() BinomialDistribution void setseed( int s ); double getrandomnumber() Specific implementation for a binomial distribution NormalDistribution Same for all random distributions void setseed( int s ) double getrandomnumber() Specific implementation for a normal distribution The superclass The subclass public class RandomDistribution RandomDistribution() seed = -1; public void setseed( int seed ) this.seed = seed; abstract public double getrandomnumber(); private int seed; public class BinomialDistribution extends RandomDistribution BinomialDistribution() name = Binomial ; super(); public double getrandomnumber() double randomnumber = 0; //...... // Code specific for binomial class // should be added here! //...... return randomnumber; 4
Distribution determined at run-time (polymorphism) Method Methods of Object Purpose public static void main(string[] args) Scanner in = new Scanner( System.in ); System.out.println( "Input distribution name (binomial or normal): "); String dist = in.nextline(); RandomDistribution rand = null; if( dist.equals( binomial ) ) rand = new BinomialDistribution(); Determined at run-time! else if( dist.equals( normal ) ) rand = new NormalDistribution(); if( rand!= null ) rand.setseed( 20 ); // Print 5 random numbers for( int i = 0; i < 5; ++i ) System.out.println( rand.getrandomnumber() ); String tostring() boolean equals( Object otherobject) Object clone() public boolean equals( Objects o ) Planet other = (Planet) o; return name.equals( other.name ) && radius == other.radius; Returns a string representation of the object Tests whether the object equals another object Makes a full copy of an object 5