1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

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.
OOP: Inheritance By: Lamiaa Said.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10: Inheritance and Polymorphism
Chapter 10 Classes Continued
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
Java SE 8 for Programmers, Third Edition Advanced Java Programming.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
CS342 Data Structure Review of C++: Overriding base- class functions.
Interfaces Need arising from software engineering –Disparate groups of programmers need to agree to a “contract” that spells out how their software interacts.
Java File Structure.  File class which is defined by java.io does not operate on streams  deals directly with files and the file system  File class.
Lecture 9 Polymorphism Richard Gesick.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Method signature Name and parameter list public static void test() public static int test() => Syntax error, Duplicate method test You cannot declare more.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
1 Chapter 5 - Object-Oriented Programming 1 What is inheritance? Object-oriented systems allow classes to be defined in terms of other classes. Classes.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
O O P Polymorphism Object Oriented Programming Prepared & Presented by: dr.Ismail Farahat Chapter 4.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
1 Advanced Programming Inheritance (2). 2 Inheritance Inheritance allows a software developer to derive a new class from an existing one The existing.
© 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.
CS 5JA Introduction to Java Pop Quiz Define/Explain encapsulation. In object-oriented programming, encapsulation refers to the grouping of data and the.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
Inheritance ndex.html ndex.htmland “Java.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Object-Oriented Programming: Polymorphism Chapter 10.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Web Design & Development Lecture 9
Inheritance and Polymorphism
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Chapter 9 Object-Oriented Programming: Inheritance
Week 6 Object-Oriented Programming (2): Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Inheritance Inheritance is a fundamental Object Oriented concept
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes. For instance, we could have an abstract superclass Shape and derive concrete classes (non-abstract classes) such as Square, Circle, Triangle etc. We do not know how to implement the area() method of the Shape class - leave it empty and let subclasses to define it (abstract method). Chapter 7 - Object-Oriented Programming 3

2 Abstract Methods A method declared as abstract is NOT allowed to contain a body. public abstract void f(); No method body! To force the subclasses to define the method. A class must be declared as abstract if it contains one or more abstract methods.

