Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Abstract Classes, Interfaces - Web Design and Development - Lecture Slides, Slides of Web Design and Development

Abstract Classes, Interfaces, Class Shape Hierarchy, Problem AND Requirements, Example of abstract class, Circle.java, Driver class, Compile, Execute, Interfaces Definition are terms you can learn in this lecture along with others.

Typology: Slides

2011/2012

Uploaded on 11/06/2012

parasad
parasad 🇮🇳

4.5

(56)

129 documents

1 / 29

Toggle sidebar

Related documents


Partial preview of the text

Download Abstract Classes, Interfaces - Web Design and Development - Lecture Slides and more Slides Web Design and Development in PDF only on Docsity! Web Design & Development Lecture 9 Docsity.com Abstract Classes a and Interfaces Docsity.com Problem AND Requirements • Suppose that in order to exploit polymorphism, we specify that 2-D objects must be able to compute their area. – All 2-D classes must respond to area() message. • How do we ensure that? – Define area method in class Shape – Force the subclasses of Shape to respond area() message • Java’s Solutions – Abstract Classes – Interfaces Docsity.com Abstract Classes • Idea – To define only part of an implementation  Can contain instance variables & methods that are fully implemented – Leaving the subclasses to provide the details • Any class with an abstract method must be declared abstract – However you can declare a class abstract that has no abstract method. – An abstract method has no implementation (known in C++ as a pure virtual function) Docsity.com Abstract Classes • If subclass overrides all abstract methods of the superclass, than it becomes a concrete class otherwise we have to declare it as abstract or we can not compile it • Any subclass can override a concrete method inherited from the superclass and declare them abstract • An abstract class cannot be instantiated • However references to an abstract class can be declared – Can point to the objects of concrete subclasses Docsity.com Circle.java // providing definition of abstract method public void calculateArea () { double area = 3.14 * (radius * radius); System.out.println(“Area: ” + area); } }//end of class Docsity.com Test.java (Driver class) public class Test { public static void main (String args[ ]){ //can only create references of abstract class Shape s = null; // Shape s1 = new Shape(); //cannot instantiate abstract class //can point to the concrete subclass s = new Circle(); s.calculateArea(); } } Docsity.com AO Chae eC :NexamplesSNabstract> javac Shape. java :\examplesS\abstract> javac Circle. java :\examples\abstract> javac Test. java i :\examples‘\abstract> java Test Area: 314.0 D:\examples ‘abstract > «| | Lb oe Bocsity=com Interfaces Definition Example • Syntax (appears like abstract class): • All methods are abstract and public by default • All constants are static and final by default public interface Speaker { public void speak( ); } Docsity.com Implementing (Using) Interfaces • Classes Implement interfaces – Implementing an interface is like signing a contract. – A class that implements an interface will have to provide the definition of all the methods that are present inside an interface“ – If the class does not provide definitions of all methods, the class would not compile. We have to declare it as an abstract class in order to get it compiled. • “Responds to” relationship – Relationship between a class and interface Docsity.com Interface - Example speak() Politician Coach <<Interface>> Speaker speak() speak() Lecturer speak() Docsity.com area ORS CURFA CITC D:\examples\interface) javac Student. java Student. java:l: Student is not abstract and does not override abstract method print() in Printable public class Student implements Printablef 1 error D:\examplesNinterfaced_ 2 ® Bocsity=com Example Code (cont.) public class Student implements Printable{ private String name; private String address; public String toString () { return "name:"+name +" address:"+address; } public void print() { System.out.println("Name:" +name+" address"+address); } } Implementing Interface (Modification) Docsity.com Compile Docsity.com Interface based Polymorphism Docsity.com Review again – Interface Example speak() Politician Coach <<Interface>> Speaker speak() speak() Lecturer speak() Docsity.com Example: Interface based Polymorphism /* Speaker interface is implemented by the Politician, Coach and Lecturer class. */ public class Test{ public static void main (String args[ ]) { Speaker sp = null; System.out.println("sp pointing to Politician"); sp = new Politician(); sp.speak(); System.out.println("sp pointing to Coach"); sp = new Coach(); sp.speak(); System.out.println("sp pointing to Lecturer"); sp = new Lecturer(); sp.speak(); } } Docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved