Coming up Which methods are where? – Overriding Calling super’s methods Coupling and cohesion.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Introduction to Java 2 Programming
OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Objects First With Java A Practical Introduction Using BlueJ Improving structure with inheritance 2.0.
Improving structure with inheritance Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Improving structure with inheritance (Chapters 8 and 9)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Inheritance Chapter 8.
More about inheritance Exploring polymorphism. 02/12/2004Lecture 8: More about inheritance2 Main concepts to be covered method polymorphism static and.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
19-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
More about inheritance Exploring polymorphism 5.0.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Abstract Classes and Interfaces
More about inheritance Exploring polymorphism 3.0.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance, Abstract & Interface Pepper With help from and
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
More about inheritance Exploring polymorphism 5.0.
Object Oriented Programming
Comp1004: Inheritance I Super and Sub-classes. Coming up Inheritance and Code Duplication – Super and sub-classes – Inheritance hierarchies Inheritance.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Improving structure with inheritance 3.0. The media project stores details about CDs and DVDs –CD: title, artist, number of tracks, playing time, got-it,
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Coming up Inheritance – Code duplication – Super classes – Constructors Polymorphic collections – “Anywhere a super class is, a sub class can go” Casting.
Inheritance.
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
Advanced Object-Oriented Programming
Inheritance and Polymorphism
Inheritance Basics Programming with Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
More about inheritance
Week 6 Object-Oriented Programming (2): Polymorphism
Computer Programming with JAVA
Chapter 11 Inheritance and Polymorphism
Chapter 14 Abstract Classes and Interfaces
Comp1202: Inheritance I Super and Sub-classes.
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Improving structure with inheritance
Chapter 11 Inheritance and Encapsulation and Polymorphism
Lecture 15 Inheritance.
Presentation transcript:

Coming up Which methods are where? – Overriding Calling super’s methods Coupling and cohesion

More about inheritance Exploring polymorphism 3.0 OO

Coming up Which methods are where? –O–Overriding Calling super’s methods Coupling and cohesion

Continuing with animals The animal metaphor is useful for explaining inheritance Animal sleep() roam() eat() Animal sleep() roam() eat() Dog Cat Elephant All animals eat, sleep and roam Roam in the Animal class is defined as wandering around. Eating is eating anything edible Sleeping is, well, sleeping. OO

Dog Is a type of Animal Dogs sleep like any other animal eats meat and roams in packs They have their own way of doing those things We give them their own methods which are more specific than the animal class Animal sleep() roam() eat() Animal sleep() roam() eat() Dog eat() roam() Dog eat() roam() IS-AN Sleeps as an OO

Continuing with animals The animal metaphor is useful for explaining inheritance Animal sleep() roam() eat() Animal sleep() roam() eat() Dog Cat Elephant All animals eat, sleep and roam Roam in the Animal class is defined as wandering around. Eating is eating anything edible Sleeping is, well, sleeping. Dog eat() roam() Dog eat() roam() OO

Cat is the same Is a type of Animal Cats sleep like any other animal eats meat and roams independantly Animal sleep() roam() eat() Animal sleep() roam() eat() Cat eat() roam() Cat eat() roam() IS-AN Sleeps as an OO

Continuing with animals The animal metaphor is useful for explaining inheritance Animal sleep() roam() eat() Animal sleep() roam() eat() Dog Cat Elephant All animals eat, sleep and roam Roam in the Animal class is defined as wandering around. Eating is eating anything edible Sleeping is, well, sleeping. Dog eat() roam() Dog eat() roam() Cat eat() roam() Cat eat() roam() OO

Elephant Is a type of Animal Elephants sleep like any other animal eats grass and roams by wandering like an animal Animal sleep() roam() eat() Animal sleep() roam() eat() Elephant eat() Elephant eat() IS-AN Sleeps as an Roams like an OO

Continuing with animals The animal metaphor is useful for explaining inheritance Animal sleep() roam() eat() Animal sleep() roam() eat() Dog Cat Elephant All animals eat, sleep and roam Roam in the Animal class is defined as wandering around. Eating is eating anything edible Sleeping is, well, sleeping. Dog eat() roam() Dog eat() roam() Cat eat() roam() Cat eat() roam() Elephant eat() Elephant eat() OO

Overriding This is called overriding – Overriding is replacing a method in the super class with a more specific method in the sub class This is not the same as overloading – Over loading is writing two or more methods with the same name but different signatures (return types and parameter lists) OO

Recap Overloading Rules Return types can be different – As long as the arguments are different too i.e. You can’t just change the return type You can change the access levels to more or less restrictive – i.e. You can change public to private, and vice versa Overloading is different to another principle, overiding, which is when you replace a super’s method with one written in a sub class. OO

Inheritance works one way A sub class can ‘see’ properties and methods in its super class A super class cannot see it’s subclasses They look up, not down Sub class Super class Object OO

Which method is called? Elephant e; e = new Elephant(); Cat c = new Cat(); Animal d; d = new Dog(); e.eat(); c.roam(); d.eat(); d.sleep(); e.toString(); Animal sleep() roam() eat() Animal sleep() roam() eat() Dog Cat Elephant Dog eat() roam() Dog eat() roam() Cat eat() roam() Cat eat() roam() Elephant eat() Elephant eat() Object toString() Object toString() ` OO

In short... The ‘first found’ method is called i.e. The bottom-most method wins This means you can override methods from almost all the classes the keyword final will stop you overriding methods. We study it later OO

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The Object class’s methods Methods in Object are inherited by all classes. Any of these may be overridden. The toString method is commonly overridden: – public String toString() – Returns a string representation of the object. Calls to println with just an object automatically result in toString being called: – System.out.println(item);

Coming up Which methods are where? –O–Overriding Calling super’s methods Coupling and cohesion

What if I want to call the super’s method We’ve overloaded the eat method in cat We’d do this, for example, to specify the cat eats meat. What if, in a special case, we call the super classes method? Give the cat some cheese? Remember how to call the super constructor?

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Inheritance and constructors public class CD extends Item { private String artist; private int numberOfTracks; /** * Constructor for objects of class CD */ public CD(String theTitle, String theArtist, int tracks, int time) { super(theTitle, time); artist = theArtist; numberOfTracks = tracks; } // methods omitted } This is how you call a constructor from a super class

Calling a super’s method is almost the same public class Cat{ //code public void feedCatWithCheese(){ super.eat(); } //code }

Coming up Which methods are where? –O–Overriding Calling super’s methods Coupling and cohesion

A different concept Coupling and Cohesion Important in software development Particularly important in OO development OO

Cohesion A class should represent a single concept The public methods (and variables?) define your class’ interface. The interface should be cohesive. i.e. Your interface features should be closely related to your class concept OO

Coupling Coupling refers to – How much your classes rely on each other A class should only need to know about itself in order to work If a class depends strongly on an other class to work, and therefore can’t be used for another project, it is tightly coupled OO

C+C You won’t be examined on Coupling and Cohesion But high cohesion and low coupling are marks of good software design SEG next year Software Engineering this year 3 rd year project

Summary Which methods are where? – Overriding Calling super’s methods Coupling and cohesion