File tree Expand file tree Collapse file tree 4 files changed +150
-0
lines changed
src/main/java/com/thealgorithms/oops Expand file tree Collapse file tree 4 files changed +150
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .thealgorithms .oopconcepts ;
2+
3+ abstract class Vehicle {
4+ abstract void start ();
5+ abstract void stop ();
6+ }
7+
8+ class Car extends Vehicle {
9+ @ Override
10+ void start () {
11+ System .out .println ("Car engine started" );
12+ }
13+
14+ @ Override
15+ void stop () {
16+ System .out .println ("Car stopped" );
17+ }
18+ }
19+
20+ public class AbstractionDemo {
21+ public static void main (String [] args ) {
22+ Vehicle v = new Car ();
23+ v .start ();
24+ v .stop ();
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package com .thealgorithms .oopconcepts ;
2+
3+ class Account {
4+ private String holder ;
5+ private double balance ;
6+
7+ public String getHolder () {
8+ return holder ;
9+ }
10+
11+ public void setHolder (String holder ) {
12+ this .holder = holder ;
13+ }
14+
15+ public double getBalance () {
16+ return balance ;
17+ }
18+
19+ public void deposit (double amount ) {
20+ if (amount > 0 ) balance += amount ;
21+ }
22+
23+ public void withdraw (double amount ) {
24+ if (amount > 0 && amount <= balance ) balance -= amount ;
25+ }
26+ }
27+
28+ public class EncapsulationTest {
29+ public static void main (String [] args ) {
30+ Account acc = new Account ();
31+ acc .setHolder ("Suryanshu" );
32+ acc .deposit (50000 );
33+ acc .withdraw (12000 );
34+ System .out .println (acc .getHolder () + " has balance ₹" + acc .getBalance ());
35+ }
36+ }
Original file line number Diff line number Diff line change 1+ package com .thealgorithms .oopconcepts ;
2+ class Vehicle {
3+ int tyres ;
4+ String colour ;
5+ String companyName ;
6+
7+ void setTyres (int tyres ) {
8+ this .tyres = tyres ;
9+ System .out .println ("Number of tyres: " + tyres );
10+ }
11+
12+ void setColour (String colour ) {
13+ this .colour = colour ;
14+ System .out .println ("Colour of vehicle: " + colour );
15+ }
16+
17+ void setCompanyName (String companyName ) {
18+ this .companyName = companyName ;
19+ System .out .println ("Company name: " + companyName );
20+ }
21+ }
22+
23+ class Truck extends Vehicle {
24+ int wheels = 8 , headlights = 2 ;
25+ String model ;
26+
27+ Truck (String model ) {
28+ this .model = model ;
29+ System .out .println ("This is a truck: " + model );
30+ }
31+ }
32+
33+ class Motorcycle extends Vehicle {
34+ int wheels = 2 , headlights = 1 ;
35+ String model ;
36+
37+ Motorcycle (String model ) {
38+ this .model = model ;
39+ System .out .println ("This is a motorcycle: " + model );
40+ }
41+ }
42+
43+ public class InheritanceDemo {
44+ public static void main (String [] args ) {
45+ Motorcycle mc = new Motorcycle ("Dream" );
46+ mc .setColour ("Black" );
47+ mc .setTyres (2 );
48+ mc .setCompanyName ("Honda" );
49+
50+ Truck tk = new Truck ("Tata Ultra" );
51+ tk .setTyres (8 );
52+ tk .setColour ("White" );
53+ tk .setCompanyName ("Tata" );
54+ }
55+ }
Original file line number Diff line number Diff line change 1+ package com .thealgorithms .oopconcepts ;
2+
3+ class Payment {
4+ void pay () {
5+ System .out .println ("Processing generic payment" );
6+ }
7+ }
8+
9+ class CreditCardPayment extends Payment {
10+ @ Override
11+ void pay () {
12+ System .out .println ("Payment done via Credit Card" );
13+ }
14+ }
15+
16+ class UpiPayment extends Payment {
17+ @ Override
18+ void pay () {
19+ System .out .println ("Payment done via UPI" );
20+ }
21+ }
22+
23+ public class PolymorphismExample {
24+ public static void main (String [] args ) {
25+ Payment p1 = new CreditCardPayment ();
26+ Payment p2 = new UpiPayment ();
27+ Payment p3 = new Payment ();
28+
29+ p1 .pay ();
30+ p2 .pay ();
31+ p3 .pay ();
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments