MELJUN CORTES Java Lecture Inheritance Polymorphism

34
Inheritance and Inheritance and Polymorphism in Polymorphism in Java Java Java Fundamentals and Object- Java Fundamentals and Object- Oriented Programming Oriented Programming The Complete Java Boot Camp The Complete Java Boot Camp MELJUN CORTES MELJUN CORTES MELJUN CORTES MELJUN CORTES

description

MELJUN CORTES Java Lecture Inheritance Polymorphism

Transcript of MELJUN CORTES Java Lecture Inheritance Polymorphism

  • 1.Inheritance and Polymorphism in JavaMELJUN CORTESJava Fundamentals and ObjectOriented Programming The Complete Java Boot CampMELJUN CORTES

2. What You Should Learn ImplementationInheritance Extending a Class Overriding a Method Abstract Class The final Keyword The protected Modifier The super Keyword Interface Inheritance What is a Java interface? Using Interfaces Creating an Interface Implementing an Interface Extending an Interface 3. Implementation Inheritance Java Fundamentals and ObjectOriented Programming The Complete Java Boot Camp 4. Extending a Class Use extends keyword to create asubclass: class Bar extends class Foo { } 5. Extending a Class A subclass can still implement interfaces: class Bar extends Foo implements Pik, Pak, Boom { } 6. Overriding a method You can re-implement an inherited method: class Foo { void method() { do something } } class Bar extends Foo { void method() { do something else } } 7. Abstract Class You can mark a class to be purely forcode-sharing purposes: abstract class Foo { } An abstract class cannot be instantiated. 8. The final Keyword You can also mark a class so it cannot besubclassed: final class Bar { } 9. The final Keyword You can also mark a method so thatmethod cannot be overridden: class Bar { final void method() { } } 10. The protected Modifier Methods and attributes that are marked protectedwill be visible to a subclass even if it is in another package class Foo { protected void method() { } } 11. The protected Modifier but will not be visible to any other class outsidethe superclasss package.class Foo { protected void method() { } } 12. The super Keyword The super keyword is used to denote thesuperclass:class Bar extends Foo { Bar() { super(); } int method() { return super.anotherMethod(); } } 13. The super Keyword Use super to access members of thesuperclass: int method() { int temp = super.doSomething(); return temp; } 14. The super Keyword If you do not call a superclass constructor in yoursubclass constructor, the compiler will add it for you: your code: class Bar extends Foo { Bar() { do stuff } } 15. The super Keyword If you do not call a superclass constructor in yoursubclass constructor, the compiler will add it for you: after compilation: class Bar extends Foo { Bar() { super(); added by compiler do stuff } } 16. The super Keyword If the no-argument constructor does not exist inthe superclass, then the compiler will throw an error: after compilation: class Bar extends Foo { Bar() { super(); added by compiler do stuff } } 17. The super Keyword If you will explicitly call the superclassconstructor, it must be at the first line of your subclass constructor class Bar extends Foo { Bar(int x, int y) { super(x, y); do stuff } } 18. The super Keyword If you will explicitly call the superclassconstructor, it must be at the first line of your subclass constructor class Bar extends Foo { Bar(int x, int y) { do stuff super(x, y); } } 19. Interface Inheritance Java Fundamentals and ObjectOriented Programming The Complete Java Boot Camp 20. What is a Java interface? Java construct for interface inheritance Classes that implement an interface inherit only method signatures. Its a lot like a contract Any class that implements a particular interface must implement all its methods. Useful in large-team environments. Compels other programmers in the team to implement their classes in a way that will fit with your own work. 21. Using an Interface Example: 22. Using an Interface Example: 23. Using an Interface Best Practice: If an interface exists, neverrefer to the concrete class unless you absolutely have to. Refer to the interface instead: List listOfStudents = new ArrayList(); 24. Using an Interface Best Practice: Method parameters and returntypes should be interfaces and not concrete classes whenever possible. List getAllStudents() { final List students = new ArrayList(); ... return students; } void enrollStudents(final List students) {... 25. Using an Interface that way, if you ever need to change yourimplementation, you only need to edit one line of code....// in the getAllStudents method: List students = new ArrayList(); ... return students; ...// somewhere else in the application final List allStudents = dao.getAllStudents(); allStudents.add(joey); service.enrollStuents(allStudents); 26. Using an Interface that way, if you ever need to change yourimplementation, you only need to edit one line of code....// in the getAllStudents method: List students = new LinkedList(); ... return students; ...// somewhere else in the application final List allStudents = dao.getAllStudents(); allStudents.add(joey); service.enrollStuents(allStudents); 27. Creating an Interface interface { * * } 28. Creating an Interface public interface StudentDAO { int UNDERGRAD = 1; int MASTERAL = 2; int DOCTORAL = 3; List getAllStudents(); Student getStudentWithId(int studentId); void saveStudent(Student student); void deleteStudent(Student student); void deleteStudentWithId(int studentId); } 29. Creating an Interface All methods are public even if you dontspecify it! You cannot create static methods. All fields are public, static and final even ifyou dont specify it! (constants) 30. Implementing an Interface class StudendDaoOracleImplimplements StudentDAO { public List getAllStudents() { final String sql = SELECT * FROM stu... ... }public Student getStudentWithId(int studentId) { ... } } 31. Implementing an Interface Use the implements keyword to declarethat a class inherits from an interface. You must implement all methods ordeclare your class abstract. 32. Interface Implementing an Interface A class can implement more than oneinterface: class Foo implements Pik, Pak, Boom { } 33. Extending an Interface An interface can extend another interfaceusing the extends keyword. interface Super extends Sub { } 34. The End Java Fundamentals and Object-Oriented Programming The Complete Java Boot Camp