OOP Programs:
Course: Object-Oriented Programming
Task 01
- Write a class with name "Abc" have Variable i,j.
- Write another class "XYZ" inherit the Class "Abc" and Variable k.
- Write the Default and Parameterized Constructor for both classes.
- Show method in both classes.
- Also Write a method which return the sum off i,j, and k variables.
Task 02
- Write public ,private and protected keywords with show methods and i,j Variables.
- And write your Observation.
File Name:-->Abc.java
class Abc{
public int i,j; // Two variables
//Default Constructor
Abc(){
i=0;
j=0;
System.out.println("Defualt Constructor Abc");
}
//Parametrized Constructor
Abc(int x, int y){
System.out.println("Parameterized Constructor Abc");
i=x;
j=y;
}
// Show
public void show(){
System.out.println("i"+i+"j"+j);
}
}
File Name:-->XYZ.java
//extends Keywords in use to inherit class
class XYZ extends Abc{
private int k; // k is the Third Variable add
// Default Constructor
XYZ(){
k=0;
System.out.println("Default Constructor of XYZ");
}
// Parameterized Constructor
XYZ(int a,int b,int c){
super(a,b);
k=c;
System.out.println("Parameterized Constructor of XYZ");
}
// Show
public void show(){
System.out.println("i =" +i+ " j =" +j+ " k = "+k);
// Add i,j,k Variables
System.out.println("Sum =" +(i+j+k));
}
}
File Name:-->main.java
class main{
public static void main(String args[])
{
/*Creating Object of the XYZ and Pass the Values That assign to i,j,k
to Show Sum */
XYZ hassan= new XYZ(12,12,12);
hassan.show();
System.out.println("\n"); //"\n" is used change the line
// Creating Object XYZ to show Default Values
XYZ ali=new XYZ();
ali.show();
}
}
إرسال تعليق