Assignment # 3: Concept of Inheritance
Assignment # 3: Concept of Inheritance
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:
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.
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();
} }
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:
• 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 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:
Sphere class
}
test class:
package shapeAbstructCLass;
}
}
Output: