Abstract methods and 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.
Inheritance. Inheritance  New class (derived class) is created from another class (base class).  Ex. EmployeeEmployee  HourlyEmployee  SalariedEmployee.
Inheritance Juan Marquez 12_inheritance.ppt
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,
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
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.
Chapter 8 Polymorphism and Abstract Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 9: – Polymorphism Copyright © 2008 Xiaoyan Li.
Chapter 7 Inheritance Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 10: – Abstract Classes Copyright © 2008 Xiaoyan Li.
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CMSC 202 Inheritance II. Version 10/102 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Chapter 8 Polymorphism and Abstract Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Method signature Name and parameter list public static void test() public static int test() => Syntax error, Duplicate method test You cannot declare more.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
CMSC 202 Inheritance I Class Reuse with Inheritance.
Inheritance and Design CSIS 3701: Advanced Object Oriented Programming.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
COMP Inheritance Basics Yi Hong June 09, 2015.
Interfaces and Inner Classes
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
Abstract methods and classes. Abstract method A placeholder for a method that will be fully defined in a subclass.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
More About Java and Java How to Program By Deitel & Deitel.
Polymorphism and Abstract Classes
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Using local variable without initialization is an error.
Computer Science II Exam 1 Review.
Abstract Classes.
Inheritance 2nd Lecture
ارث بری 2 استفاده ی مجدد از کلاس توسط وراثت
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Class Inheritance (Cont.)
Polymorphism 2nd Lecture
null, true, and false are also reserved.
Comp 249 Programming Methodology
Inheritance I Class Reuse with Inheritance
ارث بری 2 استفاده ی مجدد از کلاس توسط وراثت
An Introduction to Java – Part II
Inheritance 2nd Lecture
CMSC 202 Inheritance.
Inheritance.
Class Reuse with Inheritance
Inheritance 2nd Lecture
مظفر بگ محمدی دانشگاه ایلام
مظفر بگ محمدی دانشگاه ایلام
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
CMSC 202 Inheritance II.
Chapter 8 Abstract Classes.
Chapter 7 Inheritance.
Presentation transcript:

Abstract methods and classes

Abstract method A placeholder for a method that will be fully defined in a subclass.

Abstract method An abstract method has a complete method heading with the addition of the keyword abstract. Cannot be private or final. Ex. abstract public double getPay ( ); abstract public void doSomething ( int count );

Abstract class A class that has at least one abstract method is called an abstract class. The class definition must have the keyword abstract. Ex. abstract public class Feet { … abstract void doSomething ( int count ); }

Abstract vs. concrete class A class without any abstract methods is called a concrete class.

Points to remember You cannot create an instance of an abstract class. Ex. Employee joe = new Employee(); //the above is illegal if Employee is abstract

Points to remember An abstract class is a type. Therefore you can use abstract classes as parameters to functions. Ex. (perfectly “legal”) public abstract class Employee { … public boolean samePay ( Employee other ) { }

public abstract class Employee { private String name; private Date hireDate; public abstract double getPay( ); public Employee( ) name = "No name"; hireDate = new Date("Jan", 1, 1000); //Just a place holder. } /** Precondition: Neither theName nor theDate are null. */ public Employee(String theName, Date theDate) if (theName == null || theDate == null) System.out.println("Fatal Error creating employee."); System.exit(0); name = theName; hireDate = new Date(theDate);

public Employee(Employee originalObject) { name = originalObject.name; hireDate = new Date(originalObject.hireDate); } public boolean samePay(Employee other) if (other == null) System.out.println("Error: null Employee object."); System.exit(0); //else return (this.getPay( ) == other.getPay( )); public String getName( ) return name; public Date getHireDate( ) return new Date(hireDate); ...

Points to remember An abstract class is a type. Therefore you can use abstract classes as variable types for concrete classes derived from the abstract class. Example follows…

Points to remember Ex. (perfectly “legal”) public abstract class Employee { … } public class HourlyEmployee extends Employee { public class SalaryEmployee extends Employee { Employee e = new HourlyEmployee();

Example Employee SalariedEmployee HourlyEmployee