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

Lab 9 - Inheritance

This document provides information about inheritance in object-oriented programming. It discusses how subclasses inherit properties and behaviors from parent classes using the extends keyword. The superclass and subclass relationship represents an "is-a" relationship. The super keyword can be used to invoke constructors from the same class. The lab tasks involve implementing classes that demonstrate inheritance and using the super and this keywords.

Uploaded by

CAPTAIN PRICE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab 9 - Inheritance

This document provides information about inheritance in object-oriented programming. It discusses how subclasses inherit properties and behaviors from parent classes using the extends keyword. The superclass and subclass relationship represents an "is-a" relationship. The super keyword can be used to invoke constructors from the same class. The lab tasks involve implementing classes that demonstrate inheritance and using the super and this keywords.

Uploaded by

CAPTAIN PRICE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSE 215L: Programming Language II Lab

Lab - 09: Inheritance


Summer-2023
Lab Instructor: Md. Abdullah Al Sayed

Inheritance: In Object-oriented programming (OOP), new classes can be defined from


the existing classes which are called inheritance. More simply, Inheritance is a
mechanism in which one object acquires all the properties and behaviors of a parent
object. It represents the IS-A relationship which is also known as a parent-child
relationship. Inheritance has two purposes: reuse existing code, and reduce code
duplication.

Superclasses and Subclasses: Inheritance enables us to define a general class (i.e.,


a superclass) and later extends it to more specialized classes (i.e., subclasses).
Generally, when common traits are found among two classes, define one as
super/parent/base class and the other as sub/child/extended/derived class. A child class
can inherit the properties of the parent class and also can add its own properties.

Using the extends keyword: The extends keyword has been used to relate the
subclass with the superclass. For example, let’s say we have two classes, named class
A and B, respectively, where class A is the superclass and B is the subclass. Then the
Java syntax will be,
public class B extends A

Using the super keyword (this vs. super): The keyword this refers to the object itself.
It can also be used inside a constructor to invoke another constructor of the same class.
For example, the following code shows the uses of this keyword.
public class Circle { ● this keyword is used to reference
private double radius; the hidden data field radius of the
object being constructed.
public Circle(double radius) {
this.radius = radius;
} ● this(1.0), in this statement, this
keyword is used to invoke
public Circle() { another constructor.
this(1.0);
}
………
……...
}

Lab Tasks:

1. Implement the following classes and test their methods.


2. Implement the following classes and test their methods.

You might also like