You are on page 1of 21

Name Syed Muhammad Asadullah

Enrolment 02-132212-011
Program BCE 3A
Lab 9
INHERITANCE

EXAMPLE

package gui;

public class GUI {

public static void main(String[] args) {

Cat m =new Cat();

BabyDog d=new BabyDog();

d.bark();

d.eat();

d.weep();

m.eat();

m.meaow();

class Animal

void eat()

System.out.println("eating.....");

}
}

class Dog extends Animal

void bark()

System.out.println(" barking... ");

class BabyDog extends Dog

void weep()

System.out.println("weeping....");

class Cat extends Animal

void meaow()

System.out.println("meaowing...");

OUTPUT
TASK 1
WRITE A PROGRAM IN JAVA TO SHOW THE INHERITANCE?

Bahria

Faculty Student Staff

SOLUTION

package bahria;
import java.util.*;
public class BAHRIA {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("WELCOME TO THE BAHRIA UNIVERSITY");
System.out.println("___BAHRIA UNIVERSITY_____");
System.out.println("KARACHI");
faculty t =new faculty();
System.out.println("the information of FACULTY");
t.information();
t.salary();
System.out.println("The information of STUDENT");
student s=new student();
s.information();
s.fees();
System.out.println("The information of STAFF");
staff w=new staff();
w.information();
w.payement();
}

}
class University

Scanner input=new Scanner(System.in);


String name,department;
int ID,age;
double CNIC,phonenumber;

void information()
{
System.out.println("Enter the name ");
name=input.next();
System.out.println("The name of person is " +name);
System.out.println("Enter the department");
department=input.next();
System.out.println("The department is " +department);
System.out.println("Enter the ID");
ID=input.nextInt();
System.out.println("The ID of person is " +ID);
System.out.println("Enter the age ");
age=input.nextInt();
System.out.println("The age of person is " +age);
System.out.println("Enter the CNIC");
CNIC=input.nextDouble();
System.out.println("The CNIC is " +CNIC);

System.out.println("Enter the phone number");


phonenumber=input.nextDouble();
System.out.println("The phone number is " +phonenumber);
}

}
class faculty extends University
{
double salary;

void salary()
{ System.out.println("Enter the salary ");
salary =input.nextDouble();
System.out.println("The salary of faculty member is " +salary);

}
}
class student extends University
{
double FEES;
void fees()
{
System.out.println("Enter the fees of student");
FEES=input.nextDouble();
System.out.println("The fees of student is " +FEES);
}

}
class staff extends University
{
double pay;
void payement()
{
System.out.println("Enter the wage of staff");
pay=input.nextDouble();
System.out.println("The wage of staff is " +pay);
}
}
Output
TASK 2
VEHICLES

Vehicle

Bicylcle Bike Rikhshaw Truck Car

SOLUTION

package javaapplication168;

