1 / 19

Introduction to abstract

Introduction to abstract. if class contains abstract methods ( Unimplemented ) then that should be Declare as abstract class. We can’t create the object of abstract class Abstract class can contains Non- abstract methods. Abstract Classes. What is an abstract class?

welkerj
Download Presentation

Introduction to abstract

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to abstract • if class contains abstract methods (Unimplemented) then that should be Declare as abstract class. • We can’t create the object of abstract class • Abstract class can contains Non- abstract methods

  2. Abstract Classes • What is an abstract class? • Not all behaviours of the class are defined • Abstract classes cannot be instantiated. So, to have the same behaviour, the class has to be extended • Non-abstract children of the abstract class HAVE TO define the missing behaviour(s). changeGears() will not implemented in the parent class Bike but will be implemented in the subClass moutainBike() • When to define an abstract class?? • There is a behavior which all subclasses show but implementation is different Shape : Methods : Perimeter, Area Circle, Square inherit shape and calculate perimeter and area in different ways. So, perimeter and area will be abstract methods which a particular shape will implement

  3. Abstract Classes (cont.) • Usage: • Methods which are not defined should be tagged as abstract e.g.: abstract void changeGears(); • Classes having one or more abstract methods should also be tagged as abstract e.g.: abstract class Bike{ ……………. abstract void changeGears(); }

  4. Abstract Classes (cont.) • Usage (cont.) • Classes extending the abstract class should implement the abstract methods, else should be tagged abstract e.g:

  5. Interfaces Design Abstraction and a way for loosing realizing Multiple Inheritance

  6. Interfaces • Interfaceis a basically class . • Can contain only constants (static & final variables)andabstract method(no implementation) • Use when a number of classes share a common interface. • Each class should implement the interface.

  7. Interfaces • Therefore, it is the responsibility of the class that implements an interface to define the code for methods. • A class can implement any number of interfaces, but cannot extend more than one class at a time..

  8. Interface - Example <<Interface>> Speaker speak() Politician Reporter Lecturer speak() speak() speak()

  9. Interfaces Definition • Syntax (appears like abstract class): • Example: interface InterfaceName { // Constant/Final Variable Declaration // Methods Declaration – only method body } interface Speaker { public void speak( ); }

  10. Interfaces Example interface Item { public static final int CODE=100; public static final string NAME = “ Fan”; public void disp(); }

  11. Implementing Interfaces • Interfaces are used like super-classes whose properties are inherited by classes. This is achieved by creating a class that implements the given interface as follows: class ClassName implements InterfaceName [, InterfaceName2, …] { // Body of Class }

  12. Implementing Interfaces interface Speaker { public void speak( ); } class Politician implements Speaker { public void speak() { System.out.println(“Talk politics”); } }

  13. Implementing Interfaces class Reporter implements Speaker { public void speak() { System.out.println(“News Talks”); } } class Lecturer implements Speaker { public void speak() { System.out.println(“Talks About OOP’s “); } }

  14. Extending Interfaces • Like classes, interfaces can also be extended. The new sub-interface will inherit all the members of the super interface in the manner similar to classes. This is achieved by using the keyword extends as follows: interface InterfaceName2 extendsInterfaceName1 { // Body of InterfaceName2 }

  15. Extending Interface interface ItemConst { public static final ICODE =100; public static string NAME = “fan”; } interface Item extends ItemConst { void disp(); }

  16. Inheritance and Interface Implementation • A general form of interface implementation: • This shows a class can extended another class while implementing one or more interfaces. It appears like a multiple inheritance (if we consider interfaces as special kind of classes with certain restrictions or special features). class ClassName extends SuperClass implements InterfaceName [, InterfaceName2, …] { // Body of Class }

  17. Student Sports extends Exam implements extends Results Student Assessment Example • Consider a university where students who participate in the national games or Olympics are given some grace marks. Therefore, the final marks awarded = Exam_Marks + Sports_Grace_Marks. A class diagram representing this scenario is as follow:

  18. Software Implementation class Student { // student no and access methods } interface Sport { // sports grace marks (say 5 marks) and abstract methods } class Exam extends Student { // example marks (test1 and test 2 marks) and access methods } class Results extends Exam implements Sport { // implementation of abstract methods of Sport interface // other methods to compute total marks = test1+test2+sports_grace_marks; // other display or final results access methods }

  19. Interfaces and Software Engineering • Interfaces, like abstract classes and methods, provide templates of behaviour that other classes are expected to implement. • Separates out a design hierarchy from implementation hierarchy. This allows software designers to enforce/pass common/standard syntax for programmers implementing different classes. • Pass method descriptions, not implementation • Java allows for inheritance from only a single superclass. Interfaces allow for class mixing. • Classes implement interfaces.

More Related