Unit III Inheritance, Interface and Package Mr. Prashant B. Patil SY AI&DS, JPR 2021-2022
3.1. Inheritance in Java  Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).  The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class.  Moreover, you can add new methods and fields in your current class also.  Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
3.1. Inheritance in Java Why use inheritance in java:  For Method Overriding (so runtime polymorphism can be achieved).  For Code Reusability.
3.1. Inheritance in Java Terms used in Inheritance:  Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.  Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.  Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.  Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.
3.1. Inheritance in Java The syntax of Java Inheritance: class Subclass-name extends Superclass-name { //methods and fields }  The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality.  In the terminology of Java, a class which is inherited is called a parent or superclass, and the new class is called child or subclass.
3.1. Inheritance in Java Java Inheritance Example: class Employee { float salary=40000; } class Programmer extends Employee { int bonus=10000; public static void main(String args[]) { Programmer p=new Programmer(); System.out.println("Programmer salary is:"+p.salary); System.out.println("Bonus of Programmer is:"+p.bonus); } }
3.1. Inheritance in Java – Types of Inheritance
3.1. Inheritance in Java – Types of Inheritance Note: Multiple inheritance is not supported in Java through class.
3.2. Inheritance in Java – 1. Single Inheritance  When a class inherits another class, it is known as a single inheritance.
3.2. Inheritance in Java – 1. Single Inheritance  In the example given below, Dog class inherits the Animal class, so there is the single inheritance. class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class TestInheritance { public static void main(String args[]) { Dog d=new Dog(); d.bark(); d.eat(); } }
3.2. Inheritance in Java – 2. Multilevel Inheritance  When there is a chain of inheritance, it is known as multilevel inheritance.
 As you can see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance. class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class BabyDog extends Dog { void weep() { System.out.println("weeping..."); } } class TestInheritance2 { public static void main(String args[]) { BabyDog d=new BabyDog(); d.weep(); d.bark(); d.eat(); } } 3.2. Inheritance in Java – 2. Multilevel Inheritance
3.2. Inheritance in Java – 3. Hierarchical Inheritance  When two or more classes inherits a single class, it is known as hierarchical inheritance.
3.2. Inheritance in Java – 3. Hierarchical Inheritance In the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance. class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class Cat extends Animal { void meow() { System.out.println("meowing..."); } } class TestInheritance3 { public static void main(String args[]) { Cat c=new Cat(); c.meow(); c.eat(); Dog d=new Dog(); d.bark(); //c.bark(); //C.T.Error } }
3.2. Why multiple inheritance is not supported in java?  To reduce the complexity and simplify the language, multiple inheritance is not supported in java.  Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes.  If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class. class A { void msg() { System.out.println("Hello"); } } class C extends A,B { public static void main(String args[]) { C obj=new C(); obj.msg(); //Now which msg() method would be invoked? } } class B { void msg() { System.out.println("Welcome"); } }
3.2. Parameterized Constructor in Base Class  We have already seen that, the parent class is known as Superclass while child class is known as subclass.  Now continuing with previous section, if the base class has parameterized constructor then it does not get invoked automatically by the creation of derived class object.  We have to call it using super keyword in derived class constructor.
class base { int a; base (int z) { a=z; System.out.println(a); } } 3.2. Parameterized Constructor in Base Class Class derived extends base { int b; derived (int x, int y) { super(x); b = y; System.out.println(b); } } class test { public static void main(String args[]) { derived d = new derived(10,20); } } Calls base constructor Calls only derived constructor
3.2. Method Overriding in Java If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.  In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding. Usage of Java Method Overriding:  Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.  Method overriding is used for runtime polymorphism
3.2. Method Overriding in Java Rules for Java Method Overriding: 1. The method must have the same name as in the parent class 2. The method must have the same parameter as in the parent class. 3. There must be an IS-A relationship (inheritance).
3.2. Method Overriding in Java class base { void display () { System.out.println(“Base Class”); } } Class derived extends base { void display() { super.display(); System.out.println(“Derived Class”); } } class test { public static void main(String args[]) { derived d = new derived(); d.display(); } } Calls to base method Calls derived method
3.2. Method Overriding Vs. Overloading in Java S.NO Method Overloading Method Overriding 1. Method overloading is a compile time polymorphism. Method overriding is a run time polymorphism. 2. It help to rise the readability of the program. While it is used to grant the specific implementation of the method which is already provided by its parent class or super class. 3. It is occur within the class. While it is performed in two classes with inheritance relationship. 4. Method overloading may or may not require inheritance. While method overriding always needs inheritance. 5. In this, methods must have same name and different signature. While in this, methods must have same name and same signature. 6. In method overloading, return type can or can not be be same, but we must have to change the parameter. While in this, return type must be same or co-variant.
3.2. Use of “super” keyword The super keyword in java is a reference variable that is used to refer parent class objects.
class base { int a; } Class derived extends base { int a; void display() { a=10; super.a=20; System.out.println(“a”); System.out.println(“super.a”); } } class test { public static void main(String args[]) { derived d = new derived(); d.display(); } } Calls to base class member 3.2. Use of “super” keyword
3.2. Runtime Polymorphism or Dynamic method dispatch Runtime Polymorphism in Java is achieved by Method overriding in which a child class overrides a method in its parent.  An overridden method is essentially hidden in the parent class, and is not invoked unless the child class uses the super keyword within the overriding method.  This method call resolution happens at runtime and is termed as Dynamic method dispatch mechanism.
3.2. Runtime Polymorphism or Dynamic method dispatch  When Parent class reference variable refers to Child class object, it is known as Upcasting.  In Java this can be done and is helpful in scenarios where multiple child classes extends one parent class.
3.2. Runtime Polymorphism or Dynamic method dispatch A show() B show() C show() D show() A a1 = new A(); a1.show(); a1 = new B(); a1.show(); a1 = new C(); a1.show(); a1 = new D(); a1.show(); Calls to show() of class A Calls to show() of class B Calls to show() of class C Calls to show() of class D
3.2. Java “final” keyword  In Java, the final keyword is used to denote constants.  It can be used with variables, methods, and classes.  Once any entity (variable, method or class) is declared final, it can be assigned only once.  That is, - the final variable cannot be reinitialized with another value - the final method cannot be overridden - the final class cannot be extended
3.2. Java “final” keyword 1. Java final Variable In Java, we cannot change the value of a final variable. For example: class test { public static void main(String[] args) { final int AGE = 32; AGE = 45; //Error System.out.println("Age: " + AGE); } } Error cannot assign a value to final variable AGE AGE = 45; ^
3.2. Java “final” keyword 2. Java final Method: In Java, the final method cannot be overridden by the child class. For example, class FinalDemo { public final void display() { System.out.println("This is a final method."); } } class test extends FinalDemo { public final void display() { System.out.println("The final method is overridden."); } public static void main(String[] args) { test obj = new test(); obj.display(); } } Error display() in Main cannot override display() in FinalDemo public final void display() { ^ overridden method is final
3.2. Java “final” keyword 3. Java final Class: In Java, the final class cannot be inherited by another class. For example, final class FinalClass { public void display() { System.out.println("This is a final method."); } } class test extends FinalClass { public void display() { System.out.println("The final method is overridden."); } public static void main(String[] args) { test obj = new test(); obj.display(); } } Error cannot inherit from final FinalClass class Main extends FinalClass {
3.2. Abstract Methods and Classes A class which is declared with the abstract keyword is known as an abstract class in Java.  Data abstraction is the process of hiding certain details and showing only essential information to the user.  An abstract class must be declared with an abstract keyword.  It can have abstract and non-abstract methods.  It cannot be Instantiate.  It can have constructors and static methods also.  It can have final methods which will force the subclass not to change the body of the method.
3.2. Abstract Methods and Classes
abstract class Bike { abstract void run(); } class Honda4 extends Bike { void run() { System.out.println("running safely"); } public static void main(String args[]) { Honda4 obj = new Honda4(); obj.run(); } } 3.2. Abstract Methods and Classes
3.2. Java “static” keyword  Static keyword can be used with class, variable, method and block.  Static members belong to the class instead of a specific instance, this means if you make a member static, you can access it without object. The static can be:  Variable (also known as a class variable) [Ex:static int a;]  Method (also known as a class method)  Block  Nested class
3.2. Java “static” keyword class SimpleStaticExample { static void myMethod() { System.out.println("myMethod"); } public static void main(String[] args) { /* You can see that we are calling this * method without creating any object. */ myMethod(); } } Static Method
3.2. Java “static” keyword class JavaExample { static int num; static String mystr; static { num = 97; mystr = "Static keyword in Java"; } public static void main(String args[]) { System.out.println("Value of num: "+num); System.out.println("Value of mystr: "+mystr); } } Static Variables & Block

Inheritance Interface and Packags in java programming.pptx

  • 1.
    Unit III Inheritance, Interfaceand Package Mr. Prashant B. Patil SY AI&DS, JPR 2021-2022
  • 2.
    3.1. Inheritance inJava  Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).  The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class.  Moreover, you can add new methods and fields in your current class also.  Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
  • 3.
    3.1. Inheritance inJava Why use inheritance in java:  For Method Overriding (so runtime polymorphism can be achieved).  For Code Reusability.
  • 4.
    3.1. Inheritance inJava Terms used in Inheritance:  Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.  Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.  Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.  Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.
  • 5.
    3.1. Inheritance inJava The syntax of Java Inheritance: class Subclass-name extends Superclass-name { //methods and fields }  The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality.  In the terminology of Java, a class which is inherited is called a parent or superclass, and the new class is called child or subclass.
  • 6.
    3.1. Inheritance inJava Java Inheritance Example: class Employee { float salary=40000; } class Programmer extends Employee { int bonus=10000; public static void main(String args[]) { Programmer p=new Programmer(); System.out.println("Programmer salary is:"+p.salary); System.out.println("Bonus of Programmer is:"+p.bonus); } }
  • 7.
    3.1. Inheritance inJava – Types of Inheritance
  • 8.
    3.1. Inheritance inJava – Types of Inheritance Note: Multiple inheritance is not supported in Java through class.
  • 9.
    3.2. Inheritance inJava – 1. Single Inheritance  When a class inherits another class, it is known as a single inheritance.
  • 10.
    3.2. Inheritance inJava – 1. Single Inheritance  In the example given below, Dog class inherits the Animal class, so there is the single inheritance. class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class TestInheritance { public static void main(String args[]) { Dog d=new Dog(); d.bark(); d.eat(); } }
  • 11.
    3.2. Inheritance inJava – 2. Multilevel Inheritance  When there is a chain of inheritance, it is known as multilevel inheritance.
  • 12.
     As youcan see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance. class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class BabyDog extends Dog { void weep() { System.out.println("weeping..."); } } class TestInheritance2 { public static void main(String args[]) { BabyDog d=new BabyDog(); d.weep(); d.bark(); d.eat(); } } 3.2. Inheritance in Java – 2. Multilevel Inheritance
  • 13.
    3.2. Inheritance inJava – 3. Hierarchical Inheritance  When two or more classes inherits a single class, it is known as hierarchical inheritance.
  • 14.
    3.2. Inheritance inJava – 3. Hierarchical Inheritance In the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance. class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class Cat extends Animal { void meow() { System.out.println("meowing..."); } } class TestInheritance3 { public static void main(String args[]) { Cat c=new Cat(); c.meow(); c.eat(); Dog d=new Dog(); d.bark(); //c.bark(); //C.T.Error } }
  • 15.
    3.2. Why multipleinheritance is not supported in java?  To reduce the complexity and simplify the language, multiple inheritance is not supported in java.  Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes.  If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class. class A { void msg() { System.out.println("Hello"); } } class C extends A,B { public static void main(String args[]) { C obj=new C(); obj.msg(); //Now which msg() method would be invoked? } } class B { void msg() { System.out.println("Welcome"); } }
  • 16.
    3.2. Parameterized Constructorin Base Class  We have already seen that, the parent class is known as Superclass while child class is known as subclass.  Now continuing with previous section, if the base class has parameterized constructor then it does not get invoked automatically by the creation of derived class object.  We have to call it using super keyword in derived class constructor.
  • 17.
    class base { int a; base(int z) { a=z; System.out.println(a); } } 3.2. Parameterized Constructor in Base Class Class derived extends base { int b; derived (int x, int y) { super(x); b = y; System.out.println(b); } } class test { public static void main(String args[]) { derived d = new derived(10,20); } } Calls base constructor Calls only derived constructor
  • 18.
    3.2. Method Overridingin Java If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.  In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding. Usage of Java Method Overriding:  Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.  Method overriding is used for runtime polymorphism
  • 19.
    3.2. Method Overridingin Java Rules for Java Method Overriding: 1. The method must have the same name as in the parent class 2. The method must have the same parameter as in the parent class. 3. There must be an IS-A relationship (inheritance).
  • 20.
    3.2. Method Overridingin Java class base { void display () { System.out.println(“Base Class”); } } Class derived extends base { void display() { super.display(); System.out.println(“Derived Class”); } } class test { public static void main(String args[]) { derived d = new derived(); d.display(); } } Calls to base method Calls derived method
  • 21.
    3.2. Method OverridingVs. Overloading in Java S.NO Method Overloading Method Overriding 1. Method overloading is a compile time polymorphism. Method overriding is a run time polymorphism. 2. It help to rise the readability of the program. While it is used to grant the specific implementation of the method which is already provided by its parent class or super class. 3. It is occur within the class. While it is performed in two classes with inheritance relationship. 4. Method overloading may or may not require inheritance. While method overriding always needs inheritance. 5. In this, methods must have same name and different signature. While in this, methods must have same name and same signature. 6. In method overloading, return type can or can not be be same, but we must have to change the parameter. While in this, return type must be same or co-variant.
  • 22.
    3.2. Use of“super” keyword The super keyword in java is a reference variable that is used to refer parent class objects.
  • 23.
    class base { int a; } Classderived extends base { int a; void display() { a=10; super.a=20; System.out.println(“a”); System.out.println(“super.a”); } } class test { public static void main(String args[]) { derived d = new derived(); d.display(); } } Calls to base class member 3.2. Use of “super” keyword
  • 24.
    3.2. Runtime Polymorphismor Dynamic method dispatch Runtime Polymorphism in Java is achieved by Method overriding in which a child class overrides a method in its parent.  An overridden method is essentially hidden in the parent class, and is not invoked unless the child class uses the super keyword within the overriding method.  This method call resolution happens at runtime and is termed as Dynamic method dispatch mechanism.
  • 25.
    3.2. Runtime Polymorphismor Dynamic method dispatch  When Parent class reference variable refers to Child class object, it is known as Upcasting.  In Java this can be done and is helpful in scenarios where multiple child classes extends one parent class.
  • 26.
    3.2. Runtime Polymorphismor Dynamic method dispatch A show() B show() C show() D show() A a1 = new A(); a1.show(); a1 = new B(); a1.show(); a1 = new C(); a1.show(); a1 = new D(); a1.show(); Calls to show() of class A Calls to show() of class B Calls to show() of class C Calls to show() of class D
  • 27.
    3.2. Java “final”keyword  In Java, the final keyword is used to denote constants.  It can be used with variables, methods, and classes.  Once any entity (variable, method or class) is declared final, it can be assigned only once.  That is, - the final variable cannot be reinitialized with another value - the final method cannot be overridden - the final class cannot be extended
  • 28.
    3.2. Java “final”keyword 1. Java final Variable In Java, we cannot change the value of a final variable. For example: class test { public static void main(String[] args) { final int AGE = 32; AGE = 45; //Error System.out.println("Age: " + AGE); } } Error cannot assign a value to final variable AGE AGE = 45; ^
  • 29.
    3.2. Java “final”keyword 2. Java final Method: In Java, the final method cannot be overridden by the child class. For example, class FinalDemo { public final void display() { System.out.println("This is a final method."); } } class test extends FinalDemo { public final void display() { System.out.println("The final method is overridden."); } public static void main(String[] args) { test obj = new test(); obj.display(); } } Error display() in Main cannot override display() in FinalDemo public final void display() { ^ overridden method is final
  • 30.
    3.2. Java “final”keyword 3. Java final Class: In Java, the final class cannot be inherited by another class. For example, final class FinalClass { public void display() { System.out.println("This is a final method."); } } class test extends FinalClass { public void display() { System.out.println("The final method is overridden."); } public static void main(String[] args) { test obj = new test(); obj.display(); } } Error cannot inherit from final FinalClass class Main extends FinalClass {
  • 31.
    3.2. Abstract Methodsand Classes A class which is declared with the abstract keyword is known as an abstract class in Java.  Data abstraction is the process of hiding certain details and showing only essential information to the user.  An abstract class must be declared with an abstract keyword.  It can have abstract and non-abstract methods.  It cannot be Instantiate.  It can have constructors and static methods also.  It can have final methods which will force the subclass not to change the body of the method.
  • 32.
  • 33.
    abstract class Bike { abstractvoid run(); } class Honda4 extends Bike { void run() { System.out.println("running safely"); } public static void main(String args[]) { Honda4 obj = new Honda4(); obj.run(); } } 3.2. Abstract Methods and Classes
  • 34.
    3.2. Java “static”keyword  Static keyword can be used with class, variable, method and block.  Static members belong to the class instead of a specific instance, this means if you make a member static, you can access it without object. The static can be:  Variable (also known as a class variable) [Ex:static int a;]  Method (also known as a class method)  Block  Nested class
  • 35.
    3.2. Java “static”keyword class SimpleStaticExample { static void myMethod() { System.out.println("myMethod"); } public static void main(String[] args) { /* You can see that we are calling this * method without creating any object. */ myMethod(); } } Static Method
  • 36.
    3.2. Java “static”keyword class JavaExample { static int num; static String mystr; static { num = 97; mystr = "Static keyword in Java"; } public static void main(String args[]) { System.out.println("Value of num: "+num); System.out.println("Value of mystr: "+mystr); } } Static Variables & Block