Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

OOP: Inheritance By: Lamiaa Said.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
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,
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
Review of Object-Oriented Concepts in JAVA Object-Oriented Concepts supported by JAVA. Advantages of Object-Orientation. Inheritance. Abstract Classes.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Lecture 10: Inheritance Subclasses and superclasses The inheritance chain Access control The Object cosmic superclass The super keyword Overriding methods.
Polymorphism What is Polymorphism? Taking Advantage of Polymorphism
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Review of Object-Oriented Concepts in JAVA Object-Oriented Concepts supported by JAVA. Object-Oriented Concepts supported by JAVA. Advantages of Object-Orientation.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
CS102--Object Oriented Programming Lecture 10: – Abstract Classes Copyright © 2008 Xiaoyan Li.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
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.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.,
Peyman Dodangeh Sharif University of Technology Fall 2014.
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.
CET203 SOFTWARE DEVELOPMENT Session 2B Constructors, Overriding and Overloading.
Inheritance and Design CSIS 3701: Advanced Object Oriented Programming.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
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.
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.
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
CS 116 Object Oriented Programming II Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Abstract methods and classes. Abstract method A placeholder for a method that will be fully defined in a subclass.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
1 More About Derived Classes and Inheritance Chapter 9.
Chapter 15 Abstract Classes and Interfaces
Inheritance ITI1121 Nour El Kadri.
Inheritance and Polymorphism
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Abstract Classes.
Polymorphism 2nd Lecture
Review of Object-Oriented Concepts in JAVA
Comp 249 Programming Methodology
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Review of Object-Oriented Concepts in JAVA
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 8 Abstract Classes.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes

Unit 022 What is an Abstract Class? A subclass (programmer) normally has the option of whether to override the methods of a superclass or not. However, there are situations where the superclass (programmer) would like to force the subclass (programmer) to override a certain method. This is usually the case if there is no suitable default implementation. For example, consider following examples of Employee and HourlyEmployee classes.

Unit 023 What is an Abstract Class? – Cont’d 1.class Employee{ 2.private String name; 3.private double payRate; 4. 5.public Employee(String name, double payRate){ 6.this.name = name; 7.this.payRate = payRate; 8.} 9.public double pay(){ 10.return payRate; 11.} 12.public void setPayRate(double newRate){ 13.payRate = newRate; 14.} 15.public String toString(){ 16.return "Name: "+name+"Pay Rate: "+payRate; 17.} 18.} The payRate field represents the monthly pay for the employee. Notice that the pay method simply returns the value of this field.

Unit 024 What is an Abstract Class? – Cont’d 1.class HourlyEmployee extends Employee{ 2.private int hours; 3. 4.public HourlyEmployee(String name, double rate){ 5.super(name, rate); 6.hours = 0; 7.} 8.public void assHours(int moreHours){ 9.hours += moreHours; 10.} 11.public String toString(){ 12.return super.toString()+"Hours: "+hours; 13.} 14.public double pay(){ 15.return super.pay() * hours; 16.} 17.} The payRate field represents the hourly pay for the employee. Here the pay method multiplies this value with the hours worked and returns the result.

