Reusable Classes.  Motivation: Write less code!

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 1 Inheritance University Of Ha’il.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Classes and SubClasses Super Parent Sub Child IS - A.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
LECTURE 07 Programming using C# Inheritance
Inheritance using Java
Intro to OOP with Java, C. Thomas Wu
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Web Design & Development Lecture 9
Lecture 12 Inheritance.
Inheritance and Polymorphism
One class is an extension of another.
Agenda Warmup AP Exam Review: Litvin A2
Object Oriented Analysis and Design
Can perform actions and provide communication
One class is an extension of another.
Class Inheritance (Cont.)
Can perform actions and provide communication
Inheritance, Polymorphism, and Interfaces. Oh My
Inherited Classes in Java
Advanced Java Programming
Can perform actions and provide communication
Chapter 10: Method Overriding and method Overloading
Method Overriding and method Overloading
Agenda Inheritance Polymorphism Type compatibility Homework.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Reusable Classes

 Motivation: Write less code!

Reusable Classes  A reusable class is one that can be used in multiple programs  Not all classes should be reusable  Decide before you design it which category it fits into

Reusable Classes  We have already heard of the STO principle

Reusable Classes  We have already heard of the STO principle  Design a class that implements a single well- defined task.  Do not overburden the class with multiple tasks

Reusable Classes  4 main reusable object types:  User interface object  Controller object  Application logic object  Storage object

Reusable Classes  Don’t forget other  Not everything fits into these categories, but most will

Reusable Classes  Overriding and overloaded methods  Overriding – redefining a method previously defined in the extends hierarchy  Ex. – toString( )  Overloading – defining multiple methods with the same name and purpose (different parameters)  Ex. - multiple constructors

Reusable Classes  Overriding public class MarriedWoman extends Woman{ … public String toString(){ String name = super.toString(); name = name + husbandName; } … }

Reusable Classes  Overloading:  A method signature is determined by the method name and number and data type of parameters  If methods have the same name but different signatures, they are overloaded  println() is a good example public void drawCircle(int x, int y, int radius){ … } public void drawCircle(Circle c){ … }

Reusable Classes  Overloading: public class Person{ public String name; public Person(){ this(“UNKNOWN”); } public (String myName){ name = myName; }

Reusable Classes  Overloading: public class Person{ public String name; public Person(){ this(“UNKNOWN”); } public (String myName){ name = myName; } this calls this constructor

Reusable Classes  This gives users of your class more flexibility in how they call the methods.  It makes your classes more easily usable  Hence, more reusable.  Review:  Inhertance can help you make a number of objects similar in nature from a base class  You can override methods from the base class as needed in the sub classes  Within every class you can overload methods to make your class easier to use.

Reusable Classes  Abstract Classes can help define inheritance  Abstract classes are defined to show general behavior that must be implemented by sub classes  Abstract classes are not instantiable abstract class Student{ … abstract public void computeGrade(); … }

Reusable Classes abstract class Student{ … abstract public void computeGrade(); … }  Abstract methods must be implemented by a sub class  This can be very helpful in defining functionality  Not all methods in an abstract class are abstract  More on lesson 8

Reusable Classes  Questions??

Reusable Classes What is the purpose of defining multiple constructors?

Reusable Classes What is the purpose of defining multiple constructors? To increase the ease of use. By having multiple constructors, the programmer will have a flexibility in creating the instances.

Reusable Classes Which of the following method declarations have the same signature? public void one ( int x, int y) { /* 1 */... } private void one ( int x, int y) { /* 2 */... } public int one ( int x, int y) { /* 3 */... } private void one ( int x, float y) { /* 4 */... }

Reusable Classes Which of the following method declarations have the same signature? public void one ( int x, int y) { /* 1 */... } private void one ( int x, int y) { /* 2 */... } public int one ( int x, int y) { /* 3 */... } private void one ( int x, float y) { /* 4 */... } The method signature does not include the access modifier and the return type, so the methods 1, 2, and 3 have the same signature. Method #4 has a different signature because the data type of the second parameter is float.

Reusable Classes Which is the subclass and which is the superclass in the following declaration? class X extends Y {... }

Reusable Classes Which is the subclass and which is the superclass in the following declaration? class X extends Y {... } X is the subclass, and Y is the superclass.

Reusable Classes Suppose Truck and Motorcycle are subclasses of Vehicle. Which of the following declarations are invalid? Truck t = new Vehicle(); Vehicle v = new Truck(); Motorcycle m1 = new Vehicle(); Motorcycle m2 = new Truck();

Reusable Classes Suppose Truck and Motorcycle are subclasses of Vehicle. Which of the following declarations are invalid? Truck t = new Vehicle(); Vehicle v = new Truck(); Motorcycle m1 = new Vehicle(); Motorcycle m2 = new Truck(); The declarations for t, m1, and m2 are invalid.

Reusable Classes Suppose Truck and Motorcycle are subclasses of Vehicle. Which of the following declarations are invalid? Truck t = new Vehicle(); Vehicle v = new Truck(); Motorcycle m1 = new Vehicle(); Motorcycle m2 = new Truck(); The declarations for t, m1, and m2 are invalid.