Fundamentals of computing 1 - Lecture title: Abstract class and interface

Create an class RankingStudent that: Initiate an array of 10 Students Using method sort of ArrayList class to sort the Student

ppt27 trang | Chia sẻ: nguyenlam99 | Lượt xem: 944 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Fundamentals of computing 1 - Lecture title: Abstract class and interface, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Lecture Title: Abstract class and InterfaceFundamentals of Computing 1AgendaAbstract InterfaceAbstract classAbstract class is a class that is not completely implemented.Abstract specify patterns for methods that it subclasses in the hierarchy must implement and abstract does not implement these methods.We do not intend to initiate object of that abstract method.Abstract classSyntax to declare the abstract class abstract class {}A class that is declared as abstract is known as abstract class and can not be instantiated It needs to be extended and all of its abstract methods need to be implemented.A subclass must implement all abstract methods in its superclass, otherwise, it will be an abstract method.Abstract methodA method that is declared as abstract and does not have implementation is known as abstract method. Syntax to define the abstract methodabstract return_type (argument list);//no curly braces{}Example of abstract classOutput: run safelyBike+run()Honda+run()Abstract class having non-abstract methodOutput: running safely.. gear changeBike+run() +changeGear()Honda+run()Abstract class having constructor, field and non-abstract methodOutput: constructor is invoked running safely.. it has two wheels its limit = 30Bikelimit: int+Bike()+getDetails()+run() Honda+run()Another exampleAgendaAbstract InterfaceExample 1basketball courtsExample 2Club regulations:* Club member must: Pay fee Adverise* Club member can also join other activitiesInterfaceAn interface is a blueprint of a class. It has only static constants and abstract methodsAn interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.Interface (cont.)Interface syntax modifier interface InterfaceName { /* Constant declarations */ /* Method signatures */ } Using interface class ClassName implements InterfaceName { } The java compiler converts methods of interface as public and abstract, data members as public, final and static by default.Interface (cont.)Interface (cont.)An interface is treated like a special class in Java. Each interface is compiled into a separate bytecode file, just like a regular class.As with an abstract class, you cannot create an instance from an interface using the new operatorHowever, a interface can be use as a data type for a variable, as the result of casting, and so on. Simple example of InterfaceOutput: HelloMultiple inheritance by interfaceOutput: Hello WelcomeComparable interfaceTo be comparable, there must be an ordering between objectsComparable - ExampleComparable - Example Create an abstract class Person that has the following fields:id, full name, day of birth, And methods as follows:constructor settersgettersabstract method double average() Comparable - Example Create an class Student that extends class Person, implements interface Comparable and:has additional fields: math, physics, chemistryimplementation of method double average()implementation of method double average() Comparable - Example Create an class RankingStudent that:Initiate an array of 10 StudentsUsing method sort of ArrayList class to sort the Student Comparable - ExampleExtended interfaceOutput: Hello WelcomeQuestionCan abstract class be instantiated?No, it can notDoes an abstract method have implementation? No, it does not have implementationWhy use Interface?By interface, we can support the functionality of multiple inheritanceWhat does the java compiler convert methods of interface as?public and abstractWhat does the java compiler convert data members as by default?public, final and staticWhat we have coveredAbstract Interface

Các file đính kèm theo tài liệu này:

  • pptlecture_10_abstract_class_and_interface_3601.ppt
Tài liệu liên quan