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

OOP Lab-IV

1. Is-A relationship using extends keyword and Instanceof keyword


// IS-A example //implements keyword to inherit interface
class Animal { } interface AnimalTwo{ }
class Mammal extends Animal { } class MammalTwo implements AnimalTwo{}
class Reptile extends Animal { } public class DogTwo extends MammalTwo {
public class Dog extends Mammal { public static void main(String args[]) {
public static void main(String args[]) { MammalTwo m = new MammalTwo();
Animal a = new Animal(); DogTwo d = new DogTwo();
Mammal m = new Mammal(); System.out.println(m instanceof AnimalTwo);
Dog d = new Dog(); System.out.println(d instanceof MammalTwo);
System.out.println(m instanceof Animal); System.out.println(d instanceof AnimalTwo);
System.out.println(d instanceof Mammal); }
System.out.println(d instanceof Animal); }
} }
➢ Write a program that shows IS-A relationship in Vehicle, car and tvs.
2. Java overriding [override the functionality of an existing method]
// method overriding //overridden method super keyword
class Animal {
class Animal {
public void move() {
public void move() {
System.out.println("Animals can move"); } }
System.out.println("Animals can
class Dog extends Animal {
move");
public void move() {
}}
System.out.println("Dogs can walk and run"); }
class Dog extends Animal {
public void bark() {
public void move() {
System.out.println("Dogs can bark"); } }
// invokes the super class method
public class TestDog {
super.move();
public static void main(String args[]) {
System.out.println("Dogs can walk
Animal a = new Animal(); // Animal reference and object
and run");
Animal b = new Dog(); // Animal reference but Dog object
}}
a.move(); // runs the method in Animal class

1
b.move(); // runs the method in Dog class public class TestDog {
b.bark(); public static void main(String args[])
}} {
// Animal reference but Dog object
Animal b = new Dog();
b.move();
// runs the method in Dog class
}}
➢ Create a super class Degree having a method “getDegree” that prints “ I got
degree”. It has two sub classes namely “Undergraduate” and “Postgraduate”
each having a method with the same name that prints “I am an undergraduate”
and “I am postgraduate” respectively. Call the method by creating an object of
each of the three classes.
➢ A class has an integer data member “I” and a method named “printNum” to print
the value of “I”. Its subclass also has an integer data member “j” and a method
named “printNum” to print the value of “j”. Make an object of the subclass and
use it to assign a value to “I” and to “j”. Now call the method “printNum” by the
object.
3. Polymorphism [ability of an object to take on many forms]
interface Vegetarian{}
class Animalxx{}
public class Deer extends Animalxx implements Vegetarian{
public static void main(String args[]){
Deer d = new Deer();
Animalxx a = d;
Vegetarian v = d;
Object o = d;
System.out.println(d instanceof Animalxx);
System.out.println(d instanceof Vegetarian);
System.out.println(d instanceof Object);
}}

2
4. Abstraction [process of hiding the implementation details from the user]
// using Abstract classes and methods public class FullTimeEmployee extends
public abstract class EmpNewCheck { EmpNewCheck {
public FullTimeEmployee(String
private String name; name, int paymentPerHour) {
private int paymentPerHour; super(name,
paymentPerHour);
public EmpNewCheck(String name, }
int paymentPerHour) { @Override
this.name = name; public int calculateSalary() {
this.paymentPerHour = return
paymentPerHour; getPaymentPerHour() * 8;
} }
}
public abstract int calculateSalary();
public String getName() { public class Contractor extends
return name; EmpNewCheck {
}
public void setName(String name) { private int workingHours;
this.name = name; public Contractor(String name,
} int paymentPerHour, int workingHours)
public int getPaymentPerHour() { {
return paymentPerHour; super(name,
} paymentPerHour);
public void setPaymentPerHour(int this.workingHours =
paymentPerHour) { workingHours;
this.paymentPerHour = }
paymentPerHour; @Override
} public int calculateSalary() {
} return
getPaymentPerHour() * workingHours;
} }
public class DDUEMPRUN{
public static void main(String args[]){
Contractor c=new Contractor("Abebe",200,40);
FullTimeEmployee f=new FullTimeEmployee("Dagmawit",350);

System.out.println("Contract Employee:" + c.getName()+" \n


Salary="+c.calculateSalary());
System.out.println("Full Time Employee:"+f.getName()+" \n
Salary="+f.calculateSalary());

}
}

3
5. Encapsulation [mechanism of wrapping the data (variables) and methods]
public class EncapTest { public static void main(String args[]){
private String name; EncapTest e=new EncapTest();
private double salary; e.setName("Hana");
public String getName(){ e.setSalary(10000);
return name;
} System.out.println("name="+e.getName()+
public double getSalary(){ "salary=" +e.getSalary());
return salary; }
}
public void setName(String n){ }
name=n;
}
public void setSalary(double s){
salary=s;
}
➢ Write a java program to display Name, IdNo and age all the three variables of
a class will be hidden from other classes and should be accessed through the
methods of their class.

You might also like