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 class circle{ double radius; void radius(double a) { radius=a; } double getdata() { double area; area=3.14*radius*radius; return area; } } class cylinder extends circle{ double h; void height(double b,double r) { radius=r; h=b; } double getdata() { double volume; volume=3.14*radius*radius*h; return volume; }
A program demonstrating inheritance in Java public static void main(String[] args) { cylinder c1=new cylinder(); c1.height(5.12,6.5); double area,volume; volume=c1.getdata(); System.out.println("Volume of cylinder :"+volume); circle ob=new circle(); ob.radius(4.44); ob.getdata(); area=ob.getdata(); System.out.println("area of circle:"+area); } } 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 class A{ void display() { System.out.println("This is parent class."); } } class B extends A{ void display() { System.out.println("This is first child class"); } public static void main(String[] args) { B b1=new B(); b1.display(); } } 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 class Animal{ Animal(){System.out.println("animal is created");} } class Dog extends Animal{ Dog(){ super(); System.out.println("dog is created"); } } class TestSuper3{ public static void main(String args[]){ 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: class bike{ final int speedlimit=50; void run(){ speedlimit=60; //Compile- time error 5. } public static void main(String[] args){ Bike ob=new bike(); ob.run(); } } Output: Compile time error
Final keyword 2. Method: If you make a method final, you cannot override it. Example: class bike{ final void run(){ System.out.println(“Running”); 4. } class honda extends run(){ void run(){ //compile time error System.out.println(“Running at 100 kmph”); } } public static void main(String[] args){ Bike ob=new bike(); ob.run(); } } Output: Compile time error
Final keyword Class : If you make any class final, you cannot extend it. Example: final class bike{ //code here } class honda extends run(){ //Compile time error void run(){ System.out.println(“Running at 100 kmph”); } public static void main(String[] args){ honda ob=new honda(); ob.run(); } } 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 interface Printable{ void print(); } interface Showable{ void show(); } class A7 implements Printable,Showable{ public void print(){System.out.println("Hello");} public void show(){System.out.println("Welcome");} public static void main(String args[]){ A7 obj = new A7(); obj.print(); obj.show(); 14. } 15. } Output: Hello Welcome