Code reuse is implemented through inheritance. All classes in Java are obtained by inheriting the Java.lang.Object class directly or indirectly. Inherited classes are called subclasses, and inherited classes are called parent classes. Subclasses cannot inherit member variables and methods in the parent class that have access rights private.
Subclasses can override the parent class's methods and name member variables with the same name as the parent class. However, Java does not support multiple inheritance, which is the ability of a class to derive from multiple classes of superclass.
classa{A () {}Private intx=10;//Private member variable of Class A (cannot be inherited) protected inty=20;//protected member variables of Class A (can be inherited) voidFun ()//Fun member method of Class A (can be inherited){System.out.println ("y+x=" + (y+x));//outputs two-digit and } }classBextendsA//Class B is a subclass of Class A{B () {}voidGun ()//member methods for subclass B{y=y+1;//adds 1 to the value of the member variable y inherited from the parent class .System.out.println ("y=" +y); }} Public classTest2 { Public Static voidMain (String args[])//Main function{b b=NewB (); B.gun (); B.fun (); B.gun (); }} Subclass B uses a fun method inherited from the parent class to manipulate the member variable x that is not inherited in the parent class and is allocated memory space ... Result of output: Y=21y+x=31y=22
Inherited Dark Horse programmer in Java