Professional Documents
Culture Documents
In
In
INHERITANCE Inheritance is the creation of one class by extending another class so that instances of the new class automatically inherit the fields and methods of its parent class. Parent , Super, Base Class
class Furniture { int height; int width; int length; } class Table extends Furniture { int no-of-legs; void init() { height=10; width=20; length=30; no-of-legs=4; }
void display() { System.out.println( height= +hei ght); System.out.println( width= +width); System.out.println( length= +length); System.out.println( Number of Legs= +no-oflegs); } public static void main(String arr[]) { Table t1=new Table(); t1.init(); t1.display(); } }
Inheritance: Inheritance is a process, through which we can create a new class from an existing class. Types:-1.Single 2.Multilevel 3.Multiple 4.Hierarchical 1.Single Inheritance: A class A { //code } class B extends A B { //code }
Inheritance: Inheritance is a process, through which we can create a new class from an existing class.
Types:-1.Single 2.Multilevel 3.Multiple 4.Hierarchical 3.Multiple Inheritance:
Inheritance: Inheritance is a process, through which we can create a new class from an existing class.
Types:-1.Single 2.Multilevel 3.Multiple 4.Hierarchical 3.Multiple Inheritance: Not supported by Java.
class Rectangle { int l,b; Rectangle(int x,int y) { l=x; b=y; } } int area() { return(l*b); } }
class Rectangle1 extends Rectangle { int b; Rectangle1(intx,inty,intz) { super(x,y); h = z; } int volume() { return(l*b*h); } }
class Mainn { public static void main(String args[]) { Rectangle1 r = new Rectangle1(1,2,3); int area1 = r.area(); int volume1 = r.volume(); System.out.println("Area of Rectangle"+area1); System.out.println("Volume of Rectangle"+volume1); } } Output: Area of Rectangle:2 Volume of Rectangle: 6