Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

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

Inheritance and Polymorphism

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Inheritance. Transitivity

Chapter 14 Abstract Classes and Interfaces

OVERRIDING. 7/11/2015 Budditha Hettige 82

What is Inheritance?

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

Inheritance & Polymorphism

Object Oriented Programming. Java-Lecture 11 Polymorphism

Computer Science II (20073) Week 1: Review and Inheritance

COMP 110/L Lecture 19. Kyle Dewey

Chapter 5 Object-Oriented Programming

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Inheritance -- Introduction

Inheritance & Polymorphism

CS 251 Intermediate Programming Inheritance

Polymorphism. Arizona State University 1

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

Inheritance, Polymorphism, and Interfaces

More on Objects in JAVA TM

Object-Oriented Concepts

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

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

Programming II (CS300)

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

Java Object Oriented Design. CSC207 Fall 2014

Java Fundamentals (II)

First IS-A Relationship: Inheritance

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

CMSC 132: Object-Oriented Programming II

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

Chapter 10 Classes Continued. Fundamentals of Java

CSCE3193: Programming Paradigms

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

CS111: PROGRAMMING LANGUAGE II

CS-202 Introduction to Object Oriented Programming

Interface Class. Lecture 22. Based on Slides of Dr. Norazah Yusof

Programming in C# Inheritance and Polymorphism

Inheritance and Polymorphism. CS180 Fall 2007

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

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

Polymorphism and Inheritance

Polymorphism. Agenda

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Java Magistère BFA

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Programming overview

COMP 250 Fall inheritance Nov. 17, 2017

BBM 102 Introduction to Programming II Spring Inheritance

CS111: PROGRAMMING LANGUAGE II

More Relationships Between Classes

Inheritance and Interfaces

Introduction to Classes and Objects. David Greenstein Monta Vista High School

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

Object-Oriented Programming More Inheritance

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

C++ Important Questions with Answers

Computer Science 210: Data Structures

Java: introduction to object-oriented features

Introduction to Inheritance

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Lecture 4: Extending Classes. Concept

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

Inheritance (Part 2) Notes Chapter 6

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Building custom components IAT351

Inheritance (Outsource: )

Practice for Chapter 11

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

ECE 122. Engineering Problem Solving with Java

More about inheritance

Overriding Variables: Shadowing

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Object Orientation Fourth Story. Bok, Jong Soon

AP CS Unit 6: Inheritance Notes

Starting Out with Java: From Control Structures Through Objects Sixth Edition

The Essence of Object Oriented Programming with Java and UML. Chapter 2. The Essence of Objects. What Is an Object-Oriented System?

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

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

8. Polymorphism and Inheritance

COMP 110/L Lecture 20. Kyle Dewey

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Rules and syntax for inheritance. The boring stuff

Inheritance (Part 5) Odds and ends

INHERITANCE. Spring 2019

Inf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

COP 3330 Final Exam Review

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

CS260 Intro to Java & Android 03.Java Language Basics

Programming II (CS300)

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

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

CompSci 125 Lecture 20. Inheritance: Introduction, Overrides UML: Introduction to Class Diagrams

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

Class, Variable, Constructor, Object, Method Questions

Transcription:

Class Hierarchy and Interfaces David Greenstein Monta Vista High School

Inheritance Inheritance represents the IS-A relationship between objects. an object of a subclass IS-A(n) object of the superclass Superclass (Base class) Subclass (Derived class) The Subclass extends the Superclass

Class Hierarchy Using inheritance, a programmer can define a hierarchy of classes.

Class Hierarchy (cont) Hierarchy helps reduce duplication of code by factoring out common code from similar classes into a common superclass. abstract class Animal abstract eats() dateborn() abstract class Mammal abstract growhair() abstract getsfood() abstract class Reptile shedskin() locomotion() (concrete) class Horse eats() coattexture() (concrete) class Snake scaletype() hasbite()

Class Hierarchy (cont) Hierarchy helps reduce duplication of code by letting you write more general methods in client classes. public void dateborn(horse h) public void dateborn(dog d) public void dateborn(mammal m) Works for a Horse or Dog or any Mammal

