AC-S24-OOP-Lect05-Part2-AbstractClasses-Interfaces.pdf
Document Details
Uploaded by LuxuryAbundance
Algonquin College
2024
Tags
Full Transcript
Lect 05B CST8132 OOP Algonquin College Lect 05B Computer Engineering Technology CST8132 OOP Summer, 2024 Based on resources developed by prof....
Lect 05B CST8132 OOP Algonquin College Lect 05B Computer Engineering Technology CST8132 OOP Summer, 2024 Based on resources developed by prof. Howard Rosemblum, James Mwangi, Anu Thomas and Ramanjeet Singh. Prof. Paulo Sousa Algonquin College Lect 05B Computer Engineering Technology Abstract Class / Interface CST8132 OOP Summer, 2024 Based on resources developed by prof. Howard Rosemblum, James Mwangi, Anu Thomas and Ramanjeet Singh. Prof. Paulo Sousa L1 L2 Week 5B: OOP Concepts A2W01 A2W02 A2W09 A2W10 A2W03 A2W11 A2W04 A2W12 A2W05 A2W13 Abstract Class A2W06 A2W14 Interface A2W07 A2W15 A2W08 4 Topics Abstract Classes Inherit, override, customize. Interfaces Contract in OO. UML and Collections Object manipulation. 5 OOP – Week 5 Objects and Abstractions 6 Warm up – Brief Reviews Q1. 2 minute List two things a subclass inherits from a superclass? What kind of relationship is this? 7 Abstraction in Java Hiding implementation details from the user. The user only gets the functionality but is not allowed to see the implementation details. Abstraction in Java is achieved through Abstract classes and Interfaces. 8 Abstract Class Definition: An abstract class is a class that is declared abstract - it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. public abstract class GraphicObject { // declare fields // declare nonabstract methods abstract void draw(); } If a class includes abstract methods, then the class itself must be declared abstract. 9 Abstract Class Notes: If a class is abstract, it cannot be instantiated. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract. To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it. 10 Examples 11 In-Class Practice – for students [Eclipse] 1. Create an abstract class ‘Shape’. 2. Show how subclasses Circle, Rectangle, Triangle individually inherit and customize implementations of abstract methods calcArea(). 12 Interfaces Creating contracts in OO program. Bundle common functionality in classes that are not a part of inheritance hierarchy. An interface is a completely "abstract class" that is used to group related methods with empty bodies: public interface Shapeble { public class Shape implements Shapeble { // declare constants void draw() { // declare methods // implementation void draw(); } } } 13 Interfaces If an abstract class has nothing other than public static final properties and abstract methods, then it could be made into an interface. This is because an interface is a list of abstract methods (and if it has any properties, then they are public static final). In fact, as all methods in an interface are abstract, there is no need to declare them as abstract. 14 Interfaces An interface tells us what we can do with an object, and how we do it. An interface provides us with a set of method signatures. Signature: Method name, number and type of parameters. An interface provides us the return values of methods In other words, an interface tells us how to use the object that implements the interface 15 Interface Example Note: The subclass ‘implements’ the interface Recall: Subclass extends abstract class 16 OOP – Week 5 More about Collections 17 Creation Array: String studentName[] = new String; int currentPosition = 0; studentName[i++] = “Peter”; studentName[i++] = “Paul”; studentName[i++] = “Mary”; ArrayList: List studentName = new ArrayList(); studentName.add(“Peter”); studentName.add(“Paul”); studentName.add(“Mary”); 18 Printing Array: for(int i=0; i0; i--) studentNames[i]=studentNames[i-1]; studentNames=“John"; ArrayList: studentNames.add(0, “John"); 20 Open questions… Any doubts / questions? How we are until now? 21 See you… Enjoy our course and season… 22 OOP – Week 5 Thank you for your attention! Contact: [email protected] 23