Programming in C# CHAPTER - 11

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Abstract Classes & Interfaces Definitions –abstract methods = Methods that are declared, with no implementation –abstract class = A class with abstract.
Lecture 5: Interfaces.
Object-Oriented Programming
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.
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 10: Introduction to Inheritance
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Object-oriented Programming Concepts
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
UML Class Diagram: class Rectangle
CSCI-383 Object-Oriented Programming & Design Lecture 15.
9/4/2015Abstract classes & Interface1 Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Polymorphism & Interfaces
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Object Oriented Software Development
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
1 Interface Design. 2 concept An interface is a way to describe what classes should do, without specifying how they should do it. It’s not a class but.
© 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.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Programming in Java: lecture 7
Abstract classes and interfaces
Web Design & Development Lecture 9
Sections Inheritance and Abstract Classes
OOP: Encapsulation &Abstraction
Interface, Subclass, and Abstract Class Review
Inheritance and Polymorphism
One class is an extension of another.
UML Class Diagram: class Rectangle
Using local variable without initialization is an error.
One class is an extension of another.
Abstract Classes.
Lecture 4: Interface Design
Java Programming Language
Interfaces.
Advanced Java Programming
Abstract Classes Page
Java – Inheritance.
Java Inheritance.
Section 4: Interfaces Parsing Data and
Inheritance and Polymorphism
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
Presentation transcript:

Programming in C# CHAPTER - 11

Abstract Classes & Interfaces Definitions abstract methods = Methods that are declared, with no implementation abstract class = A class with abstract methods, not meant to be instantiated interface = A named collection of method definitions (without implementations)

Examples Ex: Food is an abstract class. Can you make an instance of food? No, of course not. But you can make an instance of an apple or a steak or a peanut butter cup, which are types of food. Food is the abstract concept; it shouldn’t exist. Skills are interfaces. Can you make an instance of a student, an athlete or a chef? No, but you can make an instance of a person, and have that person take on all these skills. Deep down, it’s still a person, but this person can also do other things, like study, sprint and cook.

Diff b/w Abstract Class & Interface An interface cannot implement any methods, whereas an abstract class can A class can implement many interfaces but can have only one superclass (abstract or not) An interface is not part of the class hierarchy. Unrelated classes can implement the same interface Syntax: abstract class: public class Apple extends Food { … } interface: public class Person implements Student, Athlete, Chef { … }

Why are they Useful? By leaving certain methods undefined, these methods can be implemented by several different classes, each in its own way. Example: Chess Playing Program an abstract class called ChessPlayer can have an abstract method makeMove(), extended differently by different subclasses. public abstract class ChessPlayer { <variable declarations> <method declarations> public void makeMove(); }

Cont.… an interface called ChessInterface can have a method called makeMove(), implemented differently by different classes. public interface ChessInterface { public void makeMove(); }

Implicit Implementation Implicit Interface implementation is easy to implement a single interface on a class or struct. It is simply requires declaration of the class or struct with interface inheritance and the implementation of those interface members.

Explicit Implementation It is necessary to explicitly declare which interface a class or struct member implements. Reasons for Explicit Implementation When there is multiple interface inheritance and two or more interfaces declare a member with the same name. To hide a specific implementation

Mapping and Inheritance Follow these simple rules: An abstract class can’t inherit from more than one other class. Interfaces can inherit from other interfaces, and a single interface can inherit from multiple other interfaces Example: interface Singer { void sing(); void warmUpVoice(); } interface Dancer { void dance(); void stretchLegs(); } interface Talented extends Singer, Dancer { // can sing and dance. Wowwee. }

Where else can Interface be used You can pass an interface as a parameter or assign a class to an interface variable, just like you would to an abstract class. Example: Food myLunch = new Sandwich(); Food mySnack = new Apple(); Student steve = new Person(); //assuming that Person implements Student

Cont.…. If Person has methods eat(Food f) and teach(Student s), the following is possible: Person bob = new Person(); steve.teach(bob); steve.eat(myLunch); System.out.println(“Yum.”);