1 19 - Lecture 11 Announcements Assignment 2 Designs due in lab this week Program due Sunday Today Abstract classes Interfaces Using Abstraction to

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 11 Announcements Assignment 2 – Designs due in lab this week – Program due Sunday Today – Abstract classes – Interfaces Operations Determined by Type of Reference Variable • A variable can reference an object whose type is a subclass of the variable type Dog myDog = new Dingo( ); • The type of reference, not the type of the object referenced, determines what operations can be performed (compile time), but the actual type of the object determines which method will be called (run time) • Java is a strongly typed language so the compiler always veriFes that the type of the expression being assigned is compatible with the variable type Using Abstraction to Manage Complexity • An abstraction is a model of a physical entity or activity • Abstraction helps programmers deal with complex issues in a piecemeal fashion • If a higher-level class references a data object only through its methods, then the higher-level class will not have to be rewritten, even if the data representation or the method implementation changes – Information hiding: Concealing the details of a class implementation from users of the class • A Java interface can specify an abstract data type (ADT) and a Java class can implement the ADT Example public abstract class Animal // class is abstract { private String name; public Animal(String nm) // constructor method { name=nm; } public String getName() // regular method { return (name); } public abstract void speak(); // abstract method - note no {} } public class AnimalReference { public static void main(String args[ ]) //assume Cow, Dog and Snake classes exist Animal ref; // set up var for an Animal Cow aCow = new Cow("Bossy"); // makes specific objects Dog aDog = new Dog("Rover"); Snake aSnake = new Snake("Ernie"); // now reference each as an Animal ref = aCow; ref.speak(); ref = aDog; ref.speak(); ref = aSnake; ref.speak(); } Animal Cow Dog Snake Abstract Class Example 1 abstract class A { abstract void callme(); // concrete methods are still allowed in abstract classes void callmetoo( ) {System.out.println("This is a concrete method.");} } class B extends A { void callme() {System.out.println("B's implementation of callme.");} } class AbstractDemo { public static void main(String args[ ]) { B b = new B( ); // you cannot do A a = new A ( ); b.callme( ); b.callmetoo( ); } } Abstract Class Number and the Java Numeric Wrapper Classes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Interfaces & Abstract Classes • An interface can declare methods but does not provide an implementation of those methods – all abstract • An abstract class can have abstract methods, data Felds, and concrete methods – Used when we need a base class for two or more concrete classes that share some attributes & methods • Abstract class differs from a concrete class in that – An abstract class cannot be instantiated – An abstract class can declare abstract methods, which must be implemented in its subclasses – May implement an interface but it doesn’t have to deFne all of the methods declared in the interface • Implementation is left to its subclasses Number x = new Integer (35); int y = x.intValue(); x = new Double (22.3); Interfaces again
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/22/2012 for the course EE 422c taught by Professor Krasner during the Spring '11 term at University of Texas at Austin.

Page1 / 87

1 19 - Lecture 11 Announcements Assignment 2 Designs due in lab this week Program due Sunday Today Abstract classes Interfaces Using Abstraction to

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online