0% found this document useful (0 votes)
70 views21 pages

Inheritance

Inheritance allows classes to establish a "is-a" relationship where a subclass inherits attributes and behaviors from its parent superclass. This enables code reuse through common functionality defined in the superclass being applied to subclasses, while subclasses can specialize or override inherited methods. Constructors must call the parent constructor through the super keyword.

Uploaded by

Vaibhav Saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views21 pages

Inheritance

Inheritance allows classes to establish a "is-a" relationship where a subclass inherits attributes and behaviors from its parent superclass. This enables code reuse through common functionality defined in the superclass being applied to subclasses, while subclasses can specialize or override inherited methods. Constructors must call the parent constructor through the super keyword.

Uploaded by

Vaibhav Saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Inheritance

A d a pt e d f r o m
w w w. c s . u t e p . e d u / v l a d i k / c s 24 01.10 a / C h _ 11 _ I n h e r i t a n c e _ Po l y m o r p h i
s m . p pt

1
Review: Classes
User-defined data types
◦ Defined using the “class” keyword
◦ Each class has associated
◦ Data members (any object type)
◦ Methods that operate on the data
New instances of the class are declared using the “new” keyword
“Static” members/methods have only one copy for a class, regardless of
how many instances are created
Example: Shared Functionality
public class Student { public class Professor {
String name; String name;
char gender; char gender;
Date birthday; Date birthday;
Vector<Grade> grades; Vector<Paper> papers;

double getGPA() { int getCiteCount() {


… …
} }

int getAge(Date today) { int getAge(Date today) {


… …
} }
} }
public class Person {
String name;
char gender;
Date birthday;

int getAge(Date today) {



}
}

public class Student public class Professor


extends Person { extends Person {

Vector<Grade> grades; Vector<Paper> papers;

double getGPA() { int getCiteCount() {


… …
} }
} }
Inheritance
“is-a” relationship Base Class
A
Single inheritance:
◦ Subclass / Derived class is derived from one existing class Derived Class
(superclass / Base class) B

Multi-Level inheritance: Base Class


A
Class B inherits class A. Class C inherits class B
Derived Class
B

Derived Class
C
Inheritance
Multiple inheritance:
◦ Subclass is derived from more than one superclass
◦ Not supported by Java

Base Class Base Class


A B

Not supported
Derived
Class
Inheritance (continued)

Access modifier(s) class ClassName extends InheritClassName


{
memberList
}
Inheritance:
class Circle Derived from class
Shape

public class Circle extends Shape


{
.
.
.
}

8
Inheritance
Why is this useful in programming?
◦ Allow us to specify relationships between types : “is-a” relationship

◦ Allows for code reuse: General functionality can be written once and
applied to any subclass. Subclasses can specialize by adding members
and methods, or overriding functions

◦ More intuitive/expressive code


Inheritance: Adding Functionality
Subclasses have all (except private) of the data members and methods of the
superclass
Subclasses can add to the superclass
◦ Additional data members
◦ Additional methods
Subclasses are more specific and have more functionality
Superclasses capture generic functionality common across many types of
objects
public class Person {
String name;
char gender;
Date birthday;

int getAge(Date today) {



}
}

public class Student public class Professor


extends Person { extends Person {

Vector<Grade> grades; Vector<Paper> papers;

double getGPA() { int getCiteCount() {


… …
} }
} }
UML Diagram: Rectangle

What if we want to implement a 3d box object?

Remember: - Private Member


+ Public Member
Objects myRectangle and myBox
Rectangle myRectangle = new Rectangle(5, 3);
Box myBox = new Box(6, 5, 4);
UML Class Diagram: class Box

Both a Rectangle and a Box have a surface area,


but they are computed differently
Note: Private data members of Rectangle cannot Note: Both Box and Rectangle classes have
be directly accessed by Box objects. They can common methods names.
only be accessed by public methods. E.g. length Different signature (setDimension)
can be accessed by getLength() Same signature (area)
Overriding Methods (When subclass and
super class have same functions (including
signatures)
A subclass can override (redefine) the methods of the superclass

◦ Objects of the subclass type will use the new method


◦ Objects of the superclass type will use the original
class Rectangle
public double area()
{
return getLength() * getWidth();
}

class Box
public double area()
{ Note: Length and Width are
return 2 * (getLength() * getWidth() private data members of
Rectangle and hence cannot
+ getLength() * height be accessed directly in Box
+ getWidth() * height);
}
final Methods
Can declare a method of a class final using the keyword final

public final void doSomeThing()


{
//...
}

If a method of a class is declared final, it cannot be overridden


with a new definition in a derived class

17
Calling methods of the superclass
◦ If subclass overrides public method of superclass, call to public
method of superclass is made as follows:
super.MethodName(parameter list)

◦ If subclass does not override public method of superclass, call to


public method of superclass is made as follows:
MethodName(parameter list)

18
class Box
public void setDimension(double l, double w, double h)
{
super.setDimension(l, w);
if (h >= 0)
height = h;
else
height = 0;
}

Box overloads the method setDimension


(Different parameters)

19
Defining Constructors of the Subclass
Call to constructor of superclass:
 Must be first statement
 Specified by super parameter list
 in a class hierarchy, constructors are called in order of derivation,
from superclass to subclass
public Box() public Box(double l, double w,
double h)
{
{
super();
super(l, w);
height = 0;
height = h;
}
}

20
Assignment
 Read about toString method
https://www.javatpoint.com/understanding-toString()-method
https://www.geeksforgeeks.org/object-tostring-method-in-java/

21

You might also like