Tuesday, July 20, 2021

simple program using concept of inheritance (Java) (208)

package com.company;
import java.util.Scanner;
class parent
{
protected int a;
public void geta(int x)
{
a=x;
}
}
class child extends parent
{
private int b;
public void getb(int y)
{
b=y;
}
public int sum()
{
return a+b;
}
}
public class Program45 {
public static void main(String[] args) {
Scanner p = new Scanner(System.in);
System.out.println("please enter a first number:");
int a=p.nextInt();
System.out.println("please enter a second number:");
int b=p.nextInt();
child obj=new child();
obj.geta(a);
obj.getb(b);
System.out.print("sum is : ");
System.out.println(obj.sum());
}
}

No comments:

Post a Comment