MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Abstract Class and Interface
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.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Inheritance using Java
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.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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.,
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
MIT AITI 2004 – Lecture 12 Inheritance. What is Inheritance?  In the real world: We inherit traits from our mother and father. We also inherit traits.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance and Design CSIS 3701: Advanced Object Oriented Programming.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
MIT AITI 2004 Lecture 10 Static and Final. public class MyMath { public double PI = ; public double square (double x) { return x * x; } public.
Interfaces and Polymorphism CS 162 (Summer 2009).
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
BY:- TOPS Technologies
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Web Design & Development Lecture 9
Sixth Lecture ArrayList Abstract Class and Interface
Final and Abstract Classes
Inheritance and Polymorphism
Chapter 5 Hierarchies IS-A associations superclasses subclasses
OOP’S Concepts in C#.Net
Abstract Classes.
CSC 113 Tutorial QUIZ I.
More inheritance, Abstract Classes and Interfaces
Object-Oriented Programming: Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Java Programming
CS18000: Problem Solving and Object-Oriented Programming
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces

What is an Abstract Class?  An abstract class is a class that cannot be instantiated—we cannot create instances of an abstract class.  One or more methods may be declared, but not defined. (The programmer has not yet written code for a few methods).  The declared methods and classes have the keyword abstract in their signature.

public class Employee { private String name; private double salary; public Employee(String n, double s) { name = n; salary = s; } public String getName() { return name; } public double getSalary() { return salary; } public String description() { return "employee with a salary of $ " + salary; } Employee Class

public class Student { private String name; private String course; public Student(String n, String c) { name = n; course = c; } public String getName() { return name; } public String getCourse() { return course; } public String description() { return "a student majoring in " + course; } Student Class

Common Functionality  Student and Employee may have common fields and methods. private String name; getName()  Instead of repeating code, introduce a superclass

Example Hierarchy  Consider the following class structure: Person EmployeeStudent Superclass Subclasses

public class Person { String name; public Person(String n) { name = n; } public String getName() { return name; } Person Class

public class Employee extends Person { // private String name; private double salary; public Employee(String n, double s) { super(n); salary = s; } // public String getName() { return name; } public double getSalary() { return salary; } public String description() { return "an employee with a salary of $" + salary; } Employee Subclass of Person

public class Student extends Person{ // private String name; private String course; public Student(String n, String c) { super(n); course = c; } // public String getName() { return name; } public String getCourse() { return course; } public String description() { return "a student majoring in " + course; } } Revised Student

description() Method  Let’s create Person, Employee, and Student object Person kwame = new Student("Kwame", "CS"); Employee kojo = new Employee("Kojo", ); Student yaa = new Student("Yaa", "Math");  Description of an Employee and a Student returns: employee with a salary of ¢ student majoring in Math  Can we say: kwame.description()  NO! the variable kwame is of type Person, which does not have a description() method defined

public class Person { String name; public Person(String n) { name = n; } public String getName() { return name; } // add a method to return the // description of a Person } Let's Revise Person

public class Person { String name; public Person(String n) { name = n; } public String getName() { return name; } public String description() { return "person named " + name; } Revised Person

description() Revisited  Now we can call the description() method on Objects that are of type Person (instances of a Student, Employee, or Person ) Person kwame = new Person("Kwame"); Person kojo = new Employee("Kojo", ); Person yaa = new Student("Yaa", "Math"); kwame.description(); kojo.description(); yaa.description();

description Method Revisited  Now we can call the description() method on Objects that are of type Person (instances of a Student, Employee, or Person ) Person kwame = new Person("Kwame"); Person kojo = new Employee("Kojo", 20000); Person yaa = new Student("Yaa", "Math"); kwame.description(); // method in Person kojo.description(); // method in Employee yaa.description(); // method in Student  PROBLEM: We don’t want to create instances of Person, just Students and Employee

Abstract Methods Solution: Use the keyword abstract A method labeled abstract is declared but not implemented Rule: An abstract class can have zero or more abstract methods Make description() an abstract method in the class Person

public abstract class Person { String name; public Person(String n) { name = n; } public String getName() { return name; } public abstract String description(); } Abstract Person Class

Abstract Classes  Cannot instantiate or create an object of an abstract class Person jacob = new Person("Jacob") // ERROR!!  An abstract class can have both abstract and non-abstract methods  Abstract methods need to be defined in concrete subclasses (classes that can be instantiated)

Using Abstract Classes Variables can be objects of abstract types Person p = new Student("Greg", "CS"); Here p has the type Person, but references an instance of a non-abstract class, Student

Calls to Abstract Methods Person[] people = new Person[2]; people[0] = new Employee("Evita", ); people[1] = new Student("Greg", "CS"); for (int i = 0; i < people.length; i++) { Person p = people[i]; System.out.println(p.getName() + ", " + p.description()); } What is the output? Evita, an employee with a salary of $ Greg, a student majoring in CS

public abstract class Person { String name; public Person(String n) { name = n; } public String getName() { return name; } // must declare in order to call // method on variable of type Person public abstract String description(); } Abstract Person Class

Advantages Classes can now be very general in a class/type hierarchy. –This allows more abstraction in object oriented programming. Have more control over inheritance in a class/type hierarchy. –Make a class abstract even if there are no abstract methods

Summary of Abstract Classes Partial implementation of a class Cannot be instantiated Use the abstract keyword in their signature. Abstract methods are defined in subclasses

Problem Situation Consider creating an Object that represents an Intern. An Intern behaves like both an Employee and a Student. Problem: a class can only extend ONE other class

Interfaces Solution: Use an interface, which is a set of requirements for a class A class can implement more than one interface Methods in an interface are automatically public and abstract Make Employee an interface

Interface Details An interface is a contract for a class. An interface specifies a set of methods a class must implement. An interface, similar to an abstract class, cannot be instantiated An interface has no constructors, only constants and method declarations. Classes implement interfaces using the keyword implements

Employee Interface public interface Employee { // fields are public static final constants double STARTING_SALARY = ; // methods are automatically public and // abstract; must be overridden in // classes that implement the interface String description(); double getSalary(); }

public class Student { private String name; private String course; public Student(String n, String c) { name = n; course = c; } public String getName() { return name; } public String getCourse() { return course; } public String description() { return "a student majoring in " + course; } Student Class Revisted

class Intern extends Student implements Employee { private double income; public Intern(String n, String c) { super(n, c); income = STARTING_SALARY; } public double getSalary() { return income; } public String description() { return "intern majoring in "+ super.getCourse() + "with an income of $" + income; } Intern Class

Using Intern Class public static void main(String[] args) { Intern irish = new Intern("Conor", "Math"); System.out.println(irish.getName() + "," + irish.description()); } Output: Conor, intern majoring in Math with an income of $

Variable Types A variable may have the type of an abstract class, an interface, or a concrete class (a non-abstract class). Because only a concrete class can be instantiated, an object may only have the type of a concrete class. All of these are valid variable declarations: Intern a = new Intern("Conor", "Math"); Student b = new Intern("Conor", "Math"); Employee c = new Intern("Conor", "Math");

Variable vs Object Types (Again) Intern a = new Intern("Conor", "Math"); Student b = new Intern("Conor", "Math"); Employee c = new Intern("Conor", "Math"); These expressions will not compile: b.getSalary() // Student does not have getSalary c.getCourse() // Employee does not have getCourse But all of these will: ((Employee)b).getSalary() ((Intern)b).getSalary() ((Student)c).getCourse() ((Intern)c).getCourse()

Interface Rules Interfaces may specify but do not implement methods. A class that implements the interface must implement all its methods. Interfaces cannot be instantiated. Interfaces may contain constants.