Abstract Classes.

Slides:



Advertisements
Similar presentations
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Advertisements

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
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.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Chapter 11: Implementing Inheritance and Association Visual Basic.NET Programming: From Problem Analysis to Program Design.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Chapter 10: Introduction to Inheritance
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
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.
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.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
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.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Abstract Classes. Review PA – 3 Design Considerations /Web/CS239/programs/pa3/draft.php ?harrisnlCS239
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.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Object Oriented programming Instructor: Dr. Essam H. Houssein.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
© 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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
CET203 SOFTWARE DEVELOPMENT Session 2A Inheritance (programming in C#)
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Advanced Programming in Java
Sections Inheritance and Abstract Classes
Inheritance and Polymorphism
EKT 472: Object Oriented Programming
One class is an extension of another.
Object Oriented Programming
Lecture 14 - Abstract Classes
One class is an extension of another.
Class Inheritance (Cont.)
Chapter 9: Polymorphism and Inheritance
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance.
Advanced Programming Behnam Hatami Fall 2017.
Abstract Classes Page
Java Programming, Second Edition
Java Inheritance.
Fundaments of Game Design
Inheritance.
Chapter 14 Abstract Classes and Interfaces
Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Abstract Classes and Interfaces
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

Abstract Classes

Abstract Classes As you move up inheritance hierarchy, more general and probably more abstract Instead of employee, student, parent  person Forms a basis for other classes Common attributes (such as name) Common methods (getDescription)

Abstract Methods getDescription For student  a student majoring in math For employee  an employee with salary $50K Easy to implement for Student and Employee classes, but Person class? getDescription  empty string? Use abstract method public abstract String getDescription ( ); //no implementation required

Abstract Classes A class with one or more abstract methods must be declared to be abstract abstract class Person { … public abstract String getDescription ( ); } A class with no abstract methods may also be abstract

C++ Abstract Classes A class with one or more virtual methods is abstract class Person { … public: virtual string getDescription ( ) = 0; } No special keyword to denote abstract classes

Concrete data and methods Can have concrete data and methods as well abstract class Person { public Person (String n) { name =n; } public abstract String getDescription ( ); public String getName ( ) { return name; private String name; You WANT to move as much functionality into superclass (whether or not it is abstract)

Instances Abstract classes cannot be instantiated, cannot create objects of that class new Person (“Vince Vahn”) Can create objects of concrete subclasses. Can create object variables of an abstract class, but it must refer to an object of nonabstract subclass! Person p = new Student (“Vince Vahn”, “Math”);

Extending Classes Two options Leave some or all of abstract methods abstract (subclass is abstract) Define all methods

Example Class Student extends Person { public Student (String n, String m) { super (n); major = m; } public String getDescription ( ) //method defined { return “A student majoring in “ + major; private String major;

Invoking methods Person [ ] people = new Person [2]; People [0] = new Employee (…); People [1] = new Student ( …); Person p = people [i]; System.out.println (p.getName() + “, “ + p.getDescription());

Invoking methods Not a call to undefined method, p refers to object of concrete class, and it is defined for these classes Do NEED the abstract method to invoke!

The End