Download presentation
Presentation is loading. Please wait.
Published byOwen Bruce Modified over 9 years ago
1
11 Implementing Abstract Classes
2
2 Contents Define Abstract Class Rules on Abstract Class Define Interface Implementing Interface Uses of Interface Abstract class versus Interface Define Final Class Rules on Final Class Abstract class versus Final class
3
3 Objectives Define an abstract class and its importance Demonstrate how to declare an abstract class Define interface and its importance Demonstrate how to declare an interface Differentiate the use of abstract class and interface Define final class and its importance Demonstrate how to declare a final class Differentiate the use of abstract class and final class
4
4 Defining Abstract Class An Abstract Class is a class that provides common behavior across a set of subclasses, but is not itself designed to have instances of its own A class that dictates certain behavior but allows its subclasses to provide implementation A class designed only as a parent from which sub-classes may be derived, but which is not itself suitable for instantiation A class often used to "abstract out" incomplete sets of features which may then be shared by a group of sibling sub-classes which add different variations of the missing pieces
5
5 Rules on Abstract Class An Abstract Class cannot be instantiated An abstract class can SHOULD be extended An abstract class can have any number of abstract methods or none at all A class with at least one abstract method must be declared an abstract class A subclass can provide partial or full implementations of the inherited abstract methods A class that has at least one abstract method, whether declared or inherited from an abstract class, must be declared abstract.
6
6 Abstract Class (Example) public abstract class AccountInfo { private String acctNo; private String acctName; private double balance; public String getAcctNo() {return acctNo;} public void setAcctNo(String no) {acctNo = no;} public String getAcctName() {return acctName;} public void setAcctName(String name){acctName = name;} public double getBalance() {return balance;} public void setBalance(double bal) {balance = bal;} public abstract void printAccountInfo(); } public class Savings extends AccountInfo { public void printAccountInfo() { System.out.println("Account Number: " + getAcctNo()); System.out.println("Account Name: " + getAcctName()); System.out.println("Account Balance: " + getBalance()); } Account Number: 001 Account Name: Juan dela Cruz Account Balance: 500.0 public class BankApp { public static void main(String[] args) { Savings pesoAcct = new Savings(); pesoAcct.setAcctNo("001"); pesoAcct.setAcctName("Juan dela Cruz"); pesoAcct.setBalance(500); pesoAcct.printAccountInfo(); }
7
7 Defining Interface An Interface defines a contract by specifying a set of method prototypes for which each class that implements it must adhere An interface is 100% abstract class An interface provides only a form for a class but no implementation An interface defines what a class can do but not how the class will do it
8
8 Implementing Interface Implementing an interface means providing implementations for its methods Interfaces are implemented using the implements keyword Rules on implementing the interface methods 1.Must have the same method signature and return type 2.Cannot narrow the method accessibility 3.Cannot specify broader checked exceptions Interface variables are implicitly public final static Interface methods are implicitly public abstract
9
9 Rules on Interface An interface can extend several interfaces Interfaces can be implemented by any class A class can implement several interfaces, thus enabling multiple inheritances A class that implements an interface partially must be declared abstract An interface can be declared as a reference variable An interface cannot implement any data type An interface cannot be instantiated
10
10 Interface (Example) public abstract class AccountInfo { /* * class body here (from Abstract Class example) */ } public interface BankOperations { public void withdraw(double amount); public void deposit(double amount); public double balanceInquiry(); } public class Savings extends AccountInfo implements BankOperations { public void printAccountInfo() { System.out.println("Account Number: " + getAcctNo()); System.out.println("Account Name: " + getAcctName()); System.out.println("Account Balance: " + getBalance()); } public void withdraw(double Amount) { setBalance(getBalance()-Amount); } public void deposit(double Amount) { setBalance(getBalance()+Amount); } public double balanceInquiry() { return getBalance(); } Account Number: 001 Account Name: Juan dela Cruz Account Balance: 500.0 Updated Balance: 750.0 public class BankApp { public static void main(String[] args) { Savings pesoAcct = new Savings(); pesoAcct.setAcctNo("001"); pesoAcct.setAcctName("Juan dela Cruz"); pesoAcct.setBalance(500); pesoAcct.printAccountInfo(); pesoAcct.deposit(300); pesoAcct.withdraw(50); System.out.println("Updated Balance: " + pesoAcct.balanceInquiry()); }
11
11 interface CanFight {void fight();} interface CanSwim {void swim();} interface CanJump {void jump();} class ActionStar implements CanFight { public void fight() { //fight here } class Hero extends ActionStar implements CanSwim, CanJump { public void swim() { //swim here } public void jump() { //jump here } Uses of Interface Multiple Inheritance A core reason for using interfaces is to be able to upcast to more than one base type Several interfaces can be implemented by any class, thus resulting in multiple inheritance
12
12 Uses of Interface Defining Constants An interface is a convenient place for defining constant values interface DateConstants { int JANUARY = 1; int FEBRUARY = 2; int MARCH = 3; int APRIL = 4; int MAY = 5; int JUNE = 6; int JULY = 7; int AUGUST = 8; int SEPTEMBER = 9; int OCTOBER = 10; int NOVEMBER = 11; int DECEMBER = 12; } class BirthDay implements DateConstants { boolean isMyBirthday(int mm, int dd, int yy) { if (mm == APRIL & dd==20 & yy==1980) return true; return false; }
13
13 Abstract Class vs Interface Considerations in using either an interface or abstract class An interface is useful because any class can implement it. But an interface, compared to an abstract class, is a pure API specification and contains no implementation. If another method is added in an interface, all classes that implemented that interface will be broken A good implementation of both is to create an interface and let the abstract class implement it. So when there is a need for adding methods, it can be safely added to the abstract class itself rather than the interface. Use interfaces when a certain method needs to be forcibly overriden/enforced by a class.
14
14 Defining Final Class A Final Class is considered complete, it cannot be improved or specialized A final class ensures that its state and behavior cannot be changed for safety and security To re-use a final class, you must utilize composition instead of inheritance
15
15 Rules on Final Class A final class cannot be extended or subclassed All methods of a final class have implementations All methods of a final class are implicitly final Members of the final class cannot be inherited or hidden Methods of the final class cannot be overridden
16
16 Final Class (Example) final class Formula { static double speed(double distance, double time) {return distance/time;} static double acceleration(double s2, double s1, double t) {return (s2-s1)/t;} static double force(double mass, double acc) {return mass * acc;} static double pressure(double force, double area) {return force / area;} static double work(double force, double distance) {return force * distance;} } public static void main(String[] args) { double d1 = 50, t1 = 10; double d2 = 80, t2 = 10; double mass = 50; double s1 = Formula.speed(d1, t1); double s2 = Formula.speed(d2, t2); double acc = Formula.acceleration(s2, s1, 60); double force = Formula.force(mass, acc); double pressure = Formula.work(force,10); System.out.println("If I weigh " + mass + " kg, and"); System.out.println("\t my initial speed is " + s1 + " m/s, and"); System.out.println("\t my final speed is " + s2 + " m/s."); System.out.println("Then, my acceleration in 1 minute is " + acc + " m/s2, and"); System.out.println("\t I'm exerting a force of " + force + " newton, and"); System.out.println("\t a pressure of " + pressure + " joules for 10 meters!"); System.out.println("ehem ;-)"); } If I weigh 50.0 kg, and my initial speed is 5.0 m/s, and my final speed is 8.0 m/s. Then, my acceleration in 1 minute is 0.05 m/s2, and I'm exerting a force of 2.5 newton, and a pressure of 25.0 joules for 10 meters! ehem ;-)
17
17 Abstract Class vs Final Class Abstract class provides a template of behaviors for subclasses to follow without actually dictating to the subclasses the exact implementation of those behaviors. Final class, since it cannot be extended, encourages composition, while abstract class encourages inheritance. Inheritance, through abstract classes, introduces tight coupling between classes and can weaken encapsulation by deferring actual implementation to subclasses. Final classes are used to prevent malicious code from altering the semantics of classes essential to a framework.
18
18 Key Points An Abstract Class is a template with at least partial implementation that other subclasses are supposed to follow. They are not by themselves suitable for instantiation. An Interface defines a contract by specifying a set of abstract method prototypes without actually specifying the implementation. Interfaces are implemented using the implements keyword. Interfaces are used to implement multiple inheritances. A Final Class is considered complete; it cannot be extended.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.