0% found this document useful (0 votes)
12 views3 pages

Implementing Abstract Classes in Java

An abstract class can have both abstract and implemented methods. The document discusses an abstract CreditCard class with attributes like card number and holder name. It has an implemented issuingAuthority() method that returns the card type. Classes like Maestro and Mastercard extend CreditCard without overriding issuingAuthority(). The main method creates instances of these subclasses and calls issuingAuthority() to output the card type.

Uploaded by

Soul Knight
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Implementing Abstract Classes in Java

An abstract class can have both abstract and implemented methods. The document discusses an abstract CreditCard class with attributes like card number and holder name. It has an implemented issuingAuthority() method that returns the card type. Classes like Maestro and Mastercard extend CreditCard without overriding issuingAuthority(). The main method creates instances of these subclasses and calls issuingAuthority() to output the card type.

Uploaded by

Soul Knight
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

All Methods Abstract in an Abstract Class?

 
OK, A Strange & Common Question? If I mark a method as abstract, I mark the class also as abstract. But
if needed, Can I implement a few methods for which I know the implementation or is it that I have to mark
all methods as abstract in an abstract class.

Not at All. An Abstract class can very well have methods which are implemented and not abstract. Let's try
it out. 

Create a abstract class CreditCard with 3 private attributes

cardNumber of type String,


cardType of type String
holderName of type String

Include a method issuingAuthority()  in the CreditCard class and implement which returns the name of


the holder along with the Card Type.
 
Create the class Maestro which extends the class CreditCard .

Create the class Mastercard which extends the class CreditCard.

Create the class Visa which extends the class CreditCard.

Use Appropriate Getters Setters for the above classes.

Create a driver class named Main which creates an instance of the above mentioned classes. The
function issuingAuthority() just displays the customer's credit card type.

Input and Output format:


Refer to sample Input and Output for formatting specifications.

Note: All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output 1:

Enter the Credit Card Type


[Link] Credit Card
[Link] Credit Card
[Link] Credit Card
1
Enter the Holder Name
Vinay Kumar
Enter the Card Number
56894123
Holder Name : Vinay Kumar
Card Number : 56894123
You have an Authority of : MasterCard Credit Card
Sample Input and Output 2:

Enter the Credit Card Type


[Link] Credit Card
[Link] Credit Card
[Link] Credit Card
5
Invalid Account type

//CreditCard
abstract class CreditCard{
private String cardNumber;
private String cardType;
private String holderName;
CreditCard(){}
CreditCard(String cardNumber, String cardType, String holderName){
[Link] = cardNumber;
[Link] = cardType;
[Link] = holderName;
}
void setCardNumber(String cardNumber){
[Link] = cardNumber;
}
void setCardType(String cardType){
[Link] = cardType;
}
void setHolderName(String holderName){
[Link] = holderName;
}
String getCardNumber(){
return [Link];
}
String getCardType(){
return [Link];
}
String getHolderName(){
return [Link];
}
void issuingAuthority(){
[Link]("You have an Authority of : "+[Link]());
}
}

//Maestro

class Maestro extends CreditCard {


Maestro() {}
Maestro(String cardNumber, String cardType, String holderName) {
Super(cardNumber, cardType, holderName);
}
}

//MasterCard

class Mastercard extends CreditCard{


Mastercard(){}
Mastercard(String cardNumber, String cardType, String holderName){
super(cardNumber, cardType, holderName);
}
}

//Visa

class Visa extends CreditCard{


Visa(){}
Visa(String cardNumber, String cardType, String holderName){
super(cardNumber, cardType, holderName);
}
}

//Main

class Main {
}

You might also like