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

java

The document contains Java code demonstrating concepts of inheritance, interfaces, and abstract classes. It includes examples of animal classes with overridden methods, mathematical operations, and artwork classes with abstract methods. Additionally, it poses questions about Java inheritance and the differences between abstract classes and interfaces.

Uploaded by

ZUYEL RANA 5043
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

java

The document contains Java code demonstrating concepts of inheritance, interfaces, and abstract classes. It includes examples of animal classes with overridden methods, mathematical operations, and artwork classes with abstract methods. Additionally, it poses questions about Java inheritance and the differences between abstract classes and interfaces.

Uploaded by

ZUYEL RANA 5043
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.class Animal{ public void makeSound() 3.

interface Readable { void read();}


{ sout("Animal can sound.");}} class Lion interface Borrowable { void borrow();
extends Animal{@Override public void void returnBook();}
makeSound() { sout("Lion roars."); }} class interface LibraryItem extends Readable,
Elephant extends Animal{ @Override public Borrowable { void read(); void borrow();
void makeSound(){ sout("Elephant growls.");}} void returnBook();}
class Monkey extends Animal{ @Override class Book implements LibraryItem
public void makeSound(){ sout("Monkey { public void read(){sout("The book is
chatters."); }} public class Problem1{ public reading."); } public void borrow(){
static void main(String[] args){ Animal lion = sout("The book has borrowed."); }
new Lion(); lion.makeSound(); public void returnBook()
Animal elephant = new Elephant(); { sout("The book has
elephant.makeSound();Animal monkey = new returned"); }} public class
Monkey(); monkey.makeSound(); }} Problem3{public static void main(String[] args)
{ Book book = new Book();
2. public class
book.read();book.borrow();
MathOperations{ public void Add(int
book.returnBook(); }}
a,int b){ sout("Addition : " + a + " + " + b + " = " +
(a+b)); } public void 4. public class Problem4{public static int ans;
Add(double a,double b){ sout("Addition : " + a + public static intprocessNumbers
" + " + b + " = " + (a+b)); } public void Sub(int (intnumerator,int denominator){ try{ ans =
a,int b){ sout("Substraction : " + a + " - " + b + " = numerator/denominator; }
" + (a-b)); } public void Sub(double a,double b) catch (ArithmeticException e){sout(e);
{ sout("Substraction : " + a + " - " + b + " = " + (a- sout("Cannot devide by zero.");}return ans;}
b));}public void Multi(int a,int b) public static void main(String[] args)
{ sout("Multiplication : " + a + " * " + b + " = " + { processNumbers(10,0); }}
(a*b)); }public void Multi(double a,double b)
5. abstract class
{ sout("Multiplication : " + a + " * " + b + " = " +
Artwork{ abstract void
(a*b)); }public void Div(int a,int b){sout("Division
display(); abstract void
: " + a + " / " + b + " = " + (a/b));}
calculatePrice();} class
public void Div(double a,double b)
Painting extends Artwork{ public
{ sout("Division : " + a + " / " + b + " = " + (a/b)); }
void display(){sout("This painting is
public static void main(String[] args)
awesome."); }public void calculatePrice()
{ MathOperations obj = new MathOperations();
{ sout("The price of this painting is 500 Taka.");}}
obj.Add(10,20);obj.Sub(20,10); obj.Multi(25,20);
class Sculpture extends Artwork{ public void
obj.Div(100,20);
display(){sout("This is a sculpture."); }
sout();obj.Add(10.236,4.2365);obj.Sub(10.236,4
public void calculatePrice(){ sout("The price of
.2365);obj.Multi(10.236,4.2365);
this sculpture is 50000 Taka.");}}
obj.Div(10.236,4.2365); }}
public class Problem5{public static void
main(String[] args){ Painting painting = new
Painting();painting.display();
painting.calculatePrice();
Sculpture sculpture = new Sculpture();
sculpture.display();sculpture.calculatePrice();}}
1.define java inheritance and its type?difference
between abstract and interface?

You might also like