Lecture 08 Abstract Classes Interfaces

Embed Size (px)

Citation preview

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    1/19

    Abstract Classes

    Lecture08

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    2/19

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    3/19

    Example

    public void breath(){

    " ". . ...

    public void eat(){

    S stem.out. rintln "Livin Thin eatin ..."

    public abstract void walk();

    }

    public class Human extends LivingThing {

    public void walk(){

    System.out.println("Human walks..."); }

    }

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    4/19

    su erclass of abstract class ma be concrete

    su c ass can e a stract even ts superc ass s concrete.

    class is concrete, but its subclasses,ObjectFor example, the, ma be abstract.GeometricOb ectsuch as

    44

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    5/19

    What is an Interface?

    It defines a standard and public way of specifying the

    All methods of an interface are abstract methods ,

    body (implementation of the methods)

    A concrete class must implement the interface (allthe abstract methods of the Interface)

    It allows classes, regardless of their locations in theclass hierarchy, to implement common behaviors

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    6/19

    ,

    public boolean isLess( Object a, Object b);

    ,

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    7/19

    public class Line implements Relation {

    private double x2;private double y1;

    private double y2;

    public Line(double x1, double x2, double y1, double y2){

    this.x1 = x1;

    s.x = x ;

    this.y1 = y1;

    .

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    8/19

    public double getLength(){

    double length = Math.sqrt((x2x1)*(x2x1) +

    *

    return length; }

    ,

    double aLen = ((Line)a).getLength();double bLen = ((Line)b).getLength();

    re urn a en en ;

    public boolean isLess( Object a, Object b){ou e a en = ne a .get engt ;

    double bLen = ((Line)b).getLength();

    return (aLen < bLen);}

    public boolean isEqual( Object a, Object b){

    double aLen = ((Line)a).getLength();

    double bLen = ((Line)b).getLength();return (aLen == bLen); } }

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    9/19

    Omitting Modifiers in Interfaces

    publicand all methods arestaticfinalpublicAll data fields are

    in an interface. For this reason, these modifiers can beabstract

    omitted, as shown below:

    public interface T1 {public static finalint K = 1;

    public abstract void p();

    Equivalent

    public interface T1 {

    int K = 1;

    void p();

    } }

    )..K1T(e.g.,InterfaceName.CONSTANT_NAMEsyntax

    99

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    10/19

    The final Modifier

    The final class cannot be extended:

    na c ass Mat

    ...}

    The final variable is a constant:na s a c ou e = . ;

    The final method cannot be overridden b itssubclasses.

    10

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    11/19

    NOTE

    The modifiers are used on classes and class

    ,

    modifier can also be used on local variablesfinal .

    inside a method.

    11

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    12/19

    Why do we use Interfaces?

    eason To reveal an object's programming interfaceunct ona ty o t e o ect w t out revea ng

    its implementation

    This is the concept of encapsulation

    The implementation can change without affectingt e ca er o t e inter ace

    The caller does not need the implementation at

    e comp e me

    It needs only the interface at the compile time

    ,with the interface type

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    13/19

    Why do we use Interfaces?

    Reason #2 To have unrelated classes implement similar methods

    (behaviors)

    One class is not a subclass of another

    Class Line and class MyInteger

    They are not related through inheritance You want ot to mp ement compar son met o s

    checkIsGreater(Object x, Object y)

    checkIsLess(Object x, Object y)

    c ec s qua ec x, ec y

    Define Comparison interface which has the three abstractmethods above

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    14/19

    Why do we use Interfaces?

    Reason #3

    A class can implement multiple interfaces while it

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    15/19

    Interface vs. Abstract Class

    All methods of an Interface are abstract methods whilesome methods of an Abstract class are abstract

    methods Abstract methods of abstract class have abstract modifier

    An interface can only define constants while abstract

    class can have fields

    any particular class, they are defined independently

    Interfaces themselves have inheritance relationshipamong themselves

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    16/19

    interface, any object you assign to it must be aninstance of a class that implements the interface

    Let's say Person class implements PersonInterface

    interface Person p1 = new Person();

    PersonInterface pi1 = p1; PersonInterface pi2 = new Person();

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    17/19

    Example: Implementing Multiple

    Interfaces

    public class ComputerScienceStudent extends Student implements

    PersonInterface, AnotherInterface, Third interface

    // All abstract methods of all interfaces

    // need to be implemented.

    An interface can only be implemented by classes or extended by other

    interfaces

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    18/19

    However, interfaces can have inheritance

    void doSomething();}

    public interface StudentInterface extends PersonInterface {

    void doExtraSomethin

  • 8/11/2019 Lecture 08 Abstract Classes Interfaces

    19/19

    Homework

    Q1:Problem of Rewriting an Existing Interface.

    uppose we ange an nter ace an a

    some functions in it. What would happen tot e c ass mp ement ng t s nter ace. rov e

    a solution. Q2: When to use abstract classes over

    interface. Explain