Abstract classes and interfaces

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
UML Class Diagram: class Rectangle
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance using Java
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
2009 first semister Instructor: Abdalrahman Obidat 1 Revision 3 Instructor: Abdalrahman Obidat.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
C# G 1 CSC 298 Object Oriented Programming Part 2.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
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.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Web Design & Development Lecture 9
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.
Advanced Programming in Java
Advanced Programming in Java
Software Construction
Objects as a programming concept
University of Central Florida COP 3330 Object Oriented Programming
Final and Abstract Classes
Inheritance and Polymorphism
EKT 472: Object Oriented Programming
UML Class Diagram: class Rectangle
CSC 143 Inheritance.
Instructor: Abdalrahman Obidat
Abstract classes and interfaces
Packages and Interfaces
Week 6 Object-Oriented Programming (2): Polymorphism
Interfaces.
Advanced Java Programming
Advanced Programming in Java
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Advanced Programming in Java
Tonga Institute of Higher Education
Fundaments of Game Design
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Abstract classes and interfaces
Abstract Classes and Interfaces
Object Oriented Programming
Chapter 13 Abstract Classes and Interfaces Part 01
Final and Abstract Classes
Abstract Classes and Interfaces
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Abstract classes and interfaces CSC 142 Abstract classes and interfaces [Reading: chapter 13]

protected keyword protected members are visible to any class within the same package any subclass even if it is not in the same package // file B.java package com.javaorbust; public class B {protected int i;} // file D.java import com.javaorbust.B; public class D extends B{ public void update(){ i=6; /* OK */}}

Classes in package and subclasses inside or outside the package Visibility summary Modifier Visibility private Class only none (default) Classes in the package protected Classes in package and subclasses inside or outside the package public All classes

Abstract classes Some classes are so abstract that instances of them shouldn't even exist What does it mean to have an instance of Animal? An abstract class is one that should not or can not be instantiated .  A concrete class can have instances It may not make sense to attempt to fully implement all methods in an abstract class What should Animal.speak() do?

abstract keyword declare a method with the abstract modifier to indicate that it just a prototype. It is not implemented. public abstract void speak(); A class that contains an abstract method must be declared abstract public abstract class Animal{ // more code }

Using abstract classes An abstract class can't be instantiated. An abstract class can contain other non abstract methods and ordinary variables To use it, subclass it. Implement the abstract methods in the subclass If a subclass doesn't implement all of the superclass abstract methods, the subclass is also abstract and must be declared as such. Abstract classes provides a framework to be filled in by the implementor Hierarchy: Shape(abstract) Triangle, Rectangle, Circle

Abstract class example public abstract class Accommodation{ protected boolean vacancy; protected int NumberOfRooms; public abstract void reserveRoom(); public abstract void checkIn(); // etc... } public class Motel extends Accommodation{ //must implement all of the abstract //methods of Accommodation //(if we want the class to be instantiated) //code would follow

Interfaces An interface is a purely abstract class An interface specifies a set of methods that a class must implement (unless the class is abstract) Everything inside an interface is implicitly public public interface Driveable{ // methods are always public (even if // public is omitted) // using abstract is optional boolean startEngine(); void stopEngine(); boolean turn(Direction dir); }

Using interfaces (1) An interface defines some set of behavior for an object. Think of an interface as a badge that can be worn by a class to say "I can do that". public class Automobile implements Driveable { // implements the methods of Driveable public boolean startEngine() { if (notTooCold) engineRunning = true; // more code } // other methods

Using interfaces (2) Interface types act like class types. Variables can be of an interface type formal parameters can be of an interface type A method return type can be an interface type Any object that implements the interface can fill that spot. A class can implement as many interfaces as desired public class C extends B implements I1, I2, I3 { /* class code */} This is how Java deals with multiple inheritance ( C++)

Interface variables An interface can contain constants (static final variables) public interface Scaleable { //static final is implicit and can be //omitted static final int BIG=0, MEDIUM=1, SMALL=2; void setScale(int size); }

subinterfaces An interface can extend another interface, e.g. public interface DynamicallyScaleable extends Scaleable{ void changeScale(int size); } A class that implements a subinterface must implement all of the methods in the interfaces of the hierarchy. An interface can extend any number of interfaces public interface I extends I1, I2 { /* interface code */ }