11 Implementing Abstract Classes. 2 Contents Define Abstract Class Rules on Abstract Class Define Interface Implementing Interface Uses of Interface Abstract.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Abstract Classes and Interfaces The objectives of this chapter are: To explore the concept of abstract classes To understand interfaces To understand the.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CC1007NI: Further Programming Week Dhruba Sen Module Leader (Islington College)
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Inheritance using Java
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Programming in Java CSCI-2220 Object Oriented Programming.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Topics Inheritance introduction
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
Classes, Interfaces and Packages
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
BY:- TOPS Technologies
Interfaces CMSC 202. Public Interfaces Objects define their interaction with the outside world through the their public interface. A class' public interface.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Final and Abstract Classes
Inheritance and Polymorphism
Interface.
Agenda Warmup AP Exam Review: Litvin A2
Phil Tayco Slide version 1.1 Created Oct 30, 2017
ATS Application Programming: Java Programming
Implementing Abstract Classes
CMSC 202 Static Methods.
Polymorphism and access control
Interfaces.
Java Inheritance.
CMSC 202 Interfaces.
Chapter 9 Carrano Chapter 10 Small Java
Final and Abstract Classes
Presentation transcript:

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) 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: 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 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 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 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 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: Updated Balance: 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 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 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 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 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 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 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 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 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.