1 / 16

11 Implementing Abstract Classes

11 Implementing Abstract Classes. 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. Objectives.

elkan
Download Presentation

11 Implementing Abstract Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 11 Implementing Abstract Classes

  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. 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. 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. 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. Abstract Class (Example) publicabstract 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()); } } 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(); } } Account Number: 001 Account Name: Juan dela Cruz Account Balance: 500.0

  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. Implementing Interface • Implementing an interface means providing implementations for its methods • Interfaces are implemented using the implementskeyword • Rules on implementing the interface methods • Must have the same method signature and return type • Cannot narrow the method accessibility • Cannot specify broader checked exceptions • Interface variables are implicitly public final static • Interface methods are implicitly public abstract

  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. Interface (Example) 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()); } } publicabstract 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

  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 } } 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 Uses of InterfaceMultiple Inheritance

  12. 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; } } Uses of InterfaceDefining Constants

  13. 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

  14. 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

  15. 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 ;-)

  16. 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.

More Related