Skip to main content

Inheritance | types of inheritance | inheritance in python,


 Inheritance 

Inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such a way, you can reuse, extend or modify the attributes and behaviors which are defined in other classes. 

In Java, the class which inherits the members of another class is called derived class and the class whose members are inherited is called base class. The derived class is the specialized class for the base class. 

Types of Inheritance : 

1. Single inheritance : When one class inherits another class, it is known as single level inheritance 

class Shape {

   public void area() {

       System.out.println("Displays Area of Shape");

   }

}

class Triangle extends Shape {

   public void area(int h, int b) {

       System.out.println((1/2)*b*h);

   }  

}


2. Hierarchical inheritance : Hierarchical inheritance is defined as the process of deriving more than one class from a base class. 

class Shape {

   public void area() {

       System.out.println("Displays Area of Shape");

   }

}

class Triangle extends Shape {

   public void area(int h, int b) {

       System.out.println((1/2)*b*h);

   }  

}

class Circle extends Shape {

   public void area(int r) {

       System.out.println((3.14)*r*r);

   }  

}


3. Multilevel inheritance : Multilevel inheritance is a process of deriving a class from another derived class. 

class Shape {

   public void area() {

       System.out.println("Displays Area of Shape");

   }

}

class Triangle extends Shape {

   public void area(int h, int b) {

       System.out.println((1/2)*b*h);

   }  

}

class EquilateralTriangle extends Triangle {

   int side;

}


4. Hybrid inheritance : Hybrid inheritance is a combination of 

simple, multiple inheritance and hierarchical inheritance. 






𝐀𝐦𝐚𝐳𝐨𝐧 𝐥𝐢𝐧𝐤🛍️:-

Comments

Popular posts from this blog

Conditional Statements | if, If-else, "if" and "if else", Switch Break | Complete Java Placement Course | Lecture 3

                                    Java - Introduction to Programming Lecture 3 Conditional Statements Conditional statements are a fundamental concept in programming. They are used to make decisions based on certain conditions, allowing the program to perform different actions depending on the input or data it receives. In this article, we'll take a closer look at conditional statements in programming and how they can be used effectively. What are conditional statements? Conditional statements, also known as "if-then" statements, are used in programming to execute certain code if a condition is met. In other words, the program will perform a particular action if a particular condition is true. For example, a program may check if a user has ent...

Recursion in One Shot | Theory + Question Practice + Code | Level 1 - Easy

JAVA Recursion Class 1 (Codes) Q1. Print numbers from 5 to 1. public static void printNumbers(int n) { if(n == 0) { return; } System.out.println(n); printNumbers(n-1); } Q2. Print numbers from 1 to 5. public static void printNumbers(int n) { if(n == 6) { return; } System.out.println(n); printNumbers(n+1); } Q3. Print the sum of first n natural numbers. class Recursion1 { public static void printSum(int n, int sum) { if(n == 0) { System.out.println(sum); return; } sum += n; printSum(n-1, sum); } public static void main(String args[]) { printSum(5, 0); } } Q4. Print factorial of a number n. class Recursion1 { public static void printFactorial(int n, int fact) { if(n == 0) { System.out.println(fact); return; } fact *= n; printFactorial(n-1, fact); } public static void main(String args[]) { printFactorial(5, 1); } } Q5. Print the fibonacci sequence till nth term. class Recursion1 { public static void printFactorial(int a, int b, int n) { if(n == 0) { return; } System.out.println(a); printF...

Java - Introduction to Programming| OOPS concept

  Java - Introduction to Programming Lecture 1 Installation & First Program Install Java a. Install JDK ( https://www.oracle.com/in/java/technologies/javase-downloads.html ) b. Install IntelliJ ( https://www.jetbrains.com/idea/download/#section=mac ) OR b. Install Visual Studio Code (VS Code) - Prefer THIS ( https://code.visualstudio.com/download ) Sample Code Functions A function is a block of code which takes some input, performs some operations and returns some output.  The functions stored inside classes are called methods. The function we have used is called main. Class A class is a group of objects which have common properties. A class can have some properties and functions (called methods). The class we have used is Main. Our 1st Program public class Main {     public static void main (String[] args) {    // Our 1st Program         System. out .println( "Hello World" ) ;     } }   ...