You are on page 1of 3

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


1.MasterCard Credit Card
2.Visa Credit Card
3.Maestro 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


1.MasterCard Credit Card
2.Visa Credit Card
3.Maestro 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){
this.cardNumber = cardNumber;
this.cardType = cardType;
this.holderName = holderName;
}
void setCardNumber(String cardNumber){
this.cardNumber = cardNumber;
}
void setCardType(String cardType){
this.cardType = cardType;
}
void setHolderName(String holderName){
this.holderName = holderName;
}
String getCardNumber(){
return this.cardNumber;
}
String getCardType(){
return this.cardType;
}
String getHolderName(){
return this.holderName;
}
void issuingAuthority(){
System.out.println("You have an Authority of : "+this.getCardType());
}
}

//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