import java.util.*;
import java.lang.*;
import java.io.*;
/* inheritance is used to borrow properties
for ex:- phone ----> smart phone jese phone se smart phone
bnya humne phone me hi kuchh modification krke usko hi smartphone me convert kr diya
super class ------> sub class */
class Base {
int a,b;
void setA(int a){
this.a=a;
}
int getA(){
System.out.println("i am in base class now");
return a;
}
}/* this is our base class if we need same code in other class
so in simple we copy the code butwhen wehave to change the code
we have to change in all claass so there we use inheritance*/
// making new class
// we use extends base it mean base class ka sara code isme aajayega
// agr hum base class me kucchh change krenge toh isme v ho jayga
class Derived extends Base {
int y;
void setY(int y){
this.y=y;
}
int getY(){
System.out.println("i am in derived class now");
return y;
}}
// yeh toh ho gya hum is base class or kucch extra v add kr skte h
// for ex new getter setter add kr do yaa kucch v
class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
// object for base class
Base obj1 = new Base();
obj1.setA(123);
System.out.println(obj1.getA());
// object for derived class
Derived obj2 = new Derived();
obj2.setA(15);
System.out.println(obj2.getA());
// humne y add kiya ab usko access krte h
obj2.setY(66);
System.out.println(obj2.getY());
}
}
To embed this program on your website, copy the following code and paste it into your website's HTML: