0% found this document useful (0 votes)
3 views7 pages

UEC2023155 Assignment 10

The document contains Java programs demonstrating various concepts of inheritance, including simple inheritance, member access, multilevel inheritance, and method overriding. It includes classes for different shapes and sports players, showcasing how to utilize constructors, method overriding, and polymorphism. The programs are designed to illustrate the principles of object-oriented programming in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

UEC2023155 Assignment 10

The document contains Java programs demonstrating various concepts of inheritance, including simple inheritance, member access, multilevel inheritance, and method overriding. It includes classes for different shapes and sports players, showcasing how to utilize constructors, method overriding, and polymorphism. The programs are designed to illustrate the principles of object-oriented programming in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

USN : UEC2023155

Name : Anuja Vinodrao Shingane


Assignment 10 : Inheritance
Batch : A3

Title :
a) Program on simple inheritance
b) Program to demonstrate Member access and inheritance
c) Program to demonstrate mul level inheritance without and with the use of
thekeyword super
d) Program to demonstrate Method Overriding.

class A {
int i, j; void
showij() {
System.out.println (i+ " " +j);
}
}

class B extends A { int


k; void sum ()
{
System.out.println("i+j+k = " + (i+j+k));
}
}

class ShowInherit { public sta c void main


(String args[]) {
A a = new A (); B b
= new B ();

a.i = 10;
a.j = 20;

b.i = 100;
b.j = 200;
b.k = 300;

a.showij();
b.showij();
b.sum();
}
}
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ javac ShowInherit.java
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ java ShowInherit
10 20 100
200 i+j+k =
600

class Box { double


length; double
width; double
height;
// constructor clone of an object Box
(Box ob) {
// pass object to constructor
length = ob.length; width =
ob.width;
height = ob.height;
}
// constructor when all dim spec
Box (double x, double y, double z) {
length = x; width = y;
height = z;
}
// constructor when no dim specified
Box () { length = -1;
width = -1;
height = -1;
}
}

class BoxWeight extends Box { double


weight;

BoxWeight (double l, double w, double h, double wt) { super (l,


w, h);
weight = wt;
}
double volume () { return length *
width * height;
}

class DemoBox { public sta c void main (String


args []) {
BoxWeight mybox1 = new BoxWeight (10, 20, 30, 50); BoxWeight
mybox2 = new BoxWeight (5, 10, 15, 20);

double vol;
vol = mybox1.volume ();
System.out.println ("The volume of the mybox1 is " + vol);
System.out.println ("The weight of the mybox1 is " + mybox1.weight);

vol = mybox2.volume ();


System.out.println ("The volume of the mybox2 is " + vol);
System.out.println ("The weight of the mybox2 is " + mybox2.weight);
}
}
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ javac DemoBox.java
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ java DemoBox
The volume of the mybox1 is 6000.0
The weight of the mybox1 is 50.0
The volume of the mybox2 is 750.0 The
weight of the mybox2 is 20.0

import java.u l.Scanner;


class Player { String
name; int age; String
city; int gamesPlayed;
String type;

Scanner in = new Scanner (System.in);


void getDetails () {
System.out.print ("Enter the name of the player: "); name
= in.next();
System.out.print ("Enter the age of the player: "); age
= in.nextInt ();
System.out.print ("Enter the city: "); city
= in.next ();
System.out.print ("Enter the number of games played by the player: ");
gamesPlayed = in.nextInt ();
System.out.print ("Enter the type of the game: "); type
= in.next();
}

void displayDetails () {
System.out.println ("Name | " + name);
System.out.println ("Age | " + age);
System.out.println ("City | " + city);
System.out.println ("Played | " +gamesPlayed);
System.out.println ("Type | " + type);
}
}
class Cricket extends Player {
int runs; int wickets;

Scanner in = new Scanner (System.in);


void getDetails () { super.getDetails
();
System.out.print ("Enter the runs made by the player: "); runs =
in.nextInt ();
System.out.print ("Enter the wickets taken by the player: "); wickets =
in.nextInt ();
}
void displayDetails () { super.displayDetails
();
System.out.println ("Runs | " + runs);
System.out.println ("Wickets | " + wickets);
}
}

class Football extends Player { int


goals;

Scanner in = new Scanner (System.in);


void getDetails () { super.getDetails
();
System.out.print ("Enter the goals made by the player: "); goals =
in.nextInt ();
}

void displayDetails () { super.displayDetails


();
System.out.println ("Goals | " + goals);
}
}

class Hockey extends Player { int


goals;

Scanner in = new Scanner (System.in);


void getDetails () { super.getDetails
();
System.out.print ("Enter the goals made by the player: "); goals =
in.nextInt ();
}

void displayDetails () { super.displayDetails


();
System.out.println ("Goals | " + goals);
}
}

class Inherit { public sta c void main (String args


[]) {
Cricket var1 = new Cricket ();
Football var2 = new Football (); Hockey
var3 = new Hockey ();

var1.getDetails();
var1.displayDetails ();
var2.getDetails();
var2.displayDetails ();
var3.getDetails ();
var3.displayDetails ();

}
}
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ javac Inherit.java
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ java Inherit
Enter the name of the player: Sachin
Enter the age of the player: 34
Enter the city: Pune
Enter the number of games played by the player: 100
Enter the type of the game: Cricket
Enter the runs made by the player: 100
Enter the wickets taken by the player: 50
Name | Sachin
Age | 34
City | Pune
Played | 100
Type | Cricket
Runs | 100
Wickets | 50
Enter the name of the player: Nehman
Enter the age of the player: 29
Enter the city: Mumbai
Enter the number of games played by the player: 40
Enter the type of the game: Football
Enter the goals made by the player: 100
Name | Nehman
Age | 29
City | Mumbai
Played | 40
Type | Football
Goals | 100
Enter the name of the player: Virat
Enter the age of the player: 35
Enter the city: Kolkata
Enter the number of games played by the player: 50
Enter the type of the game: Hockey
Enter the goals made by the player: 100
Name | Virat
Age | 35
City | Kolkata
Played | 50
Type | Hockey
Goals | 100
class Figure { double dim1,
dim2;

Figure (double a, double b) { dim1


= a;
dim2 = b;
}

double area () {
System.out.println ("The Area is undefined :"); return
0;
}
}

class Rectangle extends Figure {


Rectangle (double a, double b) { super
(a, b);
}
double area () { return
dim1*dim2;
}
}

class Triangle extends Figure {


Triangle (double a, double b) { super
(a, b);
}
double area () { return
0.5*dim1*dim2;
}
}

class AreaRectTri { public sta c void main (String


args []) {
Rectangle r = new Rectangle (10, 20);
Triangle t = new Triangle (30, 40); Figure

figref;

figref = r;
System.out.print ("The Area of the Rectangle is "+ figref.area());

figref = t;
System.out.print ("The Area of the Triangle is "+figref.area());
}
}
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ javac AreaRectTri.java
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ java AreaRectTri
The Area of the Rectangle is 200.0The Area of the Triangle is 600.0(base)

You might also like