Implementing Abstract Classes

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Inheritance using Java
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.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
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,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
11 Implementing Abstract Classes. 2 Contents Define Abstract Class Rules on Abstract Class Define Interface Implementing Interface Uses of Interface Abstract.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
 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.
BY:- TOPS Technologies
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.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Abstract classes and interfaces
Web Design & Development Lecture 9
Sixth Lecture ArrayList Abstract Class and Interface
Objects as a programming concept
University of Central Florida COP 3330 Object Oriented Programming
Final and Abstract Classes
Inheritance and Polymorphism
EKT 472: Object Oriented Programming
Interface.
03/10/14 Inheritance-2.
Phil Tayco Slide version 1.1 Created Oct 30, 2017
Programming Language Concepts (CIS 635)
UML Class Diagram: class Rectangle
ATS Application Programming: Java Programming
ATS Application Programming: Java Programming
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Chapter 13 Abstract Classes and Interfaces
More inheritance, Abstract Classes and Interfaces
Abstract classes and interfaces
Interface.
Java Programming Language
Polymorphism and access control
Week 6 Object-Oriented Programming (2): Polymorphism
Interfaces.
Advanced Java Programming
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
CMSC 202 Interfaces.
Fundaments of Game Design
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Abstract classes and interfaces
Abstract Classes and Interfaces
Chapter 8 Inheritance Part 2.
Chapter 13 Abstract Classes and Interfaces Part 01
Final and Abstract Classes
Abstract Classes and Interfaces
Topics OOP Review Inheritance Review Abstract Classes
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

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 2005 All Rights Reserved Course Code #16325

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 2005 All Rights Reserved Course Code #16325

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 2005 All Rights Reserved Course Code #16325

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 2005 All Rights Reserved Course Code #16325

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 2005 All Rights Reserved Course Code #16325

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 2005 All Rights Reserved Course Code #16325

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 2005 All Rights Reserved Course Code #16325

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 2005 All Rights Reserved Course Code #16325

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 2005 All Rights Reserved Course Code #16325

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 2005 All Rights Reserved Course Code #16325

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 2005 All Rights Reserved Course Code #16325

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 2005 All Rights Reserved Course Code #16325

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 2005 All Rights Reserved Course Code #16325

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 2005 All Rights Reserved Course Code #16325

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 2005 All Rights Reserved Course Code #16325