Skip to content

Instantly share code, notes, and snippets.

@ucguy4u
Created December 8, 2020 05:16
Show Gist options
  • Save ucguy4u/59d9401e0e5bf707bd18fbc7a036e60a to your computer and use it in GitHub Desktop.
Save ucguy4u/59d9401e0e5bf707bd18fbc7a036e60a to your computer and use it in GitHub Desktop.
Inheritance example using getter and setters
class Bank {
private String bankName;
public Bank(String bankName) { // constructor
this.bankName = bankName;
}
public String getBankName() { // getter
return this.bankName;
}
}
public class Employee extends Bank{
public Employee(String bankName) {
super(bankName);
}
public static void main(String[] args){
Bank bank = new Bank("Axis");
System.out.println("Riya works for : " +
bank.getBankName());
// we are not accessing the private member directly we are instead using public getter setter to access the private member.
}
}
/**
* OUTPUT:
* Riya works for : Axis
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment