Simple coding example 1 for Bank system:
A Bank Account has common features like accountNumber, balance, deposit(), withdraw().
Different types of accounts such as Savings Account and Current Account have additional features.
Parent class:BankAccount
child class1:SavingAccount
child class2:CurrentAccount
package bank.task;
public class BankAccount {
int accountNumber;
double balance;
public void deposit(double depositAmount, int accNo,String AccType) {
double total= balance+depositAmount;
System.out.println("An amount of Rs."+ depositAmount+" has been deposited succesfully ");
System.out.println("The total amount in your "+AccType+" Account of Account number"+accNo+ " is "+total );
}
protected void withdraw(double withdrawAmount, int accNo,String AccType) {
double total= balance-withdrawAmount;
System.out.println("An amount of Rs."+ withdrawAmount+" has been withdrawn succesfully ");
System.out.println("The total amount in your "+AccType+" Account of your Account number"+accNo+ "is"+total );
}
}
class SavingAccount extends BankAccount{
double interestRate;
public void displayInterest(double rate) {
System.out.println("The interest rate is"+rate );
}
}
class CurrentAccount extends BankAccount{
int limit=2000;
public void payPenalty(float balance){
if(balance<limit)
System.out.println("Your balance is less than Rs. "+limit+" . Please pay a penalty of Rs.500");
else
System.out.println("Your balance is above than Rs. "+limit+" .NO penalty");
}
}
class User{
public static void main(String args[])
{
SavingAccount sa= new SavingAccount();
CurrentAccount ca=new CurrentAccount();
sa.balance=10000.00;
sa.deposit( 2000.00, 1234567, "Savings");
sa.withdraw(2345.00, 1234567, "Savings");
sa.displayInterest(6.7F);
ca.balance=1900;
ca.payPenalty((float)ca.balance);
}
}
Simple Coding Example 2
Parent class:Vehicles
sub classes: Car,Bike,Truck
package vehicle.task;
public class Vehicle {
int speed;
String fuelType;
protected void start(String vName) {
if((vName=="Bike")||( vName=="Car"))
{
System.out.println("Your "+vName+" has Button start feature");
}
else
{
System.out.println("Your "+vName+" has physical Key start feature");
}
}
public void stop() {
System.out.println("Make sure,you collect your keys from the vehicle before you leave");
}
}
Subclass Bike
package vehicle.task;
public class Bike extends Vehicle {
String model;
boolean ABS;
void ABS(boolean b) {
if(b)
System.out.println("Your bike "+model+" has Anti lock Braking system");
else
System.out.println("Your bike does not have Anti lock Braking system");
}
subclass Car
package vehicle.task;
public class Car extends Vehicle {
String model;
int vehicleNumber;
protected void fastenSeatBelt() {
System.out.println("Please fasten your seat belt for safety");
}
}
Subclass Truck
package vehicle.task;
public class Truck extends Vehicle {
int capacity;
void load(int c) {
System.out.println("The capacity of your truck is"+c);
}
}
Main Driving class
package vehicle.task;
public class MainUser {
public static void main(String[] args) {
Bike bk = new Bike();
bk.start("Bike");
bk.model = "pulsar";
bk.ABS(true);
Car c = new Car();
c.start("Car");
c.fastenSeatBelt();
Truck truck = new Truck();
truck.start("Truck");
truck.capacity = 6700;
truck.load(truck.capacity);
}
}
Output
Your Bike has Button start feature
Your bike pulsar has Anti lock Braking system
Your Car has Button start feature
Please fasten your seat belt for safety
Your Truck has physical Key start feature
The capacity of your truck is 6700
Example coding 3
Grandparent class:Employee
Parent:Manager
child of Manager class:Developer
Child of Manager class: Tester
package Employee;
public class Employee {
int id;
String name;
float salary;
protected void creditSalary(float salary, String name, String role) {
System.out.println("Hello " + name + " (" + role + ")Your salary " + salary + " credited");
}
}
class Manager extends Employee {
int ipaddress;
void rating(int rate) {
System.out.println("Your rating is " + rate);
}
void approveLeave(int days) {
if (days < 3)
System.out.println("Your leave is approved");
else
System.out.println("Leave can't be approved .please work from home");
}
}
class Developer extends Manager {
String ipaddress;
void gitubpush() {
System.out.println("Code has been pushed successfully from the system ip address " + ipaddress);
}
}
class Tester extends Manager {
String ipaddress;
void writeTestcases() {
System.out.println("Testcases written in the system " + ipaddress);
}
}
class Main {
public static void main(String args[]) {
Manager m = new Manager();
m.creditSalary(50000, "Gora", "Manager");
Developer dev = new Developer();
dev.name = "John";
dev.ipaddress = "192.168.0.70";
dev.gitubpush();
dev.creditSalary(20000, dev.name, "Developer");
dev.rating(3);
Tester t = new Tester();
t.ipaddress = "162.168.0.70";
t.writeTestcases();
t.creditSalary(15000, "Vyas", "Tester");
t.approveLeave(7);
}
}
Top comments (0)