Object Oriented Programming Concepts in Java

Object Oriented Programming Concepts in Java
Slide Note
Embed
Share

Interface, multiple inheritance, abstract methods, polymorphism, and implementation in Java. Exploring the relationship between abstract classes, concrete classes, interfaces, and the limitations of multiple inheritance in Java programming.

  • Java Programming
  • Object Oriented
  • Abstract Classes
  • Polymorphism
  • Interface

Uploaded on Apr 29, 2025 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Object Oriented Programming Concepts in JAVA Interface

  2. Agenda 2 interface Multiple Inheritance

  3. Review : Abstract 3 Abstract Methods No Implementation Sub-classes may implement abstract methods Abstract Classes Can not be instantiated (Usually) A class with one or more abstract methods A class which extends an abstract class Is abstract unless it implements all the abstract methods Concrete class Not abstract class

  4. Abstract Example 4

  5. Abstract Method 5 All subclasses have the method But we can not implement the method in super-class So why we define it in super-class? Polymorphism

  6. Interface 6 Sometimes we have an abstract class, with no concrete method interface : pure abstract class no implemented method

  7. Interface 7

  8. Interface 8 All the methods are implicitly abstract No need to abstract specifier All the methods are implicitly public No need to public specifier

  9. Interface Implementation 9 Some classes may inherit abstract classes Some classes may implement interfaces Is-a relation exists If a class implements an interface But does not implement all the methods What will happen? The class becomes an abstract class

  10. 10

  11. Multiple Inheritance in Java 11 A class can inherit one and only one class A class may implement zero or more interfaces

  12. 12

  13. A Question 13 Why multiple inheritance is not supported for classes? Why multiple inheritance is supported for interfaces?

  14. What About Name Collision? 14 The return types are incompatible for the inherited methods B.f(), A.f()

  15. 15

  16. Interface Extension 16 Interfaces may inherit other interfaces Code reuse Is-a relation

  17. Interface properties 17 No member variable Variables : implicitly final and static Usually interfaces declare no variable Only operations are declared No constructor Why?

  18. Example 18

  19. Interfaces Applications 19 Pure Abstract classes Describe the design The architect designs interfaces, the programmer implements them Only interfaces are delivered to code consumers More powerful inheritance and polymorphism

  20. Review of Interfaces Interface Abstract interface Abstractclass Abstractclass multipleinheritance If both parents of a child implement a method, which one does the child inherits? - Multiple inheritance confusion. Interfaces allow a child to be both of type A and B. interface Interface

  21. Review of Interfaces (contd.) Recall that Java has the Comparable interface defined as: interface Comparable { int compareTo(Object o); } Recall also that java has the java.util.Arrays class, which has a sort method that can sort any array whose contents are either primitive values or Comparable objects. Thus, to sort our list of Employee objects, all we need is to modify the Employee class to implement the Comparable interface. Notice that this will work even if the Employee class is extending another class or implementing another interface. This modification is shown in the next page.

  22. Review of Interfaces (contd.) abstract class Employee implements Comparable { protected String name; protected double payRate; public Employee(String empName, double empRate) { name = empName; payRate = empRate; } public String getName() {return name;} public void setPayRate(double newRate) { payRate = newRate; } abstract public double pay(); public int compareTo(Object o) { Employee e = (Employee) o; return name.compareTo( e.getName()); } } HourlyEmployee Comparable Employee Executive MonthlyEmployee

  23. Review of Interfaces (contd.) Since Employee class implements the Comparable interface, the array of employees can now be sorted as shown below: import java.util.Arrays; public class TestInterface { public static void main(String[] args) { Employee[] list = new Employee[3]; list[0] = new Executive("Jarallah Al-Ghamdi", 50000); list[1] = new HourlyEmployee("Azmat Ansari", 120); list[2] = new MonthlyEmployee("Sahalu Junaidu", 9000); ((Executive)list[0]).awardBonus(11000); for(int i = 0; i < list.length; i++) if(list[i] instanceof HourlyEmployee) ((HourlyEmployee)list[i]).addHours(60); Arrays.sort(list); for(int i = 0; i < list.length; i++) { list[i].print(); System.out.println("Paid: " + list[i].pay()); System.out.println("**********************"); } } } The progra m output

More Related Content