Summary

This document is a past paper for an object-oriented programming (OOP) course. It covers the fundamentals of abstract classes focusing on examples, rules, and explanations. The paper includes examples using classes like Pet, Cat, and Animal.

Full Transcript

IT1908 ABSTRACT CLASSES 2. An abstract class may include non-abstract methods and variables. Fundamentals...

IT1908 ABSTRACT CLASSES 2. An abstract class may include non-abstract methods and variables. Fundamentals Example: An abstract class is created to ensure that subclasses will public abstract class Pet { implement necessary methods. public abstract void eat(); Example 1: public void drink() { public abstract class Pet { } public abstract void eat(); } } 3. An abstract class cannot be marked as final because a final public class Cat extends Pet { class cannot be extended by another class. public void eat() { } Example: public final abstract class Pet { } } 4. An abstract class that extends another abstract class inherits Explanation: The Cat class extends its parent class, Pet. all of its abstract methods as its own abstract methods. Hence, it has to use the eat() method. 5. The first concrete class (non-abstract class that extends an Example 2: abstract class) is required to implement all inherited abstract public abstract class Pet { methods. public abstract void eat(); Example: } public abstract class Animal { public class Cat extends Pet { //does not compile public abstract void sleep(); } } public abstract class Pet extends Animal { Explanation: The Cat class will not compile because it has to public abstract void eat(); use the eat() method since it is declared in its parent class, } Pet. public class Cat extends Pet { public void sleep() { An abstract method has no body and is required to be } implemented by subclasses. It is declared without braces but public void eat() { with a semicolon. } Example: abstract void draw(); } Rules in Defining Abstract Classes Rules in Defining Abstract Methods 1. An abstract class cannot be instantiated directly. 1. An abstract method may only be defined in an abstract class. public abstract class Pet { Example: public static void main(String[] args) { public class Pet { Pet p = new Pet(); public abstract void eat(); //does not compile } } } 07 Handout 1 *Property of STI  [email protected] Page 1 of 2 IT1908 2. An abstract method cannot be marked as final because a final method cannot be overridden in a subclass. Example: public final abstract void draw(); 3. An abstract method cannot be marked as private since that method is only accessible to the class where it belongs to. Example: private abstract void eat(); 4. To override an abstract method, declare a new method with the same name, parameter list, and return type as the method in the parent class. The method in the subclass must be at least as accessible as the method in the parent class. Example: public abstract class Pet { public abstract int getAge(); } public class Cat extends Pet { public double getAge() { return 5.0; } } References: Oracle Docs (n.d.). Citing sources. Retrieved from https://docs.oracle.com/javase/tutorial/java/javaOO/index.html Savitch, W. (2014). Java: An introduction to problem solving and programming (7th ed.). New Jersey: Pearson Education, Inc. 07 Handout 1 *Property of STI  [email protected] Page 2 of 2 IT1908 INTERFACES The following interface definitions are equivalent: //Interface 1 Fundamentals public interface Drinkable { An interface is a collection of related abstract public methods. int getNumGlasses(); It can also contain default methods, static methods, and } constants declaration. //Interface 2 An interface is created and defined using the interface public abstract interface Drinkable { keyword. public abstract int getNumGlasses(); public interface Drinkable { } public static final int MAX_GLASSES = 0; public abstract int getNumGlasses(); Rules in Inheriting an Interface } 1. An interface that extends another interface, as well as an Explanation: Methods are implicitly public; there is no need abstract class that implements an interface, inherits all of the to use this keyword in the method declaration. Constants are abstract methods as its own abstract methods. implicitly public, static, and final. These keywords are Example 1: not required in the declaration of a constant. //Interface 1 A class can use an interface by adding an implements public interface Drinkable { keyword in the class declaration. public int getNumGlasses(); public class Water implements Drinkable { } public int getNumGlasses() { //Interface 2 return 8; public interface Potable { } public String getContent(); } } A class can implement multiple interfaces. //An interface inheriting from the 2 interfaces Example: public interface Sellable extends Drinkable, public class Water implements Drinkable, Potable { } Potable { } Explanation: Any class that implements the Sellable Rules in Defining an Interface interface must provide an implementation for the methods in 1. An interface cannot be instantiated directly. the parent interfaces, getNumGlasses() and getContent(). Drinkable d = new Drinkable(); //does not compile Example 2: 2. It is not required for an interface to have a method. //Interface 1 public class Water implements Drinkable { } public interface Drinkable { 3. An interface cannot be marked as final. public int getNumGlasses(); public final interface Drinkable { } } 4. Interfaces are assumed to abstract. //Interface 2 public interface Drinkable { } is equivalent to public interface Potable { public abstract interface Drinkable { } public String getContent(); 5. All abstract, default, and static methods in an interface are } implicitly public. 08 Handout 1 *Property of STI  [email protected] Page 1 of 2 IT1908 //An abstract class implementing the 2 interfaces public interface Drinkable { } public abstract class Water implements Drinkable, public interface Potable implements Drinkable { } Potable { } 2. A class can implement two (2) or more interfaces that contain Explanation: The abstract class (Water) inherits the abstract the same method. methods of the interfaces (Drinkable, Potable) but is not //Interface 1 required to implement them. public interface Drinkable { 2. The first concrete class that implements an interface, or public int getNumGlasses(); extends an abstract class that implements an interface, must } provide an implementation for all of the inherited abstract //Interface 2 methods. public interface Potable { //Interface 1 public int getNumGlasses(); public interface Drinkable { } public int getNumGlasses(); public class Water implements Drinkable, Potable { } public int getNumGlasses() { //Interface 2 return 8; public interface Potable { } public String getContent(); } } 3. A class can implement interfaces containing methods that //An abstract class implementing the 2 interfaces overload. public abstract class Water implements Drinkable, //Interface 1 Potable { } public interface Drinkable { } public class Juice extends Water implements //Interface 2 Drinkable, Potable { } public interface Potable { Explanation: The first concrete class (Juice) to extend the public int getNumGlasses(int num); abstract class (Water) must implement all the inherited } interface methods within its definition. 4. A class cannot implement interfaces that contain methods 3. A class cannot extend an interface. with the same signature but with different return types. public interface Drinkable { } //Interface 1 public class Water extends Drinkable { } public interface Drinkable { //does not compile public int getNumGlasses(); 4. An interface cannot extend a class. } public class Water { } //Interface 2 public interface Drinkable extends Water { } public interface Potable { //does not compile public void getNumGlasses(); } Rules in Implementing an Interface Reference: Oracle Docs (n.d.). Citing sources. Retrieved from 1. An interface cannot implement another interface. https://docs.oracle.com/javase/tutorial/java/javaOO/index.html 08 Handout 1 *Property of STI  [email protected] Page 2 of 2

Use Quizgecko on...
Browser
Browser