1 C.K.PITHAWALA COLLEGE OF ENGINEERING & TECHNOLOGY, SURAT Branch :- computer 3nd Year/5rd SEM (Div. D) ALA Subject :- OOPJ Topic Name :- Inheritance and interfaces Group No :-B4 Enrolment No Name Submitted To 160090107051 160090107049 160090107025 160090107044 160090107059 Shubham Sharma Pakshal Shah Hariom Maurya Pravin Rathod Naitik Vajani Dr. Ami Choksi
Inheritance and interface
Contents  Introduction to inheritance  Types of Inheritance  Method overriding using inheritance  Super keyword  Final keyword  Interfaces
Introduction to inheritance  As the name suggests, inheritance means to take something that is already made.  Inheritance is a unique feature of object oriented programming.  Inheritance is mainly used for code reusability.  The class which acquires the properties of the class is called as sub-class and the class whose properties are acquired is called as superclass. Consider the example: As shown in example, the child inherit the properties of father. Hence child will be subclass while father is a superclass.
IS-A Relationship  Inheritance defines IS-A relationship between a super class and its subclass  For Example:  Car IS A vehicle  Bike IS A vehicle  EngineeringCollege IS A college  MedicalCollege IS A college  In java, inheritance is implemented with the help of keyword - “extends”.
Types of inheritance • Single inheritance • Multilevel inheritance • Hierarchical inheritance
Single Inheritance  A structure having one and only one super class as well as subclass.  Child class is authorized to access the property of Parent class. Super class Subclass Syntax : class A { …………. …………. } class B extends A { …………. …………. } Sharing of properties
Multilevel inheritance B C A  When a class is derived from a class which is also derived from a class, then such type of inheritance is called as multilevel inheritance. Syntax : class A { …………. …………. } class B extends A { …………. …………. } class C extends B { …………. …………. }
Hierarchical inheritance  In hierarchical inheritance, one class is inherited by many subclasses.  subclasses must be connected with only one super class. B D A C Syntax : class A { …………. …………. } class B extends A { …………. …………. } class C extends A { …………. …………. } class D extends A { …………. …………. }
Why multiple inheritance is not supported in java ?  To reduce the complexity and simply the language, java doesn’t support multiple inheritance.  Suppose there are 3 classes A,B and C. The C class inherits class A and B.  If A and B have same method and you call it from child class object, there will be ambiguity to call method of A or B class.  Since compile time errors are better than run time errors, java renders compile time error if you inherit 2 classes.
Access Specifiers in java  There are four Access Specifiers in Java  1. Public: When a member of a class is declared as public specifier, it can be accessed from any code.  2. Protected: Protected is only applicable in case of Inheritance. When a member of a class is declared as protected, it can only be accessed by the members of its class or subclass.  3. Private: A member of a class is declared as private specifier, can only be accessed by the member of its class.  4. Default: When you don't specify a access specifier to a member, Java automatically specifies a default. And the members modified by default can only be accessed by any other code in the package, but can't be accessed outside of a package.
A program demonstrating inheritance in Java 1. class circle{ 2. double radius; 3. void radius(double a) 4. { 5. radius=a; 6. } 7. double getdata() 8. { 9. double area; 10.area=3.14*radius*radius; 11.return area; 12.} 13.} 14.class cylinder extends circle{ 15.double h; 16.void height(double b,double r) 17.{ 18.radius=r; 19.h=b; 20.} 21.double getdata() 22.{ 23.double volume; 24.volume=3.14*radius*radius*h; 25.return volume; 26.}
A program demonstrating inheritance in Java 27.public static void main(String[] args) { 28.cylinder c1=new cylinder(); 29.c1.height(5.12,6.5); 30.double area,volume; 31.volume=c1.getdata(); 32.System.out.println("Volume of cylinder :"+volume); 33.circle ob=new circle(); 34.ob.radius(4.44); 35.ob.getdata(); 36.area=ob.getdata(); 37.System.out.println("area of circle:"+area); 38.} 39.} Output of program: Volume of cylinder :679.2447999999 Area of circle:61.90070400000001
Method overriding in Java Subclasses inherit all methods from their superclass Sometimes, the implementation of the method in the superclass does not provide the functionality required by the subclass. In these cases, the method must be overridden. To override a method, provide an implementation in the subclass. The method in the subclass MUST have the exact same signature as the method it is overriding.
Program demonstrating method overriding 1. class A{ 2. void display() 3. { 4. System.out.println("This is parent class."); 5. } 6. } 7. class B extends A{ 8. void display() 9. { 10.System.out.println("This is first child class"); 11.} 12.public static void main(String[] args) { 13.B b1=new B(); 14.b1.display(); 15.} 16.} Output: This is first child class
Super keyword  As the name suggest super is used to access the members of the super class.  It is used for two purposes in java. 1. The first use of keyword super is to access the data variables of the super class hidden by the sub class.  e.g. Suppose class A is the super class that has two instance variables as int a and float b.  class B is the subclass that also contains its own data members named a and b.  Then we can access the super class (class A) variables a and b inside the subclass class B just by calling the following command.  super.member;
Super keyword  2.Use of super to call super class constructor: The second use of the keyword super in java is to call super class constructor in the subclass.  This functionality can be achieved just by using the following command. super(param-list);  Here parameter list is the list of the parameter requires by the constructor in the super class.  super must be the first statement executed inside a super class constructor.  If we want to call the default constructor then we pass the empty parameter list.  If we dont use super then the compiler does this task implicitly to instantiate superclass members.
Program to demonstrate Super Keyword 1) class Animal{ 2) Animal(){System.out.println("animal is created");} 3) } 4) class Dog extends Animal{ 5) Dog(){ 6) super(); 7) System.out.println("dog is created"); 8) } 9) } 10)class TestSuper3{ 11)public static void main(String args[]){ 12)Dog d=new Dog(); 13)}} Output: Animal is created Dog is created
Final Keyword  The final keyword in java is used to restrict the user.  The final keyword can be used in mainly 3 context. 1. Variable: If you make any variable final, you cannot change the value of final variable. Example: 1. class bike{ 2. final int speedlimit=50; 3. void run(){ 4. speedlimit=60; //Compile-time error 5. } 6. public static void main(String[] args){ 7. Bike ob=new bike(); 8. ob.run(); 9. } 10. } Output: Compile time error
Final keyword 2. Method: If you make a method final, you cannot override it. Example: 1. class bike{ 2. final void run(){ 3. System.out.println(“Running”); 4. } 5. class honda extends run(){ 6. void run(){ //compile time error 7. System.out.println(“Running at 100 kmph”); 8. } 9. } 10. public static void main(String[] args){ 11. Bike ob=new bike(); 12. ob.run(); 13. } 14. } Output: Compile time error
Final keyword 3. Class : If you make any class final, you cannot extend it. Example: 1. final class bike{ 2. //code here 3. } 4. class honda extends run(){ //Compile time error 5. void run(){ 6. System.out.println(“Running at 100 kmph”); 7. } 8. public static void main(String[] args){ 9. honda ob=new honda(); 10. ob.run(); 11. } 12. } Output: Compile time error
Interface in Java  Java Supports a special feature called interface.  This feature helps to connect a class with more than one classes (in order to achieve multiple inheritance).  For this type of connectivity java uses ‘implements’ keyword.  A class can implements multiple interfaces at a same time  All the variables defined in the interface are final and cannot be changed in subclass. Syntax : interface A { int a=10; public int getdata(); public void display(); } interface B { public void getmarks(); } class D implements A,B { ……….. ……….. }
Comparison between interface and abstract class Features Interface Abstract class Multiple inheritance A class may implement multiple interfaces A class can extend only one abstract class Default implementation An interface cannot provide any code at all An abstract class can provide complete code, default code Constants Constants are by default Static and final Both static and instance constants are allowed Object An object of interface is never created An object of abstract class can be created
Program demonstrating interfaces 1. interface Printable{ 2. void print(); 3. } 4. interface Showable{ 5. void show(); 6. } 7. class A7 implements Printable,Showable{ 8. public void print(){System.out.println("Hello");} 9. public void show(){System.out.println("Welcome");} 10.public static void main(String args[]){ 11.A7 obj = new A7(); 12.obj.print(); 13.obj.show(); 14. } 15.} Output: Hello Welcome
End of presentation thank you

Inheritance and interface

  • 1.
    1 C.K.PITHAWALA COLLEGE OF ENGINEERING& TECHNOLOGY, SURAT Branch :- computer 3nd Year/5rd SEM (Div. D) ALA Subject :- OOPJ Topic Name :- Inheritance and interfaces Group No :-B4 Enrolment No Name Submitted To 160090107051 160090107049 160090107025 160090107044 160090107059 Shubham Sharma Pakshal Shah Hariom Maurya Pravin Rathod Naitik Vajani Dr. Ami Choksi
  • 2.
  • 3.
    Contents  Introduction toinheritance  Types of Inheritance  Method overriding using inheritance  Super keyword  Final keyword  Interfaces
  • 4.
    Introduction to inheritance As the name suggests, inheritance means to take something that is already made.  Inheritance is a unique feature of object oriented programming.  Inheritance is mainly used for code reusability.  The class which acquires the properties of the class is called as sub-class and the class whose properties are acquired is called as superclass. Consider the example: As shown in example, the child inherit the properties of father. Hence child will be subclass while father is a superclass.
  • 5.
    IS-A Relationship  Inheritancedefines IS-A relationship between a super class and its subclass  For Example:  Car IS A vehicle  Bike IS A vehicle  EngineeringCollege IS A college  MedicalCollege IS A college  In java, inheritance is implemented with the help of keyword - “extends”.
  • 6.
    Types of inheritance •Single inheritance • Multilevel inheritance • Hierarchical inheritance
  • 7.
    Single Inheritance  Astructure having one and only one super class as well as subclass.  Child class is authorized to access the property of Parent class. Super class Subclass Syntax : class A { …………. …………. } class B extends A { …………. …………. } Sharing of properties
  • 8.
    Multilevel inheritance B C A  Whena class is derived from a class which is also derived from a class, then such type of inheritance is called as multilevel inheritance. Syntax : class A { …………. …………. } class B extends A { …………. …………. } class C extends B { …………. …………. }
  • 9.
    Hierarchical inheritance  Inhierarchical inheritance, one class is inherited by many subclasses.  subclasses must be connected with only one super class. B D A C Syntax : class A { …………. …………. } class B extends A { …………. …………. } class C extends A { …………. …………. } class D extends A { …………. …………. }
  • 10.
    Why multiple inheritanceis not supported in java ?  To reduce the complexity and simply the language, java doesn’t support multiple inheritance.  Suppose there are 3 classes A,B and C. The C class inherits class A and B.  If A and B have same method and you call it from child class object, there will be ambiguity to call method of A or B class.  Since compile time errors are better than run time errors, java renders compile time error if you inherit 2 classes.
  • 11.
    Access Specifiers injava  There are four Access Specifiers in Java  1. Public: When a member of a class is declared as public specifier, it can be accessed from any code.  2. Protected: Protected is only applicable in case of Inheritance. When a member of a class is declared as protected, it can only be accessed by the members of its class or subclass.  3. Private: A member of a class is declared as private specifier, can only be accessed by the member of its class.  4. Default: When you don't specify a access specifier to a member, Java automatically specifies a default. And the members modified by default can only be accessed by any other code in the package, but can't be accessed outside of a package.
  • 12.
    A program demonstratinginheritance in Java 1. class circle{ 2. double radius; 3. void radius(double a) 4. { 5. radius=a; 6. } 7. double getdata() 8. { 9. double area; 10.area=3.14*radius*radius; 11.return area; 12.} 13.} 14.class cylinder extends circle{ 15.double h; 16.void height(double b,double r) 17.{ 18.radius=r; 19.h=b; 20.} 21.double getdata() 22.{ 23.double volume; 24.volume=3.14*radius*radius*h; 25.return volume; 26.}
  • 13.
    A program demonstratinginheritance in Java 27.public static void main(String[] args) { 28.cylinder c1=new cylinder(); 29.c1.height(5.12,6.5); 30.double area,volume; 31.volume=c1.getdata(); 32.System.out.println("Volume of cylinder :"+volume); 33.circle ob=new circle(); 34.ob.radius(4.44); 35.ob.getdata(); 36.area=ob.getdata(); 37.System.out.println("area of circle:"+area); 38.} 39.} Output of program: Volume of cylinder :679.2447999999 Area of circle:61.90070400000001
  • 14.
    Method overriding inJava Subclasses inherit all methods from their superclass Sometimes, the implementation of the method in the superclass does not provide the functionality required by the subclass. In these cases, the method must be overridden. To override a method, provide an implementation in the subclass. The method in the subclass MUST have the exact same signature as the method it is overriding.
  • 15.
    Program demonstrating methodoverriding 1. class A{ 2. void display() 3. { 4. System.out.println("This is parent class."); 5. } 6. } 7. class B extends A{ 8. void display() 9. { 10.System.out.println("This is first child class"); 11.} 12.public static void main(String[] args) { 13.B b1=new B(); 14.b1.display(); 15.} 16.} Output: This is first child class
  • 16.
    Super keyword  Asthe name suggest super is used to access the members of the super class.  It is used for two purposes in java. 1. The first use of keyword super is to access the data variables of the super class hidden by the sub class.  e.g. Suppose class A is the super class that has two instance variables as int a and float b.  class B is the subclass that also contains its own data members named a and b.  Then we can access the super class (class A) variables a and b inside the subclass class B just by calling the following command.  super.member;
  • 17.
    Super keyword  2.Useof super to call super class constructor: The second use of the keyword super in java is to call super class constructor in the subclass.  This functionality can be achieved just by using the following command. super(param-list);  Here parameter list is the list of the parameter requires by the constructor in the super class.  super must be the first statement executed inside a super class constructor.  If we want to call the default constructor then we pass the empty parameter list.  If we dont use super then the compiler does this task implicitly to instantiate superclass members.
  • 18.
    Program to demonstrateSuper Keyword 1) class Animal{ 2) Animal(){System.out.println("animal is created");} 3) } 4) class Dog extends Animal{ 5) Dog(){ 6) super(); 7) System.out.println("dog is created"); 8) } 9) } 10)class TestSuper3{ 11)public static void main(String args[]){ 12)Dog d=new Dog(); 13)}} Output: Animal is created Dog is created
  • 19.
    Final Keyword  Thefinal keyword in java is used to restrict the user.  The final keyword can be used in mainly 3 context. 1. Variable: If you make any variable final, you cannot change the value of final variable. Example: 1. class bike{ 2. final int speedlimit=50; 3. void run(){ 4. speedlimit=60; //Compile-time error 5. } 6. public static void main(String[] args){ 7. Bike ob=new bike(); 8. ob.run(); 9. } 10. } Output: Compile time error
  • 20.
    Final keyword 2. Method:If you make a method final, you cannot override it. Example: 1. class bike{ 2. final void run(){ 3. System.out.println(“Running”); 4. } 5. class honda extends run(){ 6. void run(){ //compile time error 7. System.out.println(“Running at 100 kmph”); 8. } 9. } 10. public static void main(String[] args){ 11. Bike ob=new bike(); 12. ob.run(); 13. } 14. } Output: Compile time error
  • 21.
    Final keyword 3. Class: If you make any class final, you cannot extend it. Example: 1. final class bike{ 2. //code here 3. } 4. class honda extends run(){ //Compile time error 5. void run(){ 6. System.out.println(“Running at 100 kmph”); 7. } 8. public static void main(String[] args){ 9. honda ob=new honda(); 10. ob.run(); 11. } 12. } Output: Compile time error
  • 22.
    Interface in Java Java Supports a special feature called interface.  This feature helps to connect a class with more than one classes (in order to achieve multiple inheritance).  For this type of connectivity java uses ‘implements’ keyword.  A class can implements multiple interfaces at a same time  All the variables defined in the interface are final and cannot be changed in subclass. Syntax : interface A { int a=10; public int getdata(); public void display(); } interface B { public void getmarks(); } class D implements A,B { ……….. ……….. }
  • 23.
    Comparison between interfaceand abstract class Features Interface Abstract class Multiple inheritance A class may implement multiple interfaces A class can extend only one abstract class Default implementation An interface cannot provide any code at all An abstract class can provide complete code, default code Constants Constants are by default Static and final Both static and instance constants are allowed Object An object of interface is never created An object of abstract class can be created
  • 24.
    Program demonstrating interfaces 1.interface Printable{ 2. void print(); 3. } 4. interface Showable{ 5. void show(); 6. } 7. class A7 implements Printable,Showable{ 8. public void print(){System.out.println("Hello");} 9. public void show(){System.out.println("Welcome");} 10.public static void main(String args[]){ 11.A7 obj = new A7(); 12.obj.print(); 13.obj.show(); 14. } 15.} Output: Hello Welcome
  • 25.