3 Abstract Class Syntax A class is made abstract by declaring it with the keyword abstract. public abstract class Shape { String name; public abstract double area(); public String name() { return name; } //other stuff } An abstract class cannot be instantiated. Abstract ClassAbstract Method

4 Can't Instantiate an Abstract Class abstract class Shape { String name; abstract public double area(); public String name() { return name; } //other stuff } class TestShape { public static void main(String[] args) { Shape s = new Shape(); } The following example tries to instantiate an abstract class. >javac TestShape.java TestShape.java:10: Shape is abstract; cannot be instantiated Shape s = new Shape(); ^ 1 error

5 Abstract Method => Abstract Class class Shape { String name; abstract public double area(); public String name() { return name; } //other stuff } class TestShape { public static void main(String[] args) { Shape s = new Shape(); } if a class contains even one abstract method, then the class itself has to be declared to be abstract. >javac TestShape2.java TestShape2.java:1: Shape should be declared abstract; it does not define area() in Shape class Shape { ^ 1 error

6 Example abstract class Shape { String name; abstract public double area(); public String name() { return name; } //other stuff } class Square extends Shape { double length; public Square(double length) { this.length = length; } public double area() { return length*length; } class TestShape3 { public static void main(String[] args) { Square s = new Square(12.5); System.out.println("The area of square s is " + s.area()); } The class inherits from Shape MUST implement (provide a method body) area (). Output The area of square s is

7 Introduction to Polymorphism Polymorphism –Polymorphism means "having many forms." –In Java, it means that a single variable might be used with several different types of related objects at different times in a program. –Recall that a superclass reference can refer to subclass objects. –When the variable is used with "dot notation" variable.method() to invoke a method, exactly which method is run depends on the object that the variable currently refers to.

8 Example // Shape and Square are the same as before class Circle extends Shape { double radius; public Circle(double radius) { this.radius = radius; } public double area() { return Math.PI*radius*radius; } class TestShape4 { public static void main(String[] args) { Shape s = new Square(12.5); System.out.println("The area of s is " + s.area()); s = new Circle(5); System.out.println("The area of s is " + s.area()); } Consider the previous example, suppose we want to add a new class Circle that inherits from Shape. Output The area of s is The area of s is Shape s can refer to a Square or a Circle. s.area() calls the correct method

9 Advantages of using Polymorphism Polymorphism –Reduce amount of work in distinguishing and handling object types Old approach - Determine the object type and then uses a nested if-else statement to execute the appropriate statements (difficult and easy to make mistake) –Helps build extensible systems Program can be written to process objects of types that may not exist when the program is under development. Can add classes to systems easily

10 Polymorphism Examples Video game –Superclass GamePiece Contains method drawYourself –Subclasses Martian, Venutian, LaserBeam, etc. Override method drawYourself –Martian draws itself with antennae –LaserBeam draws itself as bright red beam –This is polymorphism –Easily extensible Suppose we add class Mercurian –Class Mercurian inherits superclass GamePiece –Overrides method drawYourself

11 Case Study: A Payroll System Using Polymorphism Example –Abstract superclass Employee Method earnings applies to all employees Person’s earnings dependent on type of Employee –Concrete Employee subclasses declared final Boss CommissionWorker HourlyWorker

12 abstract class Employee { private String firstName; private String lastName; // Constructor public Employee( String first, String last ) { firstName = new String ( first ); lastName = new String( last ); } // Return a copy of the first name public String getFirstName() { return new String( firstName ); } // Return a copy of the last name public String getLastName() { return new String( lastName ); } /* Abstract method that must be implemented for each * derived class of Employee from which objects are * instantiated. */ abstract double earnings(); }

13 public final class Boss extends Employee { private double weeklySalary; // Constructor for class Boss public Boss( String first, String last, double s) { super( first, last ); // call base-class constructor setWeeklySalary( s ); } // Set the Boss's salary public void setWeeklySalary( double s ) { weeklySalary = (s > 0) ? s : 0; } // Get the Boss's pay public double earnings() { return weeklySalary; } // Print the Boss's name public String toString() { return "Boss: " + getFirstName() + " " + getLastName(); } override Object.toString()

14 // CommissionWorker class derived from Employee final class CommissionWorker extends Employee { private double salary; // base salary per week private double commission; // amount per item sold private int quantity; // total items sold for week // Constructor for class CommissionWorker public CommissionWorker( String first, String last, double s, double c, int q) { super( first, last ); // call base-class constructor setSalary( s ); setCommission( c ); setQuantity( q ); } // Set CommissionWorker's weekly base salary public void setSalary( double s ) { salary = (s>0) ? s : 0; } // Set CommissionWorker's commission public void setCommission(double c) {commission = (c>0) ? c : 0;} // Set CommissionWorker's quantity sold public void setQuantity( int q ) { quantity = (q>0) ? q : 0; } // Determine CommissionWorker's earnings public double earnings(){ return salary + commission * quantity; } // Print the CommissionWorker's name public String toString(){ return "Commission worker: "+getFirstName()+" "+getLastName(); }

15 // Definition of class HourlyWorker final class HourlyWorker extends Employee { private double wage; // wage per hour private double hours; // hours worked for week // Constructor for class HourlyWorker public HourlyWorker( String first, String last, double w, double h ) { super( first, last ); // call base-class constructor setWage( w ); setHours( h ); } // Set the wage public void setWage( double w ) { wage = (w>0) ? w : 0; } // Set the hours worked public void setHours( double h ) { hours = (h>=0 && h<168) ? h : 0; } // Get the HourlyWorker's pay public double earnings() { return wage * hours; } public String toString() { return "Hourly worker: " + getFirstName() +" "+getLastName(); }

16 // Driver for Employee hierarchy class TestEmployee1 { public static void main(String[] args) { Employee emp; // base-class reference emp = new Boss( "John", "Smith", ); System.out.println(emp.toString() + " earned $" + emp.earnings() ); emp = new CommissionWorker( "Sue", "Jones", 400.0, 3.0, 150); System.out.println(emp.toString() + " earned $" + emp.earnings() ); emp = new HourlyWorker( "Karen", "Price", 13.75, 40 ); System.out.println(emp.toString() + " earned $" + emp.earnings() ); } Output Boss: John Smith earned $800.0 Commission worker: Sue Jones earned $850.0 Hourly worker: Karen Price earned $550.0

17 Example (Continued) class TestEmployee2 { public static void main(String[] args) { Employee[] emps = new Employee[3]; // base-class references emps[0] = new Boss( "John", "Smith", ); emps[1] = new CommissionWorker( "Sue", "Jones", 400.0, 3.0, 150); emps[2] = new HourlyWorker( "Karen", "Price", 13.75, 40 ); for (int i=0; i< emps.length; i++) { System.out.println(emps[i] + " earned $" + emps[i].earnings() ); } The above test program can be implemented by using an Employee array. toString() will be called automatically.

18 Example - Add a New Worker final class PieceWorker extends Employee { private double wagePerPiece; // wage per piece output private int quantity; // output for week // constructor for class PieceWorker public PieceWorker(String first, String last, double wage, int num){ super( first, last ); // call superclass constructor setWage( wage ); setQuantity( num ); } // set PieceWorker's wage public void setWage(double wage) { wagePerPiece=(wage>0)?wage:0; } // set number of items output public void setQuantity( int numberOfItems ) { quantity = (numberOfItems > 0) ? numberOfItems : 0; } // determine PieceWorker's earnings public double earnings() { return quantity * wagePerPiece; } public String toString() { return "Piece worker: " + getFirstName() + " " + getLastName(); } } // end class PieceWorker Suppose we have to add a new worker of a new PieceWorker class.

19 // Driver for Employee hierarchy class TestEmployee3 { public static void main(String[] args) { Employee[] emps = new Employee[4]; // base-class reference emps[0] = new Boss( "John", "Smith", ); emps[1] = new CommissionWorker( "Sue", "Jones", 400.0, 3.0, 150); emps[2] = new HourlyWorker( "Karen", "Price", 13.75, 40 ); emps[3] = new PieceWorker( "Maggie", "Jackson", 5.5, 200 ); for (int i=0; i< emps.length; i++) { System.out.println(emps[i + " earned $" + emps[i].earnings() ); } Let's how easy we can modify the main program. Output >java TestEmployee3 Boss: John Smith earned $800.0 Commission worker: Sue Jones earned $850.0 Hourly worker: Karen Price earned $550.0 Piece worker: Maggie Jackson earned $1100.0

20 Interface Java does not support multiple inheritance: a class cannot have more than one superclass. However, Java provides a concept similar to the class, called interface to enable multiple "inheritance". An interface is a collection of abstract methods. With an interface, all methods are implicitly public and abstract, and all data members are implicitly final, public and static. (i.e. they are constants).

21 Interface Example Public class TV { } public interface Recordable { void Record(); void Play(); } public class TV_VCR extends TV implements Recordable { public void Record() { } public void Play() { } // }

22 Interface Example Since the methods of an interface are implicitly public, the overriding methods of the implementing class MUST be declared public. The overriding methods of the implementing class MUST have bodies or the implementing class MUST be declared abstract.