DFC 3013 OBJECT ORIENTED PROGRAMMING Topic 3 6
DFC 3013 OBJECT ORIENTED PROGRAMMING Topic 3. 6: Abstract Classes DNS 3 A / DNS 3 B
Abstract Class • A class can be instantiated, which means an object can be created for a class. Such classes are called as concrete classes. • The classes that cannot be instantiated are called as abstract classes.
Abstract Class (Contd. . ) • The abstract class gives a general structure for a class. • This structure can be implemented by their sub classes in different ways.
Abstract Class (Contd. . ) • Consider a situation, where all the sub classes of a class must implement a method of its super class. When you forget to implement the method in a sub class. • In such cases, you can declare the super class and the method in the super class as abstract. • Now, a compiler error occurs if you forget to implement all the abstract methods of the super class in your sub class.
Hands-On! • Program Abst. java illustrates the use of Abstract classes in java.
Hands-On! 4. Write a program to define an abstract class Shape which has a method area() in it. Create two more classes Rectangle and Square that extends the Shape class. The class Rectangle should implement the method area() to find the area of rectangle. The class Square should implement the method area() to find the area of square.
Access Specifiers and Modifiers • In some situations, the member variables or methods of one class may or may not be allowed to access other class members. • The access of the members of a class can be controlled by Access Specifiers. • The 3 access specifiers in Java are Ø public Ø private Ø protected.
Access Specifiers and Modifiers • Public – When a class member is declared as public, it can be accessed by the code in any class. • Private - When it is declared as private it can be accessed only by the code in the same class where it is declared. • Protected- When it is declared as protected, it can be accessed by the code in same class and inherited classes.
Class Modifier abstract final Effect of the Modifier The class declared as abstract cannot be instantiated. The class declared as final cannot be extended.
Variable Modifiers Modifier Effect of the Modifier static Specifies that the variable is common to all the objects of that class. final Specifies that the value of the identifier cannot be changed.
Method Modifiers Modifier abstract final static Function Specifies that the method cannot be implemented in this class and it should be implemented in its sub classes. Specifies that the method cannot be overridden. Specifies that the method is common to all the objects. These methods can be accessed by using the class name.
Summary In this presentation, you learnt the following • The classes that cannot be instantiated are called as Abstract classes. • The method in the sub class overrides the method in the super class. This is known as Method Overriding. • The three access specifiers in Java are public, private and protected.
DFC 3013 OBJECT ORIENTED PROGRAMMING Topic 3. 7: Interface DNS 3 A / DNS 3 B
Declaring Interfaces: The interface keyword is used to declare an interface. Here is a simple example to declare an interface: Given is an example of an interface: /* File name : Name. Of. Interface. java */ import java. lang. *; //Any number of import statements public interface Name. Of. Interface { //Any number of final, static fields //Any number of abstract method declarations }
Interfaces have the following properties: An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface. Each method in an interface is also implicitly abstract, so the abstract keyword is not needed. Methods in an interface are implicitly public.
Example /* File name : Animal. java */ interface Animal { public void eat(); public void travel(); }
Example /* File name : Mammal. Int. java */ public class Mammal. Int implements Animal{ public void eat(){ System. out. println("Mammal eats"); } public void travel(){ System. out. println("Mammal travels"); } public int no. Of. Legs(){ return 0; } public static void main(String args[]){ Mammal. Int m = new Mammal. Int(); m. eat(); m. travel(); } }
Output Mammal eats Mammal travels
Extending Interfaces: An interface can extend another interface, similarly to the way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface. The following Sports interface is extended by Hockey and Football interfaces.
//Filename: Sports. java public interface Sports { public void set. Home. Team(String name); public void set. Visiting. Team(String name); } //Filename: Football. java public interface Football extends Sports { public void home. Team. Scored(int points); public void visiting. Team. Scored(int points); public void end. Of. Quarter(int quarter); } //Filename: Hockey. java public interface Hockey extends Sports { public void home. Goal. Scored(); public void visiting. Goal. Scored(); public void end. Of. Period(int period); public void overtime. Period(int ot); }
The Hockey interface has four methods, but it inherits two from Sports; thus, a class that implements Hockey needs to implement all six methods. Similarly, a class that implements Football needs to define three methods from Football and the two methods from Sports.
Done Chapter 3. Thank you!!
- Slides: 26