23 June 2023

What is Inheritance In Java - learngreen.net

   In Java, inheritance is an essential concept that lets classes inherit residences and behaviors from different classes. It helps create a hierarchy of classes, the place a subclass can inherit matters from its superclass.


The superclass is the type we inherit from, and the subclass is the category that inherits from the superclass. The subclass can use the fields and techniques of the superclass, and additionally add its personal special ones.


To set up inheritance, we use the "extends" key-word in the category declaration.


The subclass can get right of entry to the superclass's stuff with the aid of the usage of the dot operator. It can additionally override techniques of the superclass to supply its personal implementation. This lets in us to personalize and specialize the conduct of our classes.


Inheritance helps us reuse code due to the fact we can outline frequent attributes and behaviors in the superclass and inherit them in more than one subclasses. It additionally allows polymorphism, which skill we can deal with objects of the subclass as objects of the superclass. This offers us flexibility and makes our code greater adaptable.


It's essential to be aware that Java helps single inheritance, which potential a category can solely inherit from one superclass. To work round this limitation, Java gives interfaces, which permit lessons to put in force a couple of interfaces to inherit conduct from extraordinary sources.


In summary, inheritance in Java lets us create classification hierarchies, reuse code, and make our code extra flexible. It's a effective device that helps us construct higher and extra geared up programs.

What is Inheritance In Java?





//Example 1-> Inheritance using Nested Class
 package Interview;
 public class MyClass {
 //create superclass with name "MyClass"
 public void hobby() {
 System.out.println("Playing Cricket");
	}
 public class NewClass extends MyClass {
 //Create Nested Class with name "NewClass"
 public void color() {
 System.out.println("The color is Red");
		}
	}
 public static void main (String[] args) {	
 MyClass obj = new MyClass();
 //Create object of superclass MyClass
 
 NewClass obj1 = obj.new NewClass();
 /*Create object of nested class
  * using object of superclass
  */
 obj.hobby();
 //Call the hobby method from the superclass
 obj1.color();
 //Call the color method from the Nested Class
	 }
  }
 
/*
 * output->
 * Playing cricket
 * The color is Red
 */

 


//Example 2-> Inheritance Method override

package Interview;
 public class MyClass {
 //Create superclass with name "MyClass"
 public void draw() {
 //Create draw() method
 System.out.println("Drawing a shape.");
	   }
 public class Circle extends MyClass {
 //Create Nested class Circle
 public void draw() {
 //Method Override
 System.out.println("Drawing a circle.");
	   }
 }
 public static void main (String[] args) {	
 MyClass obj = new MyClass();
 //Create object of MyClass
 Circle obj1 = obj.new Circle();
 /*
  * Create object of Nested Class
  * Circle using object of superclass
  * MyClass
  */
 obj.draw();
 // Call the draw method from MyClass
 obj1.draw();
 //Method Override for Nested Class
   }
}
 /*
  * Output->
  * Drawing a shape
  * Drawing a circle
 */

 



//Example 3->
// Inheritance with constructor

 package Interview;
 public class MyClass {
 //create superclass Name "MyClass"
 private String brand;
 //create String variable brand
 
 public MyClass(String brand) {
 this.brand = brand;
 //create constructor
 }
 public void displayBrand() {
	 System.out.println(brand);
 //create method displayBrand	 
 }
 
 public static class jeans extends MyClass {
 //create inner class jeans which inherit
//superclass
 public jeans(String brand) {
 super(brand);
 //constructor jeans inherit constructor from
 //superclass "MyClass"
	 }
 }
 public static void main (String[] args) {
 jeans obj = new jeans("Levis");
 //create object of jeans inner class
 obj.displayBrand();
 //called displayBrand method
	}
 }

 //Output-> Levis

 



No comments:

Post a Comment