Inheritance in Java

 program to inherit one class from the base class

package com.company;
class inheritance {

public void one(){
System.out.println("class 1");
}
}
class derived extends inheritance
{
public void fum(){
System.out.println("class2");
}
}
class inhet{
public static void main(String[] args) {
inheritance h = new inheritance();
h.one();
derived d = new derived();
d.fum();
d.one();
}
}

constructor in Inheritance

package com.company;
class base{
public void fun(){
System.out.println("class 1");
}
base(){
System.out.println(" I'm the constructor of class 1");
}
}
class derive extends base{
public void fun1(){
System.out.println("class 2");
}
derive(){
System.out.println("I'm the constructor of class 2");
}
}
public class constructorINinheritance {
public static void main(String[] args) {
base b= new base();
derive d = new derive();
// the constructor will automatically call out

//d.fun1();
}
}

OUTPUT

"C:\Program Files\Java\jdk-16.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.1\lib\idea_rt.jar=53200:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\Vishal Rana\IdeaProjects\Programes\out\production\Programes" com.company.constructorINinheritance

 I'm the constructor of class 1     automatic call of constructor from class base

 I'm the constructor of class 1    automatic call of constructor from class derive which inherits class base

I'm the constructor of class 2        automatic call of constructor from class drive

Process finished with exit code 0

 program with  super keyword in Inheritance calling constructor with argument

package com.company;
class base{
public void fun(){
System.out.println("class 1");
}
base(){
System.out.println(" I'm the constructor of class 1");
}
base(int q){
System.out.println(" I'm the constructor with argument of class 1 and q ="+ q);
}
}
class derive extends base{
public void fun1(){
System.out.println("class 2");
}
derive(){
super(3); // super keyword
System.out.println("I'm the constructor of class 2");
}
}
public class constructorINinheritance {
public static void main(String[] args) {
base b= new base();
derive d = new derive();

// the constructor will automatically call out

//d.fun1();
}
}

OUTPUT

"C:\Program Files\Java\jdk-16.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.1\lib\idea_rt.jar=53200:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.2.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\Vishal Rana\IdeaProjects\Programes\out\production\Programes" com.company.constructorINinheritance

 I'm the constructor of class 1     automatic call of constructor from class base

 I'm the constructor with argument of class 1 and q =3   automatic call of constructor from class derive which inherits class base

I'm the constructor of class 2        automatic call of constructor from class drive

Process finished with exit code 0

Program with this keyword in Inheritance 

package com.company;
class tis{
int a; //this variable is denoted by the "this.a"
tis(int a){ //the 2nd same variable is used inside of the constructor
this .a = a; //this keyword is used to call variable with same name into a single class
System.out.println("the value of the a ="+a);
}
} //this keyword is used as reference of the same variable
public class THISKeyword {
public static void main(String[] args) {
tis t = new tis(3);
}
}
program to illustrate the concept of Method overriding in Inheritance

package com.company;
class pug{
public void fun(){
System.out.println("hy, i'm from class 1");
}
public void cls(){
System.out.println("hy 2");
}
}
class hui extends pug{
@Override
public void cls(){ //here this function is overwritten in this program this is called method overriding
System.out.println("hy 2 but in class 2");
}
}
public class MethodOVERRIDING {
public static void main(String[] args) {
pug p= new pug(); //here calling the same function with different classes
p.cls(); //this will call its own function
hui h= new hui();
h.cls(); //since it has its own same function so it will it's function yet it can inherit class pug
h.fun();
}
}
program to understand the concept of Dynamic method dispatch

package com.company;
class One{
public void gj(){
System.out.println("enter 1");
}
public void ih(){
System.out.println("enter 2");
}
}
class Two extends One{
public void hyr(){
System.out.println("enter 3 in class 2");
}
public void ih(){
System.out.println("enter 2 but in class 2");
}
}
public class dynamicMethodDispatch {
public static void main(String[] args) {
One a= new Two(); // since one is the parent class so we can make the child class reference from parent class
// "Two t = new One(); " but this thing is not allowed because two is child class
a.gj();
a.ih(); // here are two same objects so it will call the child class object by methodoverriding
// a.hyr(); //by using this refernece you can only call parent class objects this is not allowed
Two t=new Two();
t.hyr();
}
}
create a number guessing game 
package com.company;
import java.util.Scanner;
import java.util.Random;
class game {
int chose;
int guess;
game() {
Random r = new Random();
guess = r.nextInt(100);
System.out.println("enter your guess number");
}
boolean guess() {

if (chose < guess) {
System.out.println("your guess is low \n wrong");
return false;
} else if (chose > guess) {
System.out.println("your guess is high\n wrong");
return false;
}
else{
System.out.println("congratulation your guess is right");
}
return true;
}
public void input() {
Scanner s = new Scanner(System.in);
chose = s.nextInt();
}
}
public class guessingGAME {
public static void main(String[] args) {
game game = new game();
boolean b= false;
int d;
d=0;
while(!b) {
game.input();
b = game.guess();
d=d+1;
}
System.out.printf("you take %d attempts of win",d);

}
}









 

No comments:

Post a Comment

    Hello, I'm Vishal Rana  I'm 19 years old  I live in Himachal Pradesh and  currently I'm studying in college for my UG degree...