Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

A

Practical File
On
Object Oriented Programming Using Java

(Session:2020-2022)

Submitted to: Submitted by:


Miss Himanshi Dhayal Name- Ashutosh
Assistant Professor Class- Msc (c.s) 2ndsem.
Roll No-200000613014

Department of Information and Communication Technology,


Ch.Bansilal University, Bhiwani, Haryana

1|Page
INDEX

S. NO. PROGRAMS PAGE NO. REMARKS


1. A simple program in class and 3
object.
2. A program for simple inheritance. 4-5
3. A program for multilevel 6-8
Inheritance.
4. A program for multiple 9-10
Inheritance.

5. A program for polymorphism with 11-12


overloading.
6. A program for polymorphism with 13
overriding.
7. A program to use interface. 14

8. A program to use Abstract class. 15

9. A program for multiple 16-17


Inheritance using interface.
10. A program for multidimensional 18
array in java.

2|Page
Program 1

A Simple Program in Class & Object.

public class Circle


{
public double x=5,y=5;
public double radius=1;
public static void main(String args[])
{
Circle c=new Circle();
System.out.println ("center is at” +c.x +"x and "+c.y+"y");
System.out.println("radius of circle is” + c.radius);
}
}

Output:-

3|Page
Program 2

A Program for Simple Inheritance.

class Teacher {
String designation = "Teacher";
String collegeName = "Beginnersbook";
void does(){
System.out.println("Teaching");
}
}
class PhysicsTeacher extends Teacher{
String mainSubject = "Physics";
}
public class Main {
public static void main(String args[]) {
PhysicsTeacher obj = new PhysicsTeacher();
System.out.println(obj.collegeName);
System.out.println(obj.designation);
System.out.println(obj.mainSubject);
obj.does();
}
}

4|Page
Output:-

5|Page
Program 3

A Program of Multilevel Inheritance.

class Car{
public Car()
{
System.out.println("Class Car");
}
public void vehicleType()
{
System.out.println("Vehicle Type: Car");
}
}
class BMW extends Car{
public BMW()
{
System.out.println("Class BMW");
}
public void brand()
{
System.out.println("Brand: BMW");
}
public void speed()
{
System.out.println("Max: 220Kmph");
}
}
class BMW_i8 extends BMW{

6|Page
public BMW_i8()
{
System.out.println("BMW Model: i8");
}
public void speed()
{
System.out.println("Max: 250Kmph");
}
}

public class Main {


public static void main(String args[]) {
BMW_i8 obj=new BMW_i8();
obj.vehicleType();
obj.brand();
obj.speed();
}
}

7|Page
Output:-

8|Page
Program 4

A Program for Multiple Inheritance.

interface X
{
public void myMethod();
}
interface Y
{
public void myMethod();
}
class JavaExample implements X, Y
{
public void myMethod()
{
System.out.println("Implementing more than one interfaces");
}

}
public class Main {
public static void main(String args[]) {
JavaExample obj = new JavaExample();
obj.myMethod();
}}

9|Page
Output:-

10 | P a g e
Program 5

A Program of Polymorphism with Overloading.

class Overload
{
void demo (int a)
{
System.out.println ("a: " + a);
}
void demo (int a, int b)
{
System.out.println ("a and b: " + a + "," + b);
}
double demo(double a) {
System.out.println("double a: " + a);
return a*a;
}
}

public class Main {


public static void main(String args[]) {
Overload Obj = new Overload();
double result;
Obj .demo(10);
Obj .demo(10, 20);
result = Obj .demo(5.5);
System.out.println("O/P : " + result);
}}
11 | P a g e
Output:-

12 | P a g e
Program 6

Program of Polymorphism with Overriding.

class Animal{
public void sound(){
System.out.println("Animal is making a sound");
}
}
class Dog extends Animal{
@Override
public void sound(){
System.out.println("Bow Bow");
}
}

public class Main {


public static void main(String args[]) {
Animal obj = new Dog();
obj.sound();
}
}

Output:-

13 | P a g e
Program 7

A Program to Use Interface.

interface Polygon {
void getArea(int length, int breadth);
}
class Rectangle implements Polygon {
public void getArea(int length, int breadth) {
System.out.println("The area of the rectangle is " + (length * breadth));
}
}
class Main {
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
r1.getArea(5, 6);
}
}

Output:-

14 | P a g e
Program 8

A Program to Use Abstract Class.

abstract class Animal{


public abstract void sound();
}

class Dog extends Animal{


public void sound(){
System.out.println("Woof");
}
}

class Main {
public static void main(String[] args) {
Animal obj = new Dog();
obj.sound();
}
}

Output:-

15 | P a g e
Program 9

Write a Program for Multiple Inheritance using Interface.

interface X
{
public void myMethod();
}
interface Y
{
public void myMethod();
}
class JavaExample implements X, Y
{
public void myMethod()
{
System.out.println("Implementing more than one interfaces");
}

}
public class Main {
public static void main(String args[]) {
JavaExample obj = new JavaExample();
obj.myMethod();
}}

16 | P a g e
Output:-

17 | P a g e
Program 10

Write a Program for using multidimensional array in java.

public class Main {


public static void main(String args[]) {
int[][] values =
{
{ 10, 20, 30 }, { 40, 50, 60 }, { 70, 80, 90 },
{ 11, 21, 31 }
};
for (int row = 0; row < 4; row++)
{
for (int col = 0; col < 3; col++)
{
System.out.print(values[row][col] + " ");
}
System.out.println();
}
}
}

Output:-

18 | P a g e

You might also like