You are on page 1of 10

Inheritance

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

Child , Sub , Derived Class In Java extends key word is used

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(); } }

CORE JAVA CLOSER LOOKS TO METHODS

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 }

CORE JAVA CLOSER LOOKS TO METHODS


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 2.Multilevel Inheritance: class A { A //code } class B extends A { //code B } class C extends B { //code C }

CORE JAVA CLOSER LOOKS TO METHODS

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:

In Java,we don t have multiple inheritance instead we have interface

CORE JAVA CLOSER LOOKS TO METHODS

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.

Example Of Single Inheritance


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

You might also like