Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implementing Abstract Classes

Similar presentations


Presentation on theme: "Implementing Abstract Classes"— Presentation transcript:

1 Implementing Abstract Classes
ATS Application Programming: Java Programming Implementing Abstract Classes 4.1 Implementing Abstract Classes 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 © Accenture All Rights Reserved Course Code #16325

2 ATS Application Programming: Java Programming
Objectives 4.1 Implementing Abstract Classes 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 © Accenture All Rights Reserved Course Code #16325

3 Defining Abstract Class
ATS Application Programming: Java Programming Defining Abstract Class 4.1 Implementing Abstract Classes 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 It represents an abstract concept for which there is no actual concrete expression. For instance, "mammal" is an abstract class - there is no such real, concrete thing as a generic mammal. Instead, there are only instances of mammal, such as human being and monkey, which are types of mammals, and share common characteristics, such as having warm blood and body hair in at least part of the lifecycle. Take another example, food. Have you ever seen an instance of food? Probably not. What you see instead are instances of carrot, apple, and chocolate chip cookies. Food represents the abstract concept of what we can eat. It doesn't make sense for an instance of food to exist. Similarly, in object-oriented programming, you may want to model an abstract concept without being able to create an instance of it. For example, the Number class represents the abstract concept of numbers. It makes sense to model numbers, but it doesn't make sense to create a generic number object. Instead, the Number class makes sense only as a superclass to such classes as Integer and Float, both of which implement specific kinds of numbers. A class such as Number, which represents an abstract concept and should not be instantiated, is called an abstract class. An abstract class can only be subclassed. © Accenture All Rights Reserved Course Code #16325

4 Rules on Abstract Class
ATS Application Programming: Java Programming Rules on Abstract Class 4.1 Implementing Abstract Classes 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. © Accenture All Rights Reserved Course Code #16325

5 Abstract Class (Example)
ATS Application Programming: Java Programming Abstract Class (Example) 4.1 Implementing Abstract Classes 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()); 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 © Accenture All Rights Reserved Course Code #16325

6 ATS Application Programming: Java Programming
Defining Interface 4.1 Implementing Abstract Classes 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 © Accenture All Rights Reserved Course Code #16325

7 Implementing Interface
ATS Application Programming: Java Programming Implementing Interface 4.1 Implementing Abstract Classes Implementing an interface means providing implementations for its methods Interfaces are implemented using the implements keyword 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 © Accenture All Rights Reserved Course Code #16325

8 ATS Application Programming: Java Programming
Rules on Interface 4.1 Implementing Abstract Classes 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 © Accenture All Rights Reserved Course Code #16325

9 ATS Application Programming: Java Programming
Interface (Example) 4.1 Implementing Abstract Classes 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()); } 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 © Accenture All Rights Reserved Course Code #16325

10 Uses of Interface Multiple Inheritance
ATS Application Programming: Java Programming Uses of Interface Multiple Inheritance 4.1 Implementing Abstract Classes 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 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 © Accenture All Rights Reserved Course Code #16325

11 Uses of Interface Defining Constants
ATS Application Programming: Java Programming Uses of Interface Defining Constants 4.1 Implementing Abstract Classes 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; Notes: These constant values will then be available to any class that implements the interface Fields defined in interface are automatically declared static and final While this is a matter of preference, some designers dislike using Interfaces as a means of defining constants as this is a violation of OOP principles. An interface is meant to be a template for behaviors (methods) not values. Also with the new enum class in Java 1.5, there is a much better way now to define enumerated constants like the example above. © Accenture All Rights Reserved Course Code #16325

12 ATS Application Programming: Java Programming
Defining Final Class 4.1 Implementing Abstract Classes 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 © Accenture All Rights Reserved Course Code #16325

13 ATS Application Programming: Java Programming
Rules on Final Class 4.1 Implementing Abstract Classes 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 © Accenture All Rights Reserved Course Code #16325

14 ATS Application Programming: Java Programming
Final Class (Example) 4.1 Implementing Abstract Classes 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 ;-) © Accenture All Rights Reserved Course Code #16325

15 ATS Application Programming: Java Programming
Key Points 4.1 Implementing Abstract Classes 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. © Accenture All Rights Reserved Course Code #16325


Download ppt "Implementing Abstract Classes"

Similar presentations


Ads by Google