Inheritance is a mechanism in object-oriented programming where a new class derives features and properties from an existing class, promoting code reusability. In Java, the 'extends' keyword is used for inheritance, which includes various types such as single, multi-level, and hierarchical. An example activity involves creating a superclass 'arithmetic' with subclasses for addition, subtraction, and multiplication functionalities.
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 ratings0% found this document useful (0 votes)
2 views
Lecture 4
Inheritance is a mechanism in object-oriented programming where a new class derives features and properties from an existing class, promoting code reusability. In Java, the 'extends' keyword is used for inheritance, which includes various types such as single, multi-level, and hierarchical. An example activity involves creating a superclass 'arithmetic' with subclasses for addition, subtraction, and multiplication functionalities.
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/ 11
Inheritance
Presented By: Shereen Sikander
Definition • When we create a new class from an existing class in such a way that the new class accesses all the features(functions/methods) and properties(variables) of the existing class, then that mechanism is called inheritance Note: 1. In JAVA extends keyword is used to perform inheritance. 2. Inheritance gives an advantage of code reusability. 3. We can't access private members of a class through inheritance. 4. A sub- class can access features of super class so we should make an object of a sub- class. 5. Method overriding is possible via Inheritance. Types of Inheritance • 5 types • Single • Multiple • Multi-level • Hierarchial • Hybrid Single • The inheritance in which there is one Super class and one Sub- class is called Single Inheritance. Super class • Syntax: Class Super { /* Line of code */ } Sub class
Class Sub-class extends Super {
/* Line of code */ } Single inheritance Contd.. Single Inheritance Contd.. Single Inheriatnce Contd with Private access Modifiers… Multi-level • In multi-level Inheritance we have a single Super Super Class Class and multiple Sub Classes. • Syntax: Class Super { /* Sub Class 1 Line of code */ } Class Sub-class 1 extends Super { Sub Class 2 /* Line of code */} Class Sub-class 2 extends Sub-class 1 { Sub Class 3 /* Line of code */} Multi-Level Inheritance Contd.. Multi-Level Inheritance Contd.. Activity • Make a Super class named arithmetic and having Sub-classes named add, subtract, multiply and result. • ADD class should have a function which adds numbers given by user. • Subtract class should have a function which Subtracts numbers given by user. • Multiply class should have a function which multiply numbers given by user. • Result should display answers of Add, Subtract and Multiply.