[go: up one dir, main page]

0% found this document useful (0 votes)
68 views6 pages

Assignment # 3: Concept of Inheritance

The document describes an assignment question asking to: 1. Explain inheritance and the types of inheritance supported in Java with examples 2. Explain the differences between abstract classes and interfaces with examples 3. Create an abstract Shape class with radius and height, and concrete Sphere and Cylinder classes that inherit from Shape and implement volume and surface area calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views6 pages

Assignment # 3: Concept of Inheritance

The document describes an assignment question asking to: 1. Explain inheritance and the types of inheritance supported in Java with examples 2. Explain the differences between abstract classes and interfaces with examples 3. Create an abstract Shape class with radius and height, and concrete Sphere and Cylinder classes that inherit from Shape and implement volume and surface area calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Name: Muhammad Sami Ali

Roll Number: BSSE-2020F-201


Section: E

ASSIGNMENT # 3

Question 1: Describe the concept of inheritance? In general, how many types of inheritance can
be applied in programming languages? Which type of inheritance java supports? Explain your
answer with suitable examples.

Concept of inheritance:

Inheritance is a mechanism in which one class acquires the property of another class. For
example, a child inherits the traits of his/her parents. With inheritance, we can reuse the fields
and methods of the existing class. Hence, inheritance facilitates Reusability and is an important
concept of OOPs. Inheritance is the process of creating a new Class, called the Derived Class,
from the existing class, called the Base Class. The Inheritance has many advantages, the most
important of them being the reusability of code. Rather than developing new Objects from
scratch, new code can be based on the work of other developers, adding only the new features
that are needed. The reuse of existing classes saves time and effort.

Types of Inheritance:

There are Various types of inheritance in Java:

 Single Inheritance: In Single Inheritance one class extends another class (one class
only).
 Multiple Inheritance: In Multiple Inheritance, one class extending more than one
class. Java does not support multiple inheritance.
 Multilevel Inheritance: In Multilevel Inheritance, one class can inherit from a
derived class. Hence, the derived class becomes the base class for the new class.
 Hierarchical Inheritance: In Hierarchical Inheritance, one class is inherited by
many sub classes.
 Hybrid Inheritance: Hybrid inheritance is a combination of Single and Multiple
inheritance.

Type of inheritance java supports:


Java supports only single, multilevel & hierarchical type of inheritance using classes.
Java does not support multiple and hybrid inheritance with classes.
Question 2: What are the difference between abstract class and interface? Explain your answer
with suitable example?
ABSTRACT CLASS INTERFACE

1) Abstract class can have abstract and Interface can have only abstract methods.
non-abstract methods. Since Java 8, it can have default and static
methods also.
2) Abstract class does not support Interface supports multiple inheritance.
multiple inheritance.
3) Abstract class can have final, non- Interface has only static and final variables.
final, static and non-static variables.
4) Abstract class can provide the Interface can't provide the implementation
implementation of interface. of abstract class.
5) The abstract keyword is used to The interface keyword is used to declare
declare abstract class. interface.
6) An abstract class can extend another An interface can extend another Java
Java class and implement multiple Java interface only.
interfaces.
7) An abstract class can be extended An interface can be implemented using
using keyword "extends". keyword "implements".
8) A Java abstract class can have class Members of a Java interface are public by
members like private, protected, etc. default.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }

Example of Abstract Class vs Interface

Code:
public class JavaTester {
public static void main(String args[]) {
Animal tiger = new Tiger();
tiger.eat();
Cat lion = new Lion();
lion.eat();
}
}
interface Animal {
public void eat();
}
class Tiger implements Animal {
public void eat(){
System.out.println("Tiger eats");
}
}
abstract class Cat {
abstract public void eat();
}
class Lion extends Cat{
public void eat(){
System.out.println("Lion eats");
}
}

Output:

Tiger eats
Lion eats

Question 3: Construct java abstract class Shape containing private information of radius (int)
and height (int). The class contains the following for calculations:

• Default constructor setting radius and height to 5.


• A parameterized constructor setting radius and height to values provided in parameters.
• An abstract method volume () returning type double.
• An abstract method surface_area () returning type double
Then construct a child class Sphere inherit from Shape class. The class contains the following:
• Default constructor calling parent class default constructor to set radius and height to
5.
• A parameterized constructor calling parent class constructor for setting radius and
height to values provided in parameters.
• Provide implementation of abstract volume method to calculate and return Volume of
Sphere (V=4/3*PI*R3).

• Provide implementation of surface area method to calculate and return Surface Area
of Sphere (SA=4*PI*R2).

Then construct a child class Cylinder inherit from Shape class. The class contains the following:
• Default constructor calling parent class default constructor to set radius and height to
5.
• A parameterized constructor calling parent class constructor for setting radius and
height to values provided in parameters.

• Provide implementation of abstract volume method to calculate and return Volume of


Cylinder (V=PI*R2*H).

• Provide implementation of surface area method to calculate and return Surface Area
of Sphere (SA=2*PI*R2 + 2*PI*R*H)

Make sure to use Math and Scanner class for computing results and inputs. Then construct a
Testing class containing main() method and create ONE object of Sphere and Cylinder class.
Initialize both objects using different constructor. Finally compute and display Volume of
Sphere and Cylinder. Similarly compute and display Surface Area of Cylinder.

Source Code:
Abstract class Shape:

public abstract class Shape {

private int raduis;


private int height;
public Shape() {
this.raduis = 5;
this.height = 5;
}
public Shape(int raduis, int height) {
this.raduis = raduis;
this.height = height;
}
public abstract double volume();
public abstract double surface_area();
public int getRaduis() {
return raduis;
}
public int getHeight() {
return height;
}
public void setRaduis(int raduis) {
this.raduis = raduis;
}
public void setHeight(int height) {
this.height = height;
}

Sphere class

public class Sphere extends Shape {


public Sphere() {
super();
}
public Sphere(int raduis, int height) {
super(raduis, height);
}

public double volume() {


return (((double)4/3) * Math.PI * Math.pow(getRaduis(), 3));
}

public double surface_area() {


return 4 * Math.PI * Math.pow(getRaduis(), 2);
}

}
test class:

package shapeAbstructCLass;

public class Testclass {


public static void main(String[] args) {
//create an object of sphere
Sphere s = new Sphere();
System.out.println("The volume of the sphere is "+s.volume() + “\n”);
System.out.println("The Surface Area of the sphere is "+s.surface_area());

}
}

Output:

You might also like