Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Chapter Three Inheritance-1

Chapter Three discusses inheritance in Java, explaining how a new class can be created from an existing class, known as a superclass or parent class, and how the derived class is referred to as a subclass or child class. It covers types of inheritance, implementation using the 'extends' keyword, and how to access superclass members using the 'super' keyword. Additionally, it addresses inheritance hiding, method hiding, and the use of the final keyword in relation to inheritance.

Uploaded by

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

Chapter Three Inheritance-1

Chapter Three discusses inheritance in Java, explaining how a new class can be created from an existing class, known as a superclass or parent class, and how the derived class is referred to as a subclass or child class. It covers types of inheritance, implementation using the 'extends' keyword, and how to access superclass members using the 'super' keyword. Additionally, it addresses inheritance hiding, method hiding, and the use of the final keyword in relation to inheritance.

Uploaded by

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

Chapter Three

Inheritance
Outline

Introduction
Super-classes and Sub-classes
Inheritable members
Using super
Inheritance hiding
Use of the final keyword with inheritance
Constructors in sub-classes
Introduction to
inheritance
Inheritance

 Create a new class from an existing class.


 Derive something specific from something generic.
 A class can inherit the features of another class and add its
own modification
 The existing class is called the parent class, or superclass, or
base class
 The derived class is called the child class or subclass 4
Inheritance
 Child inherits non-private
fields and method
 Provide code re-usability
 Creates is-a relationship,
meaning the child is a more
specific version of the parent

5
Type of Inheritance

6
Type of Inheritance
 Single – single subclass extends from a single superclass
 Multiple – single subclass extends from multiple super
classes. Impossible in java
 Multilevel – subclass extends from a superclass and then
the same subclass acts as a superclass for another class
 Hierarchical – multiple subclasses extend from a single
superclass
7
How to implement inheritance?

 The extends keyword is used to perform inheritance in Java.


 Here class Car is child class and class Vehicle is parent class.
 The class Car is inheriting the properties and methods of
Vehicle class.

class Car extends Vehicle {

8
How to implement inheritance?

 The extends keyword is used to perform inheritance in Java.


 The class Car is inheriting the properties and methods of
Vehicle class.

9
What will be inherited?

 Protected and public members from the superclass class.


 Members with default modifier can be inherited, if the two
classes are in the same package.
 To access inherited members, we can simply use them directly
 We don't need super class reference to access its members

10
How to call superclass members?

 By using super keyword


 Super keyword
 invokes fields, constructor, and method of superclass
 To invoke constructor use like method – super( )

11
How to call superclass constructor?

 Child class does not inherit super class constructors.


 However, must call a superclass constructor in the inside of its
constructors!
 If a subclass calls another constructor within itself, then the
called constructor must call the superclass constructor.

12
How to call superclass constructor?

 Invocation of a superclass constructor must be the first line in


the subclass constructor.
 The syntax for calling a superclass constructor is
 super(); the superclass no-argument constructor is called. or

 super(parameter list); the superclass constructor with a


matching parameter list is called.

13
Java Inheritance
Hiding (Shadowing)

• Variable hiding

• when both the child and the parent classes have a


variable with the same name, the child's variable
hides the one from the parent.
Example

public class Parent { public class Child extends Parent{


String name = “abebe”; String name = “kebede”;
public String getParentName(){ public String getChildName(){
return name; return name;
} }
} }
Example
public class Test {
public static void main(String [] args){
Parent p = new Parent();
Parent c = new Child();

System.out.println( p.getParentName());
System.out.println( c.getChildName());
}
}
Output:
 Abebe
Method hiding

• Works with static ones


• When a child class defines a static method with the
same signature as a static method in the parent class,
then the child's method hides the one in the parent
class.
• If method instance it is called method overriding
Example

public class Parent { public class Child extends Parent{


public static String name(){ public static String name(){
return “Parent”; return “Child”;
} }
} }
Example

public class Test {


public static void main(String [] args){
System.out.println( Child.name());
}
}

Output: Child
Final and abstract
keyword
ASAMINEW GIZAW
+2519623251

Thank you [email protected]


u.et

You might also like