From 676dc381c04b905b8847e2bb6d8b771666b37406 Mon Sep 17 00:00:00 2001 From: Isha Tripathi <96119599+IT-0824@users.noreply.github.com> Date: Sat, 29 Oct 2022 18:34:45 +0530 Subject: [PATCH 1/2] Inheritance using Java --- Inheritance using Java | 73 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Inheritance using Java diff --git a/Inheritance using Java b/Inheritance using Java new file mode 100644 index 0000000..160f688 --- /dev/null +++ b/Inheritance using Java @@ -0,0 +1,73 @@ +// Java program to implement the concept of inheritance + +// base class +class Bicycle { + // the Bicycle class has two fields + public int gear; + public int speed; + + // the Bicycle class has one constructor + public Bicycle(int gear, int speed) + { + this.gear = gear; + this.speed = speed; + } + + // the Bicycle class has three methods + public void applyBrake(int decrement) + { + speed -= decrement; + } + + public void speedUp(int increment) + { + speed += increment; + } + + // toString() method to print info of Bicycle + public String toString() + { + return ("No of gears are " + gear + "\n" + + "speed of bicycle is " + speed); + } +} + +// derived class +class MountainBike extends Bicycle { + + // the MountainBike subclass adds one more field + public int seatHeight; + + // the MountainBike subclass has one constructor + public MountainBike(int gear, int speed, + int startHeight) + { + // invoking base-class(Bicycle) constructor + super(gear, speed); + seatHeight = startHeight; + } + + // the MountainBike subclass adds one more method + public void setHeight(int newValue) + { + seatHeight = newValue; + } + + // overriding toString() method + // of Bicycle to print more info + @Override public String toString() + { + return (super.toString() + "\nseat height is " + + seatHeight); + } +} + +// driver class +public class Test { + public static void main(String args[]) + { + + MountainBike mb = new MountainBike(3, 100, 25); + System.out.println(mb.toString()); + } +} From d3a078e82d29adcf6d24b10cc27bade1f5a86f27 Mon Sep 17 00:00:00 2001 From: Isha Tripathi <96119599+IT-0824@users.noreply.github.com> Date: Sat, 29 Oct 2022 18:37:38 +0530 Subject: [PATCH 2/2] Multithreading code using Java --- Multithreading code | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Multithreading code diff --git a/Multithreading code b/Multithreading code new file mode 100644 index 0000000..abd53cd --- /dev/null +++ b/Multithreading code @@ -0,0 +1,30 @@ +// Java code for thread creation by extending +// the Thread class +class MultithreadingDemo extends Thread { + public void run() + { + try { + // Displaying the thread that is running + System.out.println( + "Thread " + Thread.currentThread().getId() + + " is running"); + } + catch (Exception e) { + // Throwing an exception + System.out.println("Exception is caught"); + } + } +} + +// Main Class +public class Multithread { + public static void main(String[] args) + { + int n = 8; // Number of threads + for (int i = 0; i < n; i++) { + MultithreadingDemo object + = new MultithreadingDemo(); + object.start(); + } + } +}