Summary

This document reviews hierarchies and interfaces in object-oriented programming, discussing superclasses, abstract classes, concrete classes, method calls, polymorphism, and interfaces. It covers the concept of inheritance, and how classes can inherit from and extend other classes, and explains benefits and use cases.

Full Transcript

Hierarchies and Interfaces Superclasses (inheritance): -​ Benefits: -​ Helps avoid duplicating code -​ This makes it easier to edit things, as instead of having to edit like 6 versions of the same code and make sure there's no issues, you just edit 1 vers...

Hierarchies and Interfaces Superclasses (inheritance): -​ Benefits: -​ Helps avoid duplicating code -​ This makes it easier to edit things, as instead of having to edit like 6 versions of the same code and make sure there's no issues, you just edit 1 version Abstract: -​ When you want a certain method to be empty/undefined, you use the abstract keyword, instead of leaving the method empty (as that may cause some issues) -​ A class that has one or more abstract method must be declared as an abstract class as well -​ You cannot create an object in an abstract class and if you try it will say “the class cannot be instantiated (cannot have an instance of that class)” Concrete Class: -​ A class with no abstract methods is called a concrete class, a concrete class can create objects -​ A concrete class can supply its own constructor and define abstract methods that it inherited from a superclass -​ In terms of hierarchy, abstract and concrete classes can mix, as long as all the classes at the bottom of the hierarchy are concrete Calling Superclass Methods && Constructors: -​ Just use the super keyword -​ To call methods just do super.methodName(); -​ In terms of using a superclass's constructor just do super() -​ Make sure the super() is the first thing in the subclasses constructor Polymorphism: -​ Basically, objects in a subclass can have their own unique traits and have the same functionality of their superclass -​ We can use parts of the superclass we want, and include new parts that aren’t in the superclass, and even have things that override the superclass’s things Interfaces: -​ So a superclass can have many subclasses, but subclasses can't extend multiple classes. -​ A class cannot inherit features from multiple different classes, aka cannot have multiple inheritance. The solution to this? Interfaces!! -​ An interface is a mechanism that allows a class to inherit the code (or atleast the data) from several sources -​ Interfaces are an abstract class, all of an interfaces methods are undefined, they have no constructors and no other code -​ Other classes implement interfaces -​ If the implemented class is concrete, it must supply all the methods in the interface, if it's abstract it can leave some methods abstract Why can't we have multiple abstract superclasses instead? -​ Well little boy, it's because a class can only have ONE superclass, but it can have MANY interfaces -​ Also an interface can extend another interface (in the textbook but mr rivard never mentioned it… i think)

Use Quizgecko on...
Browser
Browser