JP-2

 1a.) Write a Java program to implement types of inheritance

  • Simple Inheritance

class Animal

{

void eat()

{

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

}

}

class Dog extends Animal

{

void bark()

{

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

}

}

class TestInheritance

{

public static void main(String args[])

{

Dog d=new Dog();

d.bark();

d.eat();

}

}


Output:


  • Multilevel Inheritance

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 TestInheritance

{

public static void main(String args[])

{

BabyDog d=new BabyDog();

d.weep();

d.bark();

d.eat();

}

}


Output:


  • Hierarchical Inheritance

class Animal

{

void eat()

{

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

}

}

class Dog extends Animal

{

void bark()

{

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

}

}

class Cat extends Animal

{

void meow()

{

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

}

}

class TestInheritance

{

public static void main(String args[])

{

Cat c=new Cat();

c.meow();

c.eat();

}

}


Output:


1b.) Write a java program to implement Method Overriding

Code:

class Vehicle

{

void run()

{

System.out.println("Vehicle is running");

}

}

class Bike2 extends Vehicle

{

void run()

{

System.out.println("Bike is running safely");

}

public static void main(String args[])

{

Bike2 obj=new Bike2();

obj.run();

}

}


Output:


2.) Write a java program to implement Abstract class

Code:

abstract class Shape

{

abstract void draw();

}

class Rectangle extends Shape

{

void draw()

{

System.out.println("Drawing rectangle");

}

}

class Circle extends Shape

{

void draw()

{

System.out.println("Drawing Circle");

}

}

class TestAbstraction

{

public static void main(String args[])

{

Shape s=new Circle();

s.draw();

}

}


Output:


3.) Write a java program to implement interface

Code:

interface printable

{

void print();

}

class A6 implements printable

{

public void print()

{

System.out.println("Hello");

}

public static void main(String args[])

{

A6 obj=new A6();

obj.print();

}

}


Output:


Code 2:

interface Drawable

{

void draw();

}

class Rectangle implements Drawable

{

public void draw()

{

System.out.println("drawing rectangle");

}

}

class Circle implements Drawable

{

public void draw()

{

System.out.println("drawing circle");

}

}

class TestInterface

{

public static void main(String args[])

{

Drawable d=new Circle();

d.draw();

}

}

Output:


Comments

Popular posts from this blog

python(BI)

(PP-7)Create a class called Numbers, which has a single class attribute called MULTIPLIER, and a constructor which takes the parameters x and y (these should all be numbers). i. Write a method called add which returns the sum of the attributes x and y. ii. Write a class method called multiply, which takes a single number parameter a and returns the product of a and MULTIPLIER. iii. Write a static method called subtract, which takes two number parameters, b and c, and returns b - c. iv. Write a method called value which returns a tuple containing the values of x and y. Make this method into a property, and write a setter and a deleter for manipulating the values of x and y.