import java.util.*;
public class JavaApplication168 {

public static void main(String[] args) {


System.out.println("WELCOME TO THE TYPES OF VEHICLES");
System.out.println("Enter the information of bicycle ");
bicycle b=new bicycle();
b.info();
b.fuelsource();
System.out.println("Enter the information of bike ");
bike m=new bike();
m.info();
m.developer();
System.out.println("Enter the information of car ");
car c=new car();
c.info();
c.cartype();
System.out.println("Enter the information of truck ");
truck t=new truck();
t.info();
t.usage();
}

}
class Vehicle
{
Scanner input=new Scanner(System.in);
String name;
int modelno;
int enginetype,passenger;
int wheels,doors;
void info()
{
System.out.println("Enter the name");
name=input.next();
System.out.println("The name of vehicle is " +name);
System.out.println("Enter model number ");
modelno=input.nextInt();
System.out.println("The model number of vehicle is " +modelno);
System.out.println("Enter the enginetype");
enginetype=input.nextInt();
System.out.println("The engine type of vehicle is " +enginetype);
System.out.println("Enter the number of wheels ");
wheels=input.nextInt();
System.out.println("The number of wheels in vehicle is " +wheels);
System.out.println("Enter the number of doors");
doors=input.nextInt();
System.out.println("The number of doors in vehicle is " +doors);
System.out.println("Enter the number of passenger");
passenger=input.nextInt();
System.out.println("The number of passenger are "+passenger);
}
}
class bicycle extends Vehicle
{
void fuelsource()
{
System.out.println("the fuel source of bicycle is HUMAN POWER and MOTOR
POWER");
}

}
class bike extends Vehicle
{

void developer()
{
System.out.println("It was developed by GOTTLIEB DAIMLER");
}
}
class rickshaw extends Vehicle
{
void countries()
{
System.out.println("Rickshaw is commonly use in ASIA while it is not so popular in other
countries");
}
}
class car extends Vehicle
{
String cartype;
void cartype()
{
System.out.println("Enter the car type");
cartype=input.nextLine();
System.out.println("The type of car is "+cartype);
}
}
class truck extends Vehicle
{
void usage()
{
System.out.println("The truck is use for transporting material and also for IMPORT and
EXPORT");
}
}
OUTPUT
TASK 3
WRITE A JAVA PROGRAM TO SHOW THE INHERITANCE?

SOLUTION
package javaapplication25;
import java.util.Scanner;
import java.util.*;

public class JavaApplication25 {

public static void main(String[] args) {


System.out.println("ANIMALS HIERARCHY.......");
System.out.println("REPTILES");
System.out.println("SNAKE.....");
snake s=new snake();
lizard l=new lizard();
s.Data();
s.Rdata();
s.Sdata();
System.out.println("LIZARD.....");
l.Data();
l.Rdata();
l.Ldata();
System.out.println("BIRDS.....");
parrot p=new parrot();
System.out.println("PARROT....");
p.Data();
p.Bdata();
p.Pdata();
System.out.println("MAMMALS....");
horse h=new horse();
System.out.println("HORSE....");

h.Data();
h.Mdata();
h.Hdata();
bat b=new bat();
System.out.println("BATS......");
b.Data();
b.Mdata();
b.Tdata();

}
class Animal
{
public void Data()
{
Scanner sc=new Scanner(System.in);
double height,weight;
int no_legs;
int eyes;
System.out.println("ENTER HEIGHT: ");
height=sc.nextDouble();
System.out.println("ENTER WEIGHT: ");
weight=sc.nextDouble();
System.out.println("ENTER NUMBER OF LEGS: ");
no_legs=sc.nextInt();
System.out.println("ENTER NUMBER OF EYES: ");
eyes=sc.nextInt();
}
}
class reptile extends Animal
{
public void Rdata()
{
Scanner c=new Scanner(System.in);
String type;
System.out.println("ENTER REPTILE SPECIES:");
type=c.nextLine();
}
}
class snake extends reptile
{
public void Sdata()
{
Scanner r=new Scanner(System.in);
double length;
System.out.println("ENTER SNAKE LENGHT: ");
length=r.nextDouble();
}
}
class lizard extends reptile
{
public void Ldata()
{
String food;
Scanner s=new Scanner(System.in);
System.out.println("WHAT DOES LIZARDS EAT: ");
food=s.nextLine();
}
}
class bird extends Animal
{
public void Bdata()
{
Scanner ss=new Scanner(System.in);
String cha;
System.out.println("BIRDS COMMON CHARACTERISTICS: ");
cha= ss.nextLine();
}
}
class parrot extends bird
{
public void Pdata()
{
Scanner ss=new Scanner(System.in);
String ca;
System.out.println("PARROTS COMMON CHARACTERISTICS: ");
ca=ss.nextLine();
}
}
class mammal extends Animal
{
public void Mdata()
{
Scanner input=new Scanner(System.in);
String c;
System.out.println("MAMMALS COMMON CHARACTERISTICS: ");
c=input.nextLine();
}
}
class horse extends mammal
{
public void Hdata()
{
Scanner input=new Scanner(System.in);
String h;
System.out.println("HORSE COMMON CHARACTERISTICS: ");
h=input.nextLine();
}
}
class bat extends mammal
{
public void Tdata()
{
Scanner input=new Scanner(System.in);
String b;
System.out.println("BATS COMMON CHARACTERISTICS: ");
b=input.nextLine();
}

}
Output

You might also like