0% found this document useful (0 votes)
42 views

Lab 7

The document contains code implementing inheritance and polymorphism in Java. It defines classes A, B, and C that extend class A and override its dismsg() method to print different messages. Main instantiates objects of these classes and calls dismsg(), demonstrating polymorphic behavior. It also defines abstract class Vehicle and subclasses Car and Bus that override its abstract color() method. Main creates Vehicle objects for each subclass and calls color(), again showing polymorphism. The rest of the code defines a Product class and uses it in an application to input product details, find the highest priced product, and calculate the total cost.

Uploaded by

Tony-99
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Lab 7

The document contains code implementing inheritance and polymorphism in Java. It defines classes A, B, and C that extend class A and override its dismsg() method to print different messages. Main instantiates objects of these classes and calls dismsg(), demonstrating polymorphic behavior. It also defines abstract class Vehicle and subclasses Car and Bus that override its abstract color() method. Main creates Vehicle objects for each subclass and calls color(), again showing polymorphism. The rest of the code defines a Product class and uses it in an application to input product details, find the highest priced product, and calculate the total cost.

Uploaded by

Tony-99
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

class A{

public void dismsg(){

System .out.println ("This is A");


}
}

class B extends A{
public void dismsg(){

System .out.println ("This is B");


}
}

class C extends A{
public void dismsg(){

System .out.println ("This is C");


}
}

public class Program


{
public static void main(String... args) {
A mya=new A();
A myb=new B();
A myc=new C();
mya.dismsg ();
myb.dismsg ();
myc.dismsg ();
}
}
abstract class Vehicle{
abstract void color();
}
class Car extends Vehicle {
public void color() {
System .out.println ("red");
}
}
class Bus extends Vehicle {

public void color() {


System .out.println ("White");
}

public class Program


{
public static void main(String[] args) {
Vehicle a=new Car() ;
Vehicle b=new Bus() ;
a.color () ;
b.color ();
}
}
package MyNormalwork;
import MyNormalwork.Product;
import java.util.Scanner;

public class MynewMain {

public static void main(String[] args) {

Product arr[] = new Product[3];


Scanner scanner = new Scanner(System.in);

System.out.println("Enter product details :- ");

for(int i = 0 ; i < arr.length ; i++) {

System.out.print("Enter id of product " + (i+1) + " : ");


int id = scanner.nextInt();
System.out.print("Enter price of product " + (i+1) + " : ");
int prc = scanner.nextInt();
System.out.print("Enter quantity of product " + (i+1) + " : ");
int qty = scanner.nextInt();
arr[i] = new Product(id, prc, qty);
}
System.out.println("All products are :- ");

for(int i = 0; i < arr.length ; i++) {

Product pr = arr[i];
System.out.println("id of product " + (i+1) + " : " + pr.getPid());
System.out.println("price of product " + (i+1) + " : "
+ pr.getPrice());
System.out.println("quantity of product " + (i+1) + " : "
+ pr.getQuantity());
System.out.println();
}
int max=0;
for(int i = 0 ; i < arr.length ; i++) {
Product p=arr[i];

int price=p.getPrice();
if(max<price);{
max=price;
}
}
System.out.println("higest Price"+max);
for(int i = 0 ; i < arr.length ; i++) {
Product p=arr[i];
int price=p.getPrice();
if(max==price)
{
System.out.println("the max product id"+p.getPid());
}
}
int total=0;
for(int i = 0; i < arr.length ; i++) {

Product pr = arr[i];
int a=pr.getPrice();
int b=pr.getQuantity();
System.out.println();
int xyz=a*b;
total=total+xyz;
}
System.out.println("total amount spend:"+total);
}
}
package MyNormalwork;
import MyNormalwork.Product;
import java.util.Scanner;

public class MynewMain {

public static void main(String[] args) {

Product arr[] = new Product[3];


Scanner scanner = new Scanner(System.in);

System.out.println("Enter product details :- ");

for(int i = 0 ; i < arr.length ; i++) {

System.out.print("Enter id of product " + (i+1) + " : ");


int id = scanner.nextInt();
System.out.print("Enter price of product " + (i+1) + " : ");
int prc = scanner.nextInt();
System.out.print("Enter quantity of product " + (i+1) + " : ");
int qty = scanner.nextInt();
arr[i] = new Product(id, prc, qty);
}
System.out.println("All products are :- ");

for(int i = 0; i < arr.length ; i++) {

Product pr = arr[i];
System.out.println("id of product " + (i+1) + " : " + pr.getPid());
System.out.println("price of product " + (i+1) + " : "
+ pr.getPrice());
System.out.println("quantity of product " + (i+1) + " : "
+ pr.getQuantity());
System.out.println();
}
int max=0;
for(int i = 0 ; i < arr.length ; i++) {
Product p=arr[i];

int price=p.getPrice();
if(max<price);{
max=price;
}
}
System.out.println("higest Price"+max);
for(int i = 0 ; i < arr.length ; i++) {
Product p=arr[i];
int price=p.getPrice();
if(max==price)
{
System.out.println("the max product id"+p.getPid());
}
}
int total=0;
for(int i = 0; i < arr.length ; i++) {

Product pr = arr[i];
int a=pr.getPrice();
int b=pr.getQuantity();
System.out.println();
int xyz=a*b;
total=total+xyz;
}
System.out.println("total amount spend:"+total);
}
}

You might also like