Unit 025 What is an Abstract Class? – Cont’d Clearly, if the pay method is not overridden, by mistake, the pay of the HourlyEmployee will be computed wrongly. To force the subclass programmer to override this method, the method should be declared as abstract. An abstract method consists of only the method heading. 1.abstract class Employee{ 2.private String name; 3.private double payRate; 4. 5.public Employee(String name, double payRate){ 6.this.name = name; 7.this.payRate = payRate; 8.} 9.public abstract double pay(); 10.public void setPayRate(double newRate){ 11.payRate = newRate; 12.} 13.// }

Unit 026 What is an Abstract Class? – Cont’d Now, once a class has at least one abstract method, then the class is said to be abstract and it must be declared as such in its heading. Thus, an abstract class essentially means a class that is "not completely" defined. The term, concrete class, is sometimes used to describe a class that is not abstract. Note: it is not necessary for an abstract class to have abstract methods. Even if all the methods in a class are implemented, the class will still be abstract if it is declared as such in its heading.

Unit 027 Properties of an Abstract Class An abstract class cannot be instantiated - that is, it cannot be used to create objects. For example, we cannot create an object of the Employee class. Thus, to represent an employee whose pay is per month, we need to sub-class the Employee abstract class as follows: 1.class MonthlyEmployee extends Employee{ 2.public MonthlyEmployee(String empNamem double empRate){ 3.super(empName, empRate); 4.} 5.public double pay(){ 6.return payRate; 7.} 8.}

Unit 028 Properties of an Abstract Class – Cont’d The relationship between the three classes is shown by the following: Although an abstract class cannot be used to create objects, it can be used to define reference variables. Similar to what we saw in inheritance, this variable can be assigned an object created using a concrete sub-class of Employee. An abstract class may contain a constructor but it is invoked only via the super keyword in the constructor of a concrete subclass constructor

Unit 029 Properties of an Abstract Class – Cont’d In summary, an abstract class is just like a normal class except that it cannot be instantiated and is mainly used to factor out instance variables and methods that all classes in a class-hierarchy should have.

Unit 0210 Properties of Abstract Methods An abstract method is like a placeholder for a method that will be fully defined in a descendent class It has a complete method heading, to which has been added the modifier abstract It cannot be private It has no method body, and ends with a semicolon in place of its body public abstract double getPay(); public abstract void doIt(int count);

Unit 0211 Discovering Abstract Classes Suppose we wish to write a program that manipulates two- dimensional geometrical shapes, namely: –Squares –Rectangles and –Circles Suppose further that we want each such shape to tell its name, its area and its perimeter. Since each of these shapes is a type of shape, it makes sense to have a superclass, Shape, from which the others can be derived. However, since the computation of area and perimeter for each of these shapes is different, the subclasses should be forced to override these methods. Thus the Shape class must be abstract.

Unit 0212 Discovering Abstract Classes – Cont’d Moreover, since square is a special type of rectangle, we can derive it from the Rectangle class. Thus, we have a two level hierarchy tree shown as follows:

Unit 0213 Discovering Abstract Classes – Cont’d The code for these classes is shown as follows: 1.public abstract class Shape{ 2.public String name(){ 3.return getClass().getName(); 4.} 5.public abstract double area(); 6.public abstract double perimeter(); 7.} 8.public class Circle extends Shape{ 9.public double radius; 10.public Circle(double r){ 11.radius = r; 12.} 13.public double area(){ 14.return Math.PI * (radius * radius); 15.} 16.public double perimeter(){ 17.return (2.0 * Math.PI )*radius; 18.} 19.}

Unit 0214 Discovering Abstract Classes – Cont’d 1.public class Rectangle extends Shape{ 2.private double length; 3.private double width; 4. 5.public Rectangle(double length, double width){ 6.this.length = length; 7.this.width = width; 8.} 9.public double area(){ 10.return length * width; 11.} 12.public double perimeter(){ 13.return 2 * (length + width ); 14.} 15.} 1.public class Square extends Rectangle{ 2.public Square(double length){ 3.super(length, length); 4.} 5.}

Unit 0215 Exercises 1.Write a class, TestEmpolyee, that has the following methods: a.printPay(Employee emp) : that prints the name and salary of emp. b.a main method that creates an instance of MonthlyEmployee and that of HourlyEmployee, and prints them using printPay method. 2. Write a class, Manager, that extends MonthlyEmployee, and has another instance variables, bonus and an instance method, setBonus. Override the pay method and update TestEmployee accordinly. 3.Write a class, TestShapes, that creates an instance of each of our shape classes and prints it. 4.Suppose you wish to write an application that stores information about reading materials in a BookShop. Reading materials could be books, journals, magazines and newspapers. Identify the classes (including abstract classes, if any) that are needed for this application. Draw a class diagram for the classes and implement them.