Inheritence

1,015 views

Published on

Published in: Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,015
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
79
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Inheritence

  1. 1. INHERITANCE • The derivation if one class from another class is called as INHERITANCE. • You can inherit a class using the keyword extends.
  2. 2. IMPORTANT POINTS TO REMEMBER • One derived class can extend only one base class. • If we do not want to give some features of base class through derived class then such features should be made private. • Constructor of one class never inherited to another class • For each and every class in Java their exists an implicit predefined super class known as java.lang.object.
  3. 3. Types of Inheritance Hierarchical Inheritance Multiple Inheritance Hybrid Inheritance Single Inheritance Multilevel Inheritance
  4. 4. SINGLE INHERITANCE • One subclass is derived from only one super-class. • Example:- class Furniture { int height=10, width=20; public void disp_furniture() { System.out.println("Height:-"+height+" Width:-"+width); } } class Chair extends Furniture { int lenght=30; public void disp_chair() { System.out.println("Length:-"+lenght); } }
  5. 5. public class FurnitureTest { public static void main(String args[]) { Furniture f=new Furniture(); Chair c=new Chair(); System.out.println("Contents of superclass"); f.disp_furniture(); System.out.println("Contents of derived class"); c.disp_furniture(); c.disp_chair(); } } • OUTPUT:- Contents of superclass Height:-10 Width:-20 Contents of derived class Height:-10 Width:-20 Length:-30
  6. 6. MULTILEVEL INHERITENCE • A subclass is derived from another derived class or subclass.
  7. 7. • Example:- class Marks { int sub1=30, sub2=40,sub3=45; void disp_marks() { System.out.println(sub1+" "+sub2+" "+sub3); } } class Average extends Marks { int total; float avg; void disp_avg() { total=sub1+sub2+sub3; avg=total/3; System.out.println("Average:- "+avg); } }
  8. 8. class Percentage extends Average { double per; void disp_per() { per=total/150; per=per*100; System.out.println("Percentage:- "+per); } } public class Furniture { public static void main(String args[]) { Percentage p=new Percentage(); p.disp_marks(); p.disp_avg(); p.disp_per(); } } OUTPUT:- 30 40 45 Average:- 38.0 Percentage:- 76.66
  9. 9. HIERARCHICAL INHERITENCE • Many classes are derived from a single superclass. • One class can be parent of many other classes.
  10. 10. WHY MULTIPLE INHERITENCE IS NOT SUPPORTED IN JAVA? • In multiple inheritance properties and behaviour of multiple classes are inherited into a single class. • Simplicity can be a reason for not including multiple inheritance in Java • Or logically Diamond problem can be a reason

×