1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

Java Programming Abstract classes and Interfaces.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Interfaces.
Inheritance Contents 1.Fundamentals 2.Generalization 3.Constructors and Derived Classes 4.Visibility Modifiers 5.Abstract Classes 6.Interfaces.
1 Derived Classes and Inheritance Chapter Objectives You will be able to: Create derived classes in C#. Understand polymorphism. Write polymorphic.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
Object-Oriented Programming with Java Java with added Swing Lecture 3.
Java Class and Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-4: Interfaces reading: self-check: exercises: #11.
Using Jeroo To Teach Object-Oriented Concepts By Christian Digout.
I NTERFACES Prepared by Miss Simab Shahid Lecturer computer Science and Software Engineering department, University of Hail Chapter.
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.
CS305j Introduction to Computing Inheritance and Polymorphism 1 Topic 26 Introduction to Inheritance and Polymorphism "One purpose of CRC cards [a design.
ICS 201 Introduction to Computer Science Problem Solving #4.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
 Definition: The ability to derive child classes (sub- classes) from parent classes (super-classes)  Characteristics:  Methods and instance variables.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
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.,
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
EE 422C Interfaces Day 5. 2 Announcements SVN has Project 2. –Partly due next week. –SVN update to get it in your repository. See Piazza for Grocery List.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4.
Interfaces and Inner Classes
Interfaces and Polymorphism CS 162 (Summer 2009).
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
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.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Modern Programming Tools And Techniques-I
Abstract classes and interfaces
Web Design & Development Lecture 9
Chapter 15 Abstract Classes and Interfaces
Sixth Lecture ArrayList Abstract Class and Interface
Interface.
University of Central Florida COP 3330 Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
Abstract classes and interfaces
null, true, and false are also reserved.
Interface.
Interfaces.
CSE 1030: Implementing GUI Mark Shtern.
class PrintOnetoTen { public static void main(String args[]) {
Chapter 14 Abstract Classes and Interfaces
Abstract classes and interfaces
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class is similar to class specification Contains only a list of methods & their parameters Omitting the implementation of each method Contains NO data except constants public interface Shape { public static final double PI= ; public double area(); public double circumference(); public void whoAreYou(); // prints shape name }

2 Class Circle implements Shape When a class implements an interface, it must define the methods as specified by that interface public class Circle implements Shape { private double radius; public Circle(double r){ radius = r ; } public double area(){ return PI * radius * radius ; } public double circumference(){ return 2 * PI * radius ; } public void whoAreYou() { System.out.println("I am a circle of radius= “ +radius); }

3 Class Rectangle implements Shape public class Rectangle implements Shape { private double length, width; public Rectangle(double l, double w){ length = l; width = w; } public double area(){ return length * width; } public double circumference(){ return 2 * (length + width); } public void whoAreYou() { System.out.println( "I am a rectangle of length="+length+ " and width="+width); }

4 Class TestShapes public class TestShapes { public static void main(String args[]){ Shape shape1 = new Circle(3.5); Shape shape2 = new Rectangle(3, 4); displayInfo(shape1); displayInfo(shape2); } // Continue … on next slide

5 Class TestShapes … Continued public static void displayInfo(Shape shape){ System.out.println("Checking the shape type:"); if(shape instanceof Circle ) System.out.println( "shape is a reference to a Circle object"); else if (shape instanceof Rectangle) System.out.println( "shape is a reference to a Rectangle object"); System.out.println("Printing shape information"); shape.whoAreYou(); System.out.println("My Area is "+ shape.area() ); System.out.println("My Circumference is " + shape.circumference( ) ); System.out.println(); } } // end class TestShapes

6 Notes on Interface Class interfaces can’t be instantiated. An interface can contain constant declarations in addition to method declarations. All methods declared in an interface are implicitly public, so the public modifier can be omitted. All constant values defined in an interface are implicitly public, static, and final. Therefore, these modifiers can be omitted.

7 Introduction to Abstract Classes Methods in Shape considered abstract (i.e., not defined) We could have written public abstract double area() ; in the interface Abstract class Similar to an interface may contain both abstract methods and defined methods In an interface, all methods must be abstract To use abstract class, we extend the class using inheritance Neither interfaces nor abstract classes can be instantiated