Abstract Classes Some superclasses are generic classes used as a basis for other subclasses. These are called abstract classes (e.g. Animal). abstract class Animal abstract eats() dateborn() abstract class Mammal abstract growhair() abstract getsfood() abstract class Reptile shedskin() locomotion() (concrete) class Horse eats() coattexture() (concrete) class Snake scaletype() hasbite()

Abstract Classes (cont) Abstract classes are closer to the root of the hierarchy; they describe more abstract objects. Abstract classes must define one or more abstract methods. Conversely, any class that has one or more abstract methods must be abstract. public abstract class Animal public abstract Date dateborn(); An abstract class An abstract method

Abstract Classes (cont) Abstract classes are not instantiated as objects (no new) because they are too general. An abstract class can still define constructors (ie. implement them). A concrete class is one which has no abstract methods. Therefore, the class cannot be labeled abstract. An abstract class can be derived (extend) from a concrete class. public abstract class Animal public abstract Date dateborn(); An abstract class An abstract method

Abstract Classes (cont) If the superclass and subclass are both abstract, then the subclass implicitly inherits all of the abstract methods of the superclass. An abstract class may or may not implement the abstract methods of its superclass. If not implemented by the abstract class(es), eventually a derived class in the hierarchy must implement the abstract method. public abstract class Animal public abstract Date dateborn(); example in which abstract dateborn() not implemented public abstract class Mammal extends Animal /* abstract dateborn() not listed */ public class Horse extends Mammal Concrete class inherits and must implement abstract dateborn() is inherited public Date dateborn()

Abstract Methods Abstract methods are not implemented so they have no body, just a signature with a semicolon. Abstract methods provide the compiler an opportunity for additional error checking. public abstract class Animal public abstract Date dateborn(); Just signature; no body

Abstract Classes (cont) SWING Abstract classes SWING Concrete classes

The Object Class The superclass of all classes is the Object class from the java.lang package. The Object class is a concrete class. public class Object public String tostring... public boolean equals (Object other)... public int hashcode()... Methods overridden as necessary // a few other methods...

The Object Class (cont) All classes extend from the Object class, even the classes you write. If not explicitly coded, the class extends Object. public class MyClass extends Object // a few fields // maybe a constructor defined If not explicitly mentioned, the compiler extends Object // a few other methods...

Constructor Hierarchy Constructors are invoked through a superclass chain - up the inheritance hierarchy. Object super() Animal super() Mammal super() Horse If no constructor is explicitly defined in a class, then a no-args constructor is generated automatically by the compiler.

Implicit Constructor The no-args constructor generated by the compiler calls the constructor of the superclass (super()). Warning! The superclass must have a no-args constructor or a syntax error is generated. public class MyClass extends MySuper // a few fields /* no constructor defined */ Your code What the compiler inserts public class MyClass extends MySuper // a few other methods... // a few fields public myclass() super(); // a few other methods...

Explicit Constructor If you explicitly write a constructor in a class, then the compiler will not generate a constructor, not even a no-args constructor! But inside your constructor, a super() call might get inserted by the compiler if you do not explicitly include it. If you put super() in your first line, then the compiler adds nothing. If you do not put super() in your first line, then the compiler adds a no-args super() automatically. public class MyClass extends MySuper // One explicit constructor public myclass(int x) Compiler inserts a super() call public class myclass extends MySuper // One explicit constructor public myclass(int x) super();

super Constructor Call By default, one of the superclass s constructors is always called. (with the exception of the Object class) If you explicitly put a super in your constructor, it must be on the first line of the constructor. The super method can have parameters. Whatever parameters are in the super call, there must be a superclass constructor with the same signature. If you do not explicit call super, then the superclass s no-args constructor is called by default.

super Method Call Subclass methods can call superclass methods using super. (dot) notation. A super. method call can be called anywhere inside a subclass method and it can be done one or more times. Superclass method calls only go up one level of hierarchy. There is no such thing as super.super!! public class MySuper... public void method1(int a)... public class MyClass extends MySuper... public void method1(int a) super. notation... Even overridden methods can be called String str; super.method1(a);

Polymorphism Ensures that the correct method is called for an object of a specific type, even when that object is disguised as a reference to a more generic type, that is, the type of the object s superclass or some ancestor higher up the inheritance line. Once you define a common superclass, polymorphism is just there no need to do anything special. // client class Mammal mammal1 = new Lion(); Mammal mammal2 = new Horse(); mammal1.eats(); mammal2.eats();

