190 likes | 217 Views
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?
E N D
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? • 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
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(); }
Abstract Classes (cont.) • Usage (cont.) • Classes extending the abstract class should implement the abstract methods, else should be tagged abstract e.g:
Interfaces Design Abstraction and a way for loosing realizing Multiple Inheritance
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.
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..
Interface - Example <<Interface>> Speaker speak() Politician Reporter Lecturer speak() speak() speak()
Interfaces Definition • Syntax (appears like abstract class): • Example: interface InterfaceName { // Constant/Final Variable Declaration // Methods Declaration – only method body } interface Speaker { public void speak( ); }
Interfaces Example interface Item { public static final int CODE=100; public static final string NAME = “ Fan”; public void disp(); }
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 }
Implementing Interfaces interface Speaker { public void speak( ); } class Politician implements Speaker { public void speak() { System.out.println(“Talk politics”); } }
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 “); } }
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 }
Extending Interface interface ItemConst { public static final ICODE =100; public static string NAME = “fan”; } interface Item extends ItemConst { void disp(); }
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 }
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:
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 }
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.