Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.
|
|
- Barnaby Bradford
- 6 years ago
- Views:
Transcription
1 Outline Inheritance Class Extension Overriding Methods Inheritance and Constructors Polymorphism Abstract Classes Interfaces 1
2 OOP Principles Encapsulation Methods and data are combined in classes Not unique to OOP Information Hiding Implementation of a class can be changed without affecting clients Not unique to OOP (Ada / Modula provided information inheritance) Inheritance Classes can be adapted through inheritance Polymorphism Invocation based on the dynamic type of a reference Substitution rule: Subtypes must fulfill the contract of its base type 2
3 Inheritance Inheritance in Java New classes can be defined by extending existing classes The extension inherits all fields and methods from the base class New fields, methods and constructors may be added Example Figure Contains position (x, y) Provides method move(dx, dy), getx(), gety() Circle Additional field: radius Additional method: getradius() Rectangle Additional fields: width / height Additional method: getarea() 3
4 Inheritance in Java Figure class Figure { private int x, y; public int getx() { return x; public int gety() { return y; public void move(int dx, int dy) { x += dx; y += dy; Circle Rectangle class Circle extends Figure { private int radius; class Rectangle extends Figure { private int width, height; public int getradius(){ return radius; public int getarea() { return width * height; 4
5 Inheritance in Java Extension Class Rectangle inherits the fields x and y Class Rectangle declares new fields width and height Class Rectangle inherits the method move(int dx, int dy) Class Rectangle declares the new method getarea() Rectangle r = new Rectangle(); r.x = 5; r.y = 5; r.width = 20; r.height = 10; r.move(10, 5); r.getarea(); r int x = 515 int y = 510 int width = 20 int height = 10 5
6 Inheritance in Java Extension Keyword extends class D extends B { Fields and methods are inherited, i.e. are available in the extension and can be accessed / invoked Only one base class can be specified to inherit from; if no base class is specified class Object is the base class Object Animal Figure Mammal Rectangle Circle 6
7 Inheritance in General Code Inheritance (Extension, Subclassing) Figure has fields x/y and method move Rectangle inherits x/y/move and adds additional fields and methods Figure.Attributes Rectangle.Attributes Interface Inheritance (Specialization, Subtyping) Every rectangle is a figure, but not every figure is a rectangle, i.e. interface specification is reused (and specialized) Type defines a set of objects: Figures Rectangles Implementation Aspect Design Aspect 7
8 Inheritance in General Code Inheritance (Extension, Subclassing) Subclasses add new fields & methods Subclasses have more (specialized) attributes & operations Circle Figure Implementation Aspect Rectangle Interface Inheritance (Specialization, Subtyping) Types define a set of objects Subtypes are specializations Figure Design Aspect Circle Rectangle 8
9 Interface Inheritance (Subtyping) Type Type defines a set of values and a set of permissible operations Type only defines a behavior, but no implementation Types are used in declarations of reference variables e.g. Figure f; Java: Types are defined by classes. Subtyping Types can be specialized Subtype defines a is-a relationship The operations defined for a type can also be applied for a subtype Java: Subtyping is defined by subclassing, i.e. each subclass also defines a subtype (i.e. no private inheritance!!!) 9
10 Method Overriding Method Overriding If in an extension class a method is declared with the same signature as in the base class, then the method in the base class is overridden Overriding # overloading!!! if signatures differ, method in base class is NOT overridden but overloaded! Overriding method hides overridden method Hidden method can be called with a supercall An overriding method can (should) be marked with annotation 10
11 Method Overriding Inherited methods may be overridden class Rectangle extends Figure { private int w, public void draw() { drawrectangle(x, y, w, h); class TextBox extends Rectangle { private String public void draw() { super.draw(); drawstring(x+5, y+5, text); 11
12 Inheritance: Initialization Initialization of base class What happens when new instance of an extended class is created? Base class and extended class have to be initialized Base class is initialized before extended class is initialized (because extended class may depend on fields in the base class) Constructor of extended class first calls default constructor of base class What happens if such a constructor is not available? Explicit invocation of a constructor in the base class with super( ) a constructor in the base class can be explicitly called super( ) is only allowed as first statement in a constructor a corresponding constructor must be provided by the base class super( ) and this( ) exclude each other 12
13 Inheritance: Initialization Example class Figure { private int x, y; public Figure(int x, int y) { this.x = x; this.y = y; public void move(int dx, int dy) { x += dx; y += dy; public void draw() { class Rectangle extends Figure { private int w, h; public Rectangle(int x, int y, int w, int h) { super(x, y); this.w = w; this.h = h; public Rectangle(int w, int h) { this(0, 0, w, h); public int getarea() { return w*h; public int getwidth() { return w; public void draw() { /* draw Rectangle */ 13
14 Inheritance: Initialization Implicit Initialization of Base Class If neither a this( ) nor a super( ) call is declared in a constructor, then the default constructor (no arguments) of the base class is called. if the default constructor does not exist: error! Default Constructor (revisited) class A { => class A { A() { super(); 14
15 Inheritance in Java: Base Class Object Object (java.lang.object) Direct or indirect base class of all classes (single rooted class tree) Methods String tostring() returns a string representation of the object boolean equals(object obj) returns whether obj is "equal to" this one int hashcode() returns a hash code value for the object void finalize() called by the GC (last wish) Object clone() returns a copy of this Class<?> getclass() returns the runtime class of an object. Synchronization primitives void notify() void notifyall() void wait() void wait(long timeout) void wait(long timeout, int nanos) 15
16 Inheritance in Java: Base Class Object Default implementations public String tostring() { return getclass().getname() + "@" + Integer.toHexString(hashCode()); public boolean equals(object obj) { return this == obj; All other methods are declared as native (implemented by the JVM) If you want to have another behavior, then override the methods String tostring() boolean equals(object obj) int hashcode() void finalize() Object clone() // same signature!!!! 16
17 Polymorphism Polymorphic Assignment Instances of extension classes can be assigned to variables of base type Figure f = new Rectangle(0, 0, 4, 2); Static type of f: Figure Static type = type of variable Static type of a variable never changes Dynamic type of f: Rectangle Dynamic type = type of referenced object Dynamic type of a variable may change with every assignment Dynamic Type Static Type 17
18 Polymorphism Polymorphic Dispatch Dynamic type defines which method is called Figure f = new Rectangle(0, 0, 4, 2); f.draw(); // => Rectangle s draw is called f = new Circle(5, 5, 10); f.draw(); // => Circle s draw is called Figure[] figs = new Figure[]{ new Rectangle(0,0,4,2), new Circle(5,5,10) ; for(int i=0; i < figs.length; i++) { figs[i].draw(); Polymorphism: Definition Polymorphism allows operations to be performed on objects without needing to know which class the object belongs to, provided that we can guarantee that the class implements the specified type 18
19 Polymorphism Static Type In Java polymorphism is controlled by type declarations Figure f; When a variable is declared as being of a particular type, then we have a language-enforced guarantee that any object referenced by that variable will have (at least) all the features of that type. f.move(2, 3); f.draw(); // works (as expected) Static type defines whether a method call is possible method has to be defined in type declaration static type is responsible for method overload resolution! Dynamic Type Dynamic type is the type of the referenced instance Dynamic type defines which method is executed (dynamic dispatch) 19
20 Polymorphism Type test: instanceof with the instanceof operator the dynamic type can be inspected if( f instanceof Rectangle)... // returns true if dynamic type of f is // Rectangle (or an extension thereof) // null instanceof C => false Type casts: C-style casts (Rectangle) f // changes the static type of expression f // to Rectangle; // a run-time error is thrown if the // dynamic type of f is not a Rectangle // (or an extension thereof) this type cast allows to access the methods / attributes defined for Rectangle only (and not for Figure). 20
21 Polymorphism Figure f = new Rectangle(); Rectangle r = (Rectangle) f; f r Rectangle x y w h 21
22 Polymorphism Liskov Substitution Principle If S is a subtype of T, then objects of type T may be replaced with objects of type S without altering the correctness of the program Whenever you work with an instance of type T, you should not be surprised if you effectively work with an instance of type S An instance of type S can be used at all places where an instance of type T is expected Consequences Overriding methods need to satisfy the rules specified by the base class Overriding methods may do more and expect less 22
23 Discussion: Which is Subtype of What Square class Square { private int x, y, w; public void setwidth(int w) { this.w = w; public int getwidth() { return w; Rectangle class Rectangle { private int x, y, w, h; public void setwidth(int w) { this.w = w; public void setheight(int h) { this.h = h; public int getwidth() { return w; public int getheight() { return h; 23
24 Outline Inheritance Abstract Classes Interfaces 24
25 Figure Example class Figure { int x, y; void draw( ) { class Rectangle extends Figure { int w, void draw( ) { /* draw Rectangle at (x,y) */ Text class TextBox extends Rectangle { String void draw( ) { Screen.drawText(x, y, text); super.draw(); How to implement draw in Figure? Do we need draw at all here? 25
26 Abstract Methods Abstract methods only define a signature public abstract void draw( ); have no body have to be implemented in subclasses Abstract methods are useful for polymorphic calls They define the methods which must be provided by subclasses Abstract methods can only be declared in abstract classes 26
27 Abstract Classes Abstract classes are declared with the abstract keyword public abstract class Figure { private int x, y; public abstract void draw( ); Abstract classes cannot be instantiated, but may have attributes, and thus may have constructors Classes which extend abstract classes have to override (implement) all abstract methods or have to be declared as abstract if not all abstract methods are overridden 27
28 Figure Example Abstract classes may have attributes Abstract classes may have constructors public abstract class Figure { private int x, y; public Figure(int x, int y) { this.x = x; this.y = y; public int getx() { return x; public int gety() { return y; public void move(int dx, int dy) { x += dx; y += dy; public abstract void draw( ); Abstract classes may have nonabstract methods public class Square extends Figure { private int size; public Square(int x, int y, int w){ super(x,y); size = w; public int getsize() { return public void draw( ) { drawrectangle(getx(), gety(), w, w); 28
29 Abstract Classes: Remarks Abstract Classes and Methods An abstract method must be declared in an abstract class An abstract class must not necessarily contain an abstract method! Useful to declare classes that cannot be instantiated (but extended) Figure[ ] figures = new Figures[10] Arrays of an abstract base type may be instantiated since NO instances are created figures-array can be filled with references to concrete figures figures[0] = new Square(0, 0, 10); figures[1] = new Rectangle(10, 10, 20, 10); for(figure f : figures) { f.draw( ); 29
30 Abstract Classes: Remarks Abstract Methods can always be called The concrete instances implement these methods Interesting case: calling abstract methods within an abstract class abstract class OutputStream { public abstract void write(byte b); // implementation depends on stream type, i.e. // SocketStream, FileStream, etc. public void write(byte[] buf){ for(int i=0; i<buf.length; i++) write(buf[i]); 30
31 Abstract Classes: Remarks private Private methods cannot be declared abstract, as private methods cannot be overridden. static Static methods cannot be declared abstract, as static methods can never be overridden. 31
32 Outline Inheritance Abstract Classes Interfaces 32
33 Interfaces Characteristics No implementation All methods are by default public and abstract No state All attributes are by default public, static and final (i.e. constants) No constructors Example public interface Figure { int getx(); int gety(); void move(int dx, int dy); void draw(); 33
34 Interfaces (since Java 8) Default methods Since Java8 interfaces may contain default methods Default methods can access other methods Allows to extend interfaces without breaking existing classes Example public interface Figure { int getx(); int gety(); void move(int dx, int dy); void draw(); default void moveto(int x, int y) { move(x - getx(), y - gety()); 34
35 Implementation of Interfaces implements Interfaces are implemented in classes All methods defined in the declared interfaces have to be implemented or the class has to be declared abstract class Rectangle implements Figure { private int x, y, w, h; public Rectangle(int x, int y, int w, int h) { this.x = x; this.y = y; this.w = w; this.h = public void draw( ) { public void move(int dx, int dy) { x+=dx; public int getx() { return public int gety() { return y; public int getwidth() { return w; public int getheight() { return h; 35
36 Interface and Abstract Base Class Abstract Figure may be used as base class for concrete Figures public abstract class AbstractFigure implements Figure { private int x, y; public AbstractFigure(int x, int y) { this.x=x; public int getx() { return public int gety() { return public void move(int dx, int dy) { x+=dx; y+=dy; Concrete Figures extend abstract base class public class Rectangle extends AbstractFigure { private int w, h; public Rectangle(int x, int y, int w, int h) { super(x, y); this.w=w; public void draw( ) { drawrectangle(x, y, w, h); 36
37 Interface and Abstract Base Class Interface: provides a type Abstract class: semi finished component Contains default implementations which can be used in subclasses which can be overridden in subclasses Concrete class: complete component 37
38 Implementing Multiple Interfaces A class can extend only one class (implicitly class Object) A class can implement several interfaces Example: Polygon provides polygon-specific methods interface Polygon { boolean isconvex(); Rectangle could implement both interfaces Figure (implemented directly or inherited over AbstractFigure) Polygon class Rectangle implements extends AbstractFigure Figure, Polygon implements { Polygon public void draw( ) public boolean isconvex() { 38
39 Interface Extension Interfaces may be extended all methods of the base interface are inherited additional methods can be specified interface Polygon { boolean isconvex(); interface Polygon2 extends Polygon { Line[] getlines() Interfaces may extend multiple interfaces (multiple subtyping) methods with same name / argument list must have the same return type interface PolygonFigure extends Figure, Polygon {... 39
40 Interface Variables Interface type can be used to declare reference variables similar to (abstract) classes: reference refers to a class which implements the interface only the methods declared in the interface are available instanceof can be used to check the dynamic type Examples Variable Dynamic Type Static Type Figure f; // interface type f = new Rectangle(0, 0, 300, 200); f.move(30, 30); Parameter void addfigure(figure f) // can be called with any class implementing interface Figure 40
41 Interface Variables Examples public interface Figure { int getx(); int gety(); void move(int dx, int dy); public interface Polygon { boolean isconvex(); public class Rectangle implements Figure, Polygon { 41
42 Interface Variables Examples Rectangle r = new Rectangle(0, 0, 50, 30); Figure f = r; // Rectangle implements Figure Polygon p = r; // and Polygon interfaces f.move(20, 20); f.isconvex(); f instanceof Polygon ((Polygon)f).isConvex(); // works // does not work // returns true as the refe- // renced object implements // the interface Polygon // works as the referenced // Object implements the // interface Polygon 42
43 Interfaces vs. Abstract Classes Abstract Class Abstract class defines a type can be used in declarations Abstract methods only define signature / behavior have to be implemented in extending class abstract class cannot be instantiated Abstract class may contain attributes method implementations A class may extend only one abstract class Interface Interface defines a type can be used in declarations Methods in interface definition only define signature / behavior have to implemented in an implementing class interface cannot be instantiated Interface = pure abstract class no attributes no code A class may implement several interfaces 43
44 Why Interfaces? Allows to define Interfaces (collection of Methods) independent of a type hierarchy interface Size { int getwidth(); int gethight(); this interface might be implemented by Figures and Cars without the need to extend Figures and Cars from a common subclass (=> unnatural) Size Vehicle Size Figure Car Figure Car 44
45 Marker Interfaces Definition Interface without methods and constants (=> empty interface) Can be used to add an attribute to a class (jdk1.5: Attributes) Example: java.util.randomaccess Definition package java.util; interface RandomAccess { Declaration class ArrayList implements RandomAccess { // ArrayList declares to support random access Test with instanceof Operator if (list instanceof RandomAccess) { 45
46 Summary Subclassing = Implementation Inheritance Code inheritance Implementation aspect Subtyping = Interface Inheritance Java Inheritance of behavioral specification Design aspect Class defines type and implementation if the type of a variable is a class name, then the variable may reference an object of the named class or any subclasss of that class Interface defines type if the type of a variable is defined by an interface name, then the variable may reference an object of any class which implements that interface 46
ITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract
More informationAdvanced Placement Computer Science. Inheritance and Polymorphism
Advanced Placement Computer Science Inheritance and Polymorphism What s past is prologue. Don t write it twice write it once and reuse it. Mike Scott The University of Texas at Austin Inheritance, Polymorphism,
More informationAgenda. Objects and classes Encapsulation and information hiding Documentation Packages
Preliminaries II 1 Agenda Objects and classes Encapsulation and information hiding Documentation Packages Inheritance Polymorphism Implementation of inheritance in Java Abstract classes Interfaces Generics
More informationCS-202 Introduction to Object Oriented Programming
CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction
More informationC12a: The Object Superclass and Selected Methods
CISC 3115 TY3 C12a: The Object Superclass and Selected Methods Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/4/2018 CUNY Brooklyn College 1 Outline The Object class and
More informationProgramming Languages and Techniques (CIS120)
Programming Languages and Techniques (CIS120) Lecture 25 November 1, 2017 Inheritance and Dynamic Dispatch (Chapter 24) Announcements HW7: Chat Client Available Soon Due: Tuesday, November 14 th at 11:59pm
More informationChapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,
Chapter 11 Inheritance and Polymorphism 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design
More informationLecture Notes Chapter #9_b Inheritance & Polymorphism
Lecture Notes Chapter #9_b Inheritance & Polymorphism Inheritance results from deriving new classes from existing classes Root Class all java classes are derived from the java.lang.object class GeometricObject1
More informationCOS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism
COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism Ibrahim Albluwi Composition A GuitarString has a RingBuffer. A MarkovModel has a Symbol Table. A Symbol Table has a Binary
More informationINHERITANCE. Spring 2019
INHERITANCE Spring 2019 INHERITANCE BASICS Inheritance is a technique that allows one class to be derived from another A derived class inherits all of the data and methods from the original class Suppose
More informationContents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance
Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass
More informationInheritance. The Java Platform Class Hierarchy
Inheritance In the preceding lessons, you have seen inheritance mentioned several times. In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those
More informationInheritance. Lecture 11 COP 3252 Summer May 25, 2017
Inheritance Lecture 11 COP 3252 Summer 2017 May 25, 2017 Subclasses and Superclasses Inheritance is a technique that allows one class to be derived from another. A derived class inherits all of the data
More informationWhat is Inheritance?
Inheritance 1 Agenda What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain super keyword Overriding methods (most important) Hiding methods Hiding fields Type casting
More informationInheritance and Polymorphism
Inheritance and Polymorphism Dr. M. G. Abbas Malik Assistant Professor Faculty of Computing and IT (North Jeddah Branch) King Abdulaziz University, Jeddah, KSA mgmalik@kau.edu.sa www.sanlp.org/malik/cpit305/ap.html
More informationInheritance. Inheritance
1 2 1 is a mechanism for enhancing existing classes. It allows to extend the description of an existing class by adding new attributes and new methods. For example: class ColoredRectangle extends Rectangle
More informationJava Object Oriented Design. CSC207 Fall 2014
Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code
More informationINHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U
C A N A D I A N I N T E R N A T I O N A L S C H O O L O F H O N G K O N G INHERITANCE & POLYMORPHISM P2 LESSON 12 P2 LESSON 12.1 INTRODUCTION inheritance: OOP allows a programmer to define new classes
More informationInheritance (continued) Inheritance
Objectives Chapter 11 Inheritance and Polymorphism Learn about inheritance Learn about subclasses and superclasses Explore how to override the methods of a superclass Examine how constructors of superclasses
More informationInheritance and Polymorphism
Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg
More informationInheritance. Transitivity
Inheritance Classes can be organized in a hierarchical structure based on the concept of inheritance Inheritance The property that instances of a sub-class can access both data and behavior associated
More informationOBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616
OBJECT ORIENTED PROGRAMMING Course 4 Loredana STANCIU loredana.stanciu@upt.ro Room B616 Inheritance A class that is derived from another class is called a subclass (also a derived class, extended class,
More informationMethods Common to all Classes
Methods Common to all Classes 9-2-2013 OOP concepts Overloading vs. Overriding Use of this. and this(); use of super. and super() Methods common to all classes: tostring(), equals(), hashcode() HW#1 posted;
More informationCMSC 132: Object-Oriented Programming II
CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation
More informationCH. 2 OBJECT-ORIENTED PROGRAMMING
CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED
More informationPractice for Chapter 11
Practice for Chapter 11 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Object-oriented programming allows you to derive new classes from existing
More informationPolymorphism. return a.doublevalue() + b.doublevalue();
Outline Class hierarchy and inheritance Method overriding or overloading, polymorphism Abstract classes Casting and instanceof/getclass Class Object Exception class hierarchy Some Reminders Interfaces
More informationInheritance Motivation
Inheritance Inheritance Motivation Inheritance in Java is achieved through extending classes Inheritance enables: Code re-use Grouping similar code Flexibility to customize Inheritance Concepts Many real-life
More informationProgramming Languages and Techniques (CIS120)
Programming Languages and Techniques (CIS120) Lecture 26 March 26, 2015 Inheritance and Dynamic Dispatch Chapter 24 public interface Displaceable { public int getx(); public int gety(); public void move
More informationComputer Science 210: Data Structures
Computer Science 210: Data Structures Summary Today writing a Java program guidelines on clear code object-oriented design inheritance polymorphism this exceptions interfaces READING: GT chapter 2 Object-Oriented
More informationDay 4. COMP1006/1406 Summer M. Jason Hinek Carleton University
Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information
More informationHAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.
HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all
More information1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides
1B1b Inheritance Agenda Introduction to inheritance. How Java supports inheritance. Inheritance is a key feature of object-oriented oriented programming. 1 2 Inheritance Models the kind-of or specialisation-of
More informationChapter 6: Inheritance
Chapter 6: Inheritance EECS 1030 moodle.yorku.ca State of an object final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80; GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT);
More informationChapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber
Chapter 10 Inheritance and Polymorphism Dr. Hikmat Jaber 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the
More informationCS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism rights reserved. 2 Motivation Suppose you want to define classes to model circles, rectangles, and
More informationChapter 5 Object-Oriented Programming
Chapter 5 Object-Oriented Programming Develop code that implements tight encapsulation, loose coupling, and high cohesion Develop code that demonstrates the use of polymorphism Develop code that declares
More informationProgramming Languages and Techniques (CIS120)
Programming Languages and Techniques (CIS120) Lecture 24 October 29, 2018 Arrays, Java ASM Chapter 21 and 22 Announcements HW6: Java Programming (Pennstagram) Due TOMORROW at 11:59pm Reminder: please complete
More informationCOMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed
1 1 COMP200 INHERITANCE OOP using Java, from slides by Shayan Javed 2 Inheritance Derive new classes (subclass) from existing ones (superclass). Only the Object class (java.lang) has no superclass Every
More informationChapter 10 Classes Continued. Fundamentals of Java
Chapter 10 Classes Continued Objectives Know when it is appropriate to include class (static) variables and methods in a class. Understand the role of Java interfaces in a software system and define an
More informationOBJECT ORİENTATİON ENCAPSULATİON
OBJECT ORİENTATİON Software development can be seen as a modeling activity. The first step in the software development is the modeling of the problem we are trying to solve and building the conceptual
More informationJava Fundamentals (II)
Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class
More informationToday. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.
Today Book-keeping Inheritance Subscribe to sipb-iap-java-students Interfaces Slides and code at http://sipb.mit.edu/iap/java/ The Object class Problem set 1 released 1 2 So far... Inheritance Basic objects,
More informationOutline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring
Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,
More informationCSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:
PROGRAMMING ASSIGNMENT 3: Read Savitch: Chapter 7 Programming: You will have 5 files all should be located in a dir. named PA3: ShapeP3.java PointP3.java CircleP3.java RectangleP3.java TriangleP3.java
More informationThe Liskov Substitution Principle
Agile Design Principles: The Liskov Substitution Principle Based on Chapter 10 of Robert C. Martin, Agile Software Development: Principles, Patterns, and Practices, Prentice Hall, 2003 and on Barbara Liskov
More informationPowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien
PowerPoint Slides Object-Oriented Design Using JAVA by Dale Skrien Chapter 2 Object-oriented Programming Divides the program into a set of communicating objects Encapsulates in an object all the behavior
More informationM301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism
Block 1: Introduction to Java Unit 4: Inheritance, Composition and Polymorphism Aims of the unit: Study and use the Java mechanisms that support reuse, in particular, inheritance and composition; Analyze
More informationMore About Objects. Zheng-Liang Lu Java Programming 255 / 282
More About Objects Inheritance: passing down states and behaviors from the parents to their children. Interfaces: requiring objects for the demanding methods which are exposed to the outside world. Polymorphism
More information(SE Tutorials: Learning the Java Language Trail : Interfaces & Inheritance Lesson )
(SE Tutorials: Learning the Java Language Trail : Interfaces & Inheritance Lesson ) Dongwon Jeong djeong@kunsan.ac.kr; http://ist.kunsan.ac.kr/ Information Sciences and Technology Laboratory, Dept. of
More informationPROGRAMMING LANGUAGE 2
31/10/2013 Ebtsam Abd elhakam 1 PROGRAMMING LANGUAGE 2 Java lecture (7) Inheritance 31/10/2013 Ebtsam Abd elhakam 2 Inheritance Inheritance is one of the cornerstones of object-oriented programming. It
More informationHAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.
HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all
More informationJava Magistère BFA
Java 101 - Magistère BFA Lesson 3: Object Oriented Programming in Java Stéphane Airiau Université Paris-Dauphine Lesson 3: Object Oriented Programming in Java (Stéphane Airiau) Java 1 Goal : Thou Shalt
More informationProgramming 2. Inheritance & Polymorphism
Programming 2 Inheritance & Polymorphism Motivation Lame Shape Application public class LameShapeApplication { Rectangle[] therects=new Rectangle[100]; Circle[] thecircles=new Circle[100]; Triangle[] thetriangles=new
More informationGraphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub
Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java
More information25. Generic Programming
25. Generic Programming Java Fall 2009 Instructor: Dr. Masoud Yaghini Generic Programming Outline Polymorphism and Generic Programming Casting Objects and the instanceof Operator The protected Data and
More informationInheritance (cont.) Inheritance. Hierarchy of Classes. Inheritance (cont.)
Inheritance Inheritance (cont.) Object oriented systems allow new classes to be defined in terms of a previously defined class. All variables and methods of the previously defined class, called superclass,
More informationCMSC 132: Object-Oriented Programming II
CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation
More informationCLASS DESIGN. Objectives MODULE 4
MODULE 4 CLASS DESIGN Objectives > After completing this lesson, you should be able to do the following: Use access levels: private, protected, default, and public. Override methods Overload constructors
More informationExample: Count of Points
Example: Count of Points 1 public class Point { 2... 3 private static int numofpoints = 0; 4 5 public Point() { 6 numofpoints++; 7 } 8 9 public Point(int x, int y) { 10 this(); // calling Line 5 11 this.x
More information1 Shyam sir JAVA Notes
1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write
More informationInheritance (Deitel chapter 9)
Inheritance (Deitel chapter 9) 1 2 Plan Introduction Superclasses and Subclasses protected Members Constructors and Finalizers in Subclasses Software Engineering with Inheritance 3 Introduction Inheritance
More informationSoftware Paradigms (Lesson 3) Object-Oriented Paradigm (2)
Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Table of Contents 1 Reusing Classes... 2 1.1 Composition... 2 1.2 Inheritance... 4 1.2.1 Extending Classes... 5 1.2.2 Method Overriding... 7 1.2.3
More informationInheritance -- Introduction
Inheritance -- Introduction Another fundamental object-oriented technique is called inheritance, which, when used correctly, supports reuse and enhances software designs Chapter 8 focuses on: the concept
More informationArgument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.
Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar
More informationObject-oriented Programming. Object-oriented Programming
2014-06-13 Object-oriented Programming Object-oriented Programming 2014-06-13 Object-oriented Programming 1 Object-oriented Languages object-based: language that supports objects class-based: language
More informationInheritance & Polymorphism Recap. Inheritance & Polymorphism 1
Inheritance & Polymorphism Recap Inheritance & Polymorphism 1 Introduction! Besides composition, another form of reuse is inheritance.! With inheritance, an object can inherit behavior from another object,
More informationClass definition. complete definition. public public class abstract no instance can be created final class cannot be extended
JAVA Classes Class definition complete definition [public] [abstract] [final] class Name [extends Parent] [impelements ListOfInterfaces] {... // class body public public class abstract no instance can
More informationChapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism 1 Motivations OOP is built on three principles: Encapsulation (classes/objects, discussed in chapters 9 and 10), Inheritance, and Polymorphism. Inheritance: Suppose
More informationIntroduction to Inheritance
Introduction to Inheritance James Brucker These slides cover only the basics of inheritance. What is Inheritance? One class incorporates all the attributes and behavior from another class -- it inherits
More informationAbstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof
Abstract Class Lecture 21 Based on Slides of Dr. Norazah Yusof 1 Abstract Class Abstract class is a class with one or more abstract methods. The abstract method Method signature without implementation
More informationFor this section, we will implement a class with only non-static features, that represents a rectangle
For this section, we will implement a class with only non-static features, that represents a rectangle 2 As in the last lecture, the class declaration starts by specifying the class name public class Rectangle
More informationInheritance (Part 5) Odds and ends
Inheritance (Part 5) Odds and ends 1 Static Methods and Inheritance there is a significant difference between calling a static method and calling a non-static method when dealing with inheritance there
More informationComputer Science II (20073) Week 1: Review and Inheritance
Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system
More informationJava: introduction to object-oriented features
Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer
More informationProgramming Languages and Techniques (CIS120)
Programming Languages and Techniques (CIS120) Lecture 25 March 18, 2013 Subtyping and Dynamic Dispatch Announcements HW07 due tonight at midnight Weirich OH cancelled today Help your TAs make the most
More informationInheritance Considered Harmful
Inheritance Considered Harmful Inheritance and Reentrance Example: StringOutputStream Robust Variants Forwarding Template Methods / Hooks Inner calls 1 Inheritance Interface Inheritance Subtyping Reuse
More informationAbstract Classes and Interfaces
Abstract Classes and Interfaces Reading: Reges and Stepp: 9.5 9.6 CSC216: Programming Concepts Sarah Heckman 1 Abstract Classes A Java class that cannot be instantiated, but instead serves as a superclass
More informationCISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College
CISC 3115 TY3 C09a: Inheritance Hui Chen Department of Computer & Information Science CUNY Brooklyn College 9/20/2018 CUNY Brooklyn College 1 Outline Inheritance Superclass/supertype, subclass/subtype
More informationCS1150 Principles of Computer Science Objects and Classes
CS1150 Principles of Computer Science Objects and Classes Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Object-Oriented Thinking Chapters 1-8
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism Abstract Classes
More informationFirst IS-A Relationship: Inheritance
First IS-A Relationship: Inheritance The relationships among Java classes form class hierarchy. We can define new classes by inheriting commonly used states and behaviors from predefined classes. A class
More informationCISC370: Inheritance
CISC370: Inheritance Sara Sprenkle 1 Questions? Review Assignment 0 due Submissions CPM Accounts Sara Sprenkle - CISC370 2 1 Quiz! Sara Sprenkle - CISC370 3 Inheritance Build new classes based on existing
More informationObject Oriented Programming. Java-Lecture 11 Polymorphism
Object Oriented Programming Java-Lecture 11 Polymorphism Abstract Classes and Methods There will be a situation where you want to develop a design of a class which is common to many classes. Abstract class
More informationReusing Classes. Hendrik Speleers
Hendrik Speleers Overview Composition Inheritance Polymorphism Method overloading vs. overriding Visibility of variables and methods Specification of a contract Abstract classes, interfaces Software development
More informationCMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.
CMSC131 Inheritance Object When we talked about Object, I mentioned that all Java classes are "built" on top of that. This came up when talking about the Java standard equals operator: boolean equals(object
More informationObject Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia
Object Oriented Programming in Java Jaanus Pöial, PhD Tallinn, Estonia Motivation for Object Oriented Programming Decrease complexity (use layers of abstraction, interfaces, modularity,...) Reuse existing
More informationExtending Classes (contd.) (Chapter 15) Questions:
Extending Classes (contd.) (Chapter 15) Questions: 1 1. The following C++ program compiles without any problems. When run, it even prints out the hello called for in line (B) of main. But subsequently
More informationInheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L
Inheritance Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 9.4 1 Inheritance Inheritance allows a software developer to derive
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Interface Abstract data types Version of January 26, 2013 Abstract These lecture notes are meant
More informationCS250 Intro to CS II. Spring CS250 - Intro to CS II 1
CS250 Intro to CS II Spring 2017 CS250 - Intro to CS II 1 Topics Virtual Functions Pure Virtual Functions Abstract Classes Concrete Classes Binding Time, Static Binding, Dynamic Binding Overriding vs Redefining
More information24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini
24. Inheritance Java Fall 2009 Instructor: Dr. Masoud Yaghini Outline Superclasses and Subclasses Using the super Keyword Overriding Methods The Object Class References Superclasses and Subclasses Inheritance
More informationInheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass
Inheritance and Polymorphism Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism Inheritance (semantics) We now have two classes that do essentially the same thing The fields are exactly
More informationUCLA PIC 20A Java Programming
UCLA PIC 20A Java Programming Instructor: Ivo Dinov, Asst. Prof. In Statistics, Neurology and Program in Computing Teaching Assistant: Yon Seo Kim, PIC Chapter 5 Classes & Inheritance Creating Classes
More informationThe Sun s Java Certification and its Possible Role in the Joint Teaching Material
The Sun s Java Certification and its Possible Role in the Joint Teaching Material Nataša Ibrajter Faculty of Science Department of Mathematics and Informatics Novi Sad 1 Contents Kinds of Sun Certified
More information22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini
22. Inheritance Java Summer 2008 Instructor: Dr. Masoud Yaghini Outline Superclasses and Subclasses Using the super Keyword Overriding Methods The Object Class References Inheritance Object-oriented programming
More informationExercise: Singleton 1
Exercise: Singleton 1 In some situations, you may create the only instance of the class. 1 class mysingleton { 2 3 // Will be ready as soon as the class is loaded. 4 private static mysingleton Instance
More informationJava for Programmers Course (equivalent to SL 275) 36 Contact Hours
Java for Programmers Course (equivalent to SL 275) 36 Contact Hours Course Overview This course teaches programmers the skills necessary to create Java programming system applications and satisfies the
More informationCSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community
CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring
More information