Professional Documents
Culture Documents
Inheritance 23101B0069
Inheritance 23101B0069
ASSIGNMENT 2 : Inheritance
23101B0069
(1) Simple Inheritance
class Animal {
void sound() {
void bark() {
System.out.println("Dog barks");
myDog.sound();
myDog.bark();
OUTPUT:
(2) Constructor Inheritance
class Vehicle {
Vehicle() {
System.out.println("Vehicle constructor");
void start() {
System.out.println("Vehicle starting...");
Car() {
System.out.println("Car constructor");
void drive() {
System.out.println("Car driving...");
myCar.start();
myCar.drive();
OUTPUT:
(3) Method Overriding
class Shape {
void draw() {
System.out.println("Drawing a shape");
void draw() {
System.out.println("Drawing a circle");
myCircle.draw();
OUTPUT:
(4) Inherited Variable
class Person {
String name;
Person(String name) {
this.name = name;
int studentId;
super(name);
this.studentId = studentId;
OUTPUT:
(5) Triangle Class
class Shape {
void draw() {
System.out.println("Drawing a shape");
void drawRectangle() {
System.out.println("Drawing a rectangle");
void drawCircle() {
System.out.println("Drawing a circle");
myRectangle.draw();
myRectangle.drawRectangle();
myCircle.draw();
myCircle.drawCircle();
OUTPUT: