/
Casting and Abstract classes/methods Casting and Abstract classes/methods

Casting and Abstract classes/methods - PowerPoint Presentation

chipaudi
chipaudi . @chipaudi
Follow
343 views
Uploaded On 2020-06-15

Casting and Abstract classes/methods - PPT Presentation

94 extension Type Casting Type Casting is essentially changing the data type Remember our Dice we generated a random number using Mathrandom which returns a double We wanted to store it in an ID: 777827

abstract class checkingaccount bankaccount class abstract bankaccount checkingaccount methods type transfer account ba2 method random called casting created project

Share:

Link:

Embed:

Download Presentation from below link

Download The PPT/PDF document "Casting and Abstract classes/methods" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Casting and Abstract classes/methods

9.4 extension

Slide2

Type Casting

Type Casting is essentially changing the data type

Remember our Dice:

we generated a random number using Math.random() which returns a double. We wanted to store it in an int type. we CASTED it as an integer to allow it. roll = (int)(Math.random()*6 +1);

Cast

Slide3

The same can be done with object variables

Given,

CheckingAccount

and SavingsAccounts are subclasses of BankAccount, and the method transfer is written in the CheckingAccount class, but not the BankAccount class, consider this code:BankAccount

ba

= new

CheckingAccount

(100);

BankAccount

ba2 = new

SavingsAccount

(200);

ba.transfer

(ba2,50);

Slide4

The same can be done with object variables

The compiler will not allow this to run because it sees no method called

transfer

in the BankAccount class and ba is a BankAccount variable. A solution:BankAccount

ba

= new

CheckingAccount

(100);

BankAccount

ba2 = new SavingsAccount(200);((CheckingAccount)ba).transfer(ba2,50);

We have changed the type to CheckingAccount

Slide5

Consider our bank account project

There are times when we want a class to be created solely for the purpose of being a super class to other classes. We would have no intention of ever instantiating that class. This is called an abstract class.

Ex. In our project, every account created should be either a checking or savings account. There is no reason to instantiate bank account

Slide6

Abstract method

An abstract class can have what are called abstract methods. These methods have no implementation. They simply require any subclass to override the method.

This would solve our issue with transfer in

BankAccountAn abstract class can have no abstract methods, but if a class has ANY abstract methods it MUST be an abstract class