Polymorphism (cont) For example, subclasses Lion and Horse each extend the Mammal class. The identifier is Mammal, but ( under the hood ) eats() still executes the correct Lion version. public class Lion extends Mammal public void eats() Uses Lion s eats method public abstract class Mammal public void eats() // client class Mammal mammal1 = new Lion(); Mammal mammal2 = new Horse(); mammal1.eats(); mammal2.eats(); public class Horse extends Mammal public void eats() Uses Horse s eats method

Non-static vs. Static Methods Static methods are bound to the identifier s data type (ie. class). The fields and methods are determined at compile time. public class Super public static void staticmethod() public void nonstaticmethod() public class Sub extends Super public static void staticmethod() public void nonstaticmethod() Identifier Data type Client class Super super1 = new Sub(); Sub sub1 = new Sub(); super1.staticmethod(); sub1.staticmethod(); sub.staticmethod(); sub.nonstaticmethod(); Polymorphism does not work here!!

Non-static vs. Static Methods Static methods are bound to the identifier s data type (ie. class). The fields and method types are determined at compile time. Non-static methods are bound to the object s data type. The field and method types are determined during execution (ie. the new object s data type). public class Super public static void staticmethod() public void nonstaticmethod() Client class Super super1 = new Sub(); Sub sub1 = new Sub(); super1.staticmethod(); sub1.staticmethod(); Object s Data type public class Sub extends Super public static void staticmethod() public void nonstaticmethod() super1.nonstaticmethod(); sub.nonstaticmethod(); Polymorphism works here!!

Interfaces An interface in Java is like an abstract class, except: it has no constructors. all of its methods are implicitly public abstract all of its fields are implicitly public static final public interface Herd void setherdsize(int num); int getherdsize(); Implied public abstract methods

Interfaces (cont) A concrete class that implements an interface must implement every method mentioned in the interface. Like abstract methods, interfaces provide the compiler an opportunity for additional error checking. public interface Herd void setherdsize(int num); int getherdsize(); Class Horse must implement all the abstract methods mentioned in Herd public class Horse implements Herd public void setherdsize(int num) public int getherdsize()

Interfaces (cont) Abstract classes can also implement interfaces. An abstract class is not obligated to implement the abstract methods of the interface, but they are allowed to implement. Concrete classes must implement all abstract methods inherited from their superclass (extends) and their interfaces (implements). public interface Herd void setherdsize(int num); int getherdsize(); Abstract classes may implement the method or let it pass to derived class. public abstract class Mammal implements Herd public void setherdsize(int num) // int getherdsize() passed to derived class

Interfaces (cont) Interfaces cannot extend classes nor implement other interfaces. Interfaces are not in the hierarchy of classes. On the other hand, interfaces can extend other interfaces creating their own hierarchy.

Interfaces (cont) Concrete classes must implement all the abstract methods of the interface. Abstract classes do not have to implement the abstract methods of the interface. public interface MouseListener extends EventListener void mouseclicked(mouseevent e); void mouseentered(mouseevent e); Abstract methods Implemented (concrete) methods public class MyPanel implements MouseListener, MouseMotionListener public void mouseclicked(mouseevent e) public void mouseentered(mouseevent e)

Interfaces (cont) Concrete and abstract classes can implement more than one interface. public interface MouseMotionListener extends EventListener void mousedragged(mouseevent e); public interface MouseListener extends EventListener void mouseclicked(mouseevent e); void mouseentered(mouseevent e); public class MyPanel implements MouseListener, MouseMotionListener public void mouseclicked(mouseevent e) public void mousedragged(mouseevent e)

Interfaces (cont) Like an abstract class, an interface supplies a secondary data type to objects of a class that implements that interface. You can declare variables and parameters of an interface type. Herd flicker = new Horse(); Polymorphism fully applies to objects disguised as interface types.

Overriding Methods To override a method is to redefine (reimplement) a superclass method in a subclass using the same signature. Overridden public abstract class Animal public abstract void eats(); public abstract class Mammal extends Animal public void eats() public class Lion extends Mammal public void eats() Superclass implementation Overridden public class Horse extends Mammal public void eats()

Questions?