Inheritance

11,047 views
10,664 views

Published on

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

No Downloads
Views
Total views
11,047
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
37
Embeds 0
No embeds

No notes for slide

Inheritance

  1. 1. INHERITANCE IN JAVA INHERITANCE PRESENTED BY :- SAPNA SHARMA1
  2. 2. CONTENT Introduction to Inheritance Types of inheritance INHERITANCE Define sub class Single Inheritance Super keyword Multilevel Inheritance Method Overriding Abstract class Final keyword 2
  3. 3. INTRODUCTION TO INHERITANCE Reusability is yet another factor of OOP’s. Java classes are used in several ways. This is basically INHERITANCE by creating new classes ,reusing the properties existing one. The deriving the new class from an old one is called inheritance. The old class is known as base class or super class. The class who inherit the properties of the base class is called derived class or sub class. 3
  4. 4. TYPES OF INHERITANCE Single inheritance(one super class) INHERITANCE Multiple inheritance( several super classes) Multilevel inheritance( Hierarchical inheritance(one super class many sub class) 4
  5. 5. DEFINE SUBCLASSclass sub-classname extends super-classname{ INHERITANCE variable declaration;// sub class variables method declaration;// sub class method } The keyword extends signifies that the properties of the super class are extended to the sub class. 5
  6. 6. EXAMPLE OF SINGLE INHERITANCEclass sum // Super class{ int a=10; INHERITANCE int b=20; void show1() { System.out.println("value of a :- " +a); System.out.println("value of b :- " +b); }}class sum2 extends sum // base class{ public static void main(String args[]) { sum2 obj = new sum2(); obj.show1();} 6}
  7. 7. SUB CLASS CONSTRUCTOR A subclass constructor is used to construct the INHERITANCE instance variable of both the subclass and super class. The subclass constructor uses the keyword super to invoke the constructor method of the super class. 7
  8. 8. CONDITION FOR SUPER KEYWORD Super may only be used within a subclass constructor. INHERITANCE The call to super class constructor must appear as the first statement with in the subclass constructor. The parameter in the super call must match the order and type of the instance variable declared in the super class. 8
  9. 9. USES OF SUPER KEYWORD It calls the super class constructor. INHERITANCESYNTAX:- super( parameter list); Access the member of the super class.SYNTAX:- super. member variable; 9
  10. 10. USING SUPER TO CALL SUPER CLASS CONSTRUCTORclass demo{ int x,y; public demo(){ x=10; INHERITANCE y=20;}public demo(int i,int j){ x=i; y=j; }}class demo1 extends demo{int j;public demo1(){super(10,15);j=30;}void show(){System.out.println(x+ " " +y + " " +j);}public static void main(String args[]){ 10demo1 d = new demo1();d.show();}}
  11. 11. MULTILEVEL INHERITANCEWhen a subclass derived from a super class and INHERITANCEa subclass is further derived from that subclass orwhen a subclass acts as a super class for someother subclass, it creates a multilevel hierarchy. 11
  12. 12. METHOD OVERRIDING In a class hierarchy , when a method in a sub class has the same name and type signature as a INHERITANCE method in its super class, then the method is said to override the method in the super class. When an overridden method is called from within a sub class, it will always refer to the version of that method defined by the subclass. The version of the method defined by the super class will be hidden. 12
  13. 13. EXAMPLE OF METHOD OVERRIDDINGclass sum // Super class{ int a=10; INHERITANCE int b=20; void show() { System.out.println("value of a :- " +a); System.out.println("value of b :- " +b); }}class sum2 extends sum // base class{ int i=30,j=40; void show() { System.out.println(i+ " " +j);// only this value will be print } public static void main(String args[]) { sum2 obj = new sum2(); 13 obj.show();}}
  14. 14. ABSTRACT CLASS AND METHODS An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be sub INHERITANCE classed. SYNTAX:- Abstract class name { } An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: SYNTAX:- abstract void method(Parameter); 14
  15. 15. EXAMPLE OF ABSTRACT CLASSabstract class sum{abstract void show();abstract void get();}class sum1 extends sum INHERITANCE{ void show() { System.out.println("HELLO"); } void get() { System.out.println("WELCOME"); } public static void main(String args[]) { sum1 obj = new sum1(); obj.show(); obj.get(); 15}}
  16. 16. FINAL KEYWORDFinal keyword is used as follows:- INHERITANCE1. To prevent Overriding .2. To prevent inheritance3. To create a named constant. 16
  17. 17. INHERITANCE THANK YOU17

×