Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Similar documents
CS 112 Introduction to Programming. (Spring 2012)

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

We are on the GUI fast track path

Inheritance. Transitivity

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Java Magistère BFA

Inheritance and Polymorphism

Arrays Classes & Methods, Inheritance

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Inheritance Advanced Programming ICOM 4015 Lecture 11 Reading: Java Concepts Chapter 13

Inheritance and Interfaces

What is Inheritance?

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

Java Object Oriented Design. CSC207 Fall 2014

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Example: Count of Points

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Building custom components IAT351

ITI Introduction to Computing II

More On inheritance. What you can do in subclass regarding methods:

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Java for Non Majors Spring 2018

AP CS Unit 6: Inheritance Notes

ITI Introduction to Computing II

Rules and syntax for inheritance. The boring stuff

CMSC 132: Object-Oriented Programming II

Contents Chapter 1 Introduction to Programming and the Java Language

Making New instances of Classes

CS 180 Final Exam Review 12/(11, 12)/08

Abstract Classes and Interfaces

CIS 110: Introduction to computer programming

First IS-A Relationship: Inheritance

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Programming overview

Class, Variable, Constructor, Object, Method Questions

Object-Oriented Concepts

INHERITANCE. Spring 2019

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Inheritance and Polymorphism. CS180 Fall 2007

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

ECE 122. Engineering Problem Solving with Java

JAVA MOCK TEST JAVA MOCK TEST II

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

Inheritance and Polymorphism

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Practice for Chapter 11

5. In JAVA, is exception handling implicit or explicit or both. Explain with the help of example java programs. [16]

CS111: PROGRAMMING LANGUAGE II

Inheritance (Part 2) Notes Chapter 6

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CS 11 java track: lecture 3

Inheritance (Part 5) Odds and ends

Exercise: Singleton 1

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

Inheritance -- Introduction

OVERRIDING. 7/11/2015 Budditha Hettige 82

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

C++ Important Questions with Answers

The class Object. Lecture CS1122 Summer 2008

Language Features. 1. The primitive types int, double, and boolean are part of the AP

CS/ENGRD 2110 SPRING 2018

Intro to Computer Science 2. Inheritance

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Inheritance. OOP: Inheritance 1

CS-202 Introduction to Object Oriented Programming

Chapter 5. Inheritance

Introduction to Computation and Problem Solving

1 Shyam sir JAVA Notes

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

C09: Interface and Abstract Class and Method

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Object Orientated Programming Details COMP360

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

CS260 Intro to Java & Android 03.Java Language Basics

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

ECE 122. Engineering Problem Solving with Java

BBM 102 Introduction to Programming II

Polymorphism. Agenda

Polymorphism. return a.doublevalue() + b.doublevalue();

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

Chapter 10 Inheritance. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Instance Members and Static Members

Binghamton University. CS-140 Fall Dynamic Types

Full file at Chapter 2 - Inheritance and Exception Handling

Lecture 18 CSE11 Fall 2013 Inheritance

Inheritance and Interfaces

AP CS Unit 11: Graphics and Events

First Name: AITI 2004: Exam 2 July 19, 2004

More Relationships Between Classes

Inheritance (P1 2006/2007)

Transcription:

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