Understanding Abstract Classes and Inheritance in Object-Oriented Programming

Slide Note
Embed
Share

Inheritance in object-oriented programming allows for reusing proven and debugged high-quality software through abstract classes. Abstract classes serve as superclasses and cannot be instantiated, instead, they force child classes to implement specific methods. Concrete and abstract methods can coexist within an abstract class, guiding child classes on method implementation. Rules for child classes dictate how abstract methods inherited from a parent abstract class are implemented. The interplay of abstract classes and concrete classes in inheritance helps organize code effectively.


Uploaded on Apr 06, 2024 | 6 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. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Module 3 - Part 2 Abstract Classes

  2. 2 Introduction Inheritance major advantage Reusing proven and debugged high-quality software. Some methods are not implemented in the parent But in the child / sub class(es) These methods are called Abstract

  3. 3 Abstract classes super class Cannot be instantiated The class header will have the word abstract in it At least one method may have the word abstract in the method header* We ll come back to this later in this presentation Like other super classes Declare attributes/variables Implement methods Constructors/ toString ( )

  4. 4 Abstract classes super class Purpose of abstract (super class) Eliminate redundancy Force the sub-classes (child class) to implement the abstract method

  5. 5 Abstract classes super class An abstract class can have a mixture of methods which are abstract and concrete. Concrete Method: Implies there is a method body public void do_stuff() { x=7;} is a concrete method Abstract Method: Implies there is NO method body: public abstract void do_stuff(); Notice there is no {} s.

  6. Rules for Child Classes Typically, the child class of an abstract class will implement all the abstract methods it inherited. It can still access all public/protected methods it inherited from it s parent as normal. If the child class implements ALL the abstract methods, it can be declared as an abstract or concrete class, it s the user s choice. If it s not declared abstract, it can be instantiated. If at least one method remains abstract in this child, the child class must be marked abstract also.

  7. Recollect slide from Inheritance Mammal - temp - weight - iQ - furColor + Eat( ) + Drink( ) + Move( ) +(abstract) makeSound() Cow Cat Dog makeSound( ) makeSound( ) makeSound( ) Note: arrows point up to class they inherit from

  8. 8 Getting to a concrete class. Picture a parent class Mammal that is abstract. Let s imagine it has 4 abstract methods: eat(), drink(), move(), and use_hand() We may have another abstract class which inherits from Mammal called Primates. It may choose to implement use_hand() because all Primates have dexterous hands, but may choose to not implement eat, drink or move() Finally we may have a concrete class Human, which inherits from Primate and implements eat(), drink() and move(). We can make objects of type Human, but not Primate or Mammal.

  9. 9 Common Misunderstanding If any method in a class is abstract, the class must be marked abstract. This is TRUE Do not simplify this to think that all abstract classes have abstract methods. It s absolutely possible to have an abstract class, which has all methods implemented. An abstract class can have 0, 1, 2 or 50 abstract methods. A concrete class must have 0 abstract methods.

  10. 10 Creating an object with an Abstract Class Mammal m = new Mammal (); //results in a compile error! Mammal m = new Dog (); Mammal m = new Cat (); Mammal m = new Cow (); All of these are valid statements

  11. 11 Creating an object with subclass that extends an Abstract class Dog d = new Dog (); d.Eat() // is valid!!!

  12. Implementing Polymorphism with Abstract Classes Consider Mammal m; Since LHS can be the parent class And multiple sub classes can inherit the parent m = new Cat () OR m = new Cow(): We now can call m.makeSound(); The result could be meow OR moo

  13. Calling methods in abstract classes abstract class A { public abstract void do_things(); } class B extends A { @Override public void do_things() { PRINT( I m do_things1 in B ); } } If you make an object as follows: A myObj = new B(); You have defined myObj to be of type A You created a new object B and placed it on myObj. If you then call: myObj.do_things() you ll get the overridden method from B.

  14. Interesting future thought. abstract class A { public abstract void do_things(); } class B extends A { @Override public void do_things() { PRINT( I m do_things1 in B ); } public void do_b_things() { PRINT( I m do_b_things in B ); } } If you make an object as follows: A myObj = new B(); You will NOT be able to call myObj.do_b_things(). If you want that, you need an abstract method in A with the same name. This is because the COMPILER will see myObj is of type A, but A has no method called do_b_things.

  15. Summary Abstract Classes Can t be instantiated May have 0, 1 or 50+ abstract methods in them. If there is at least 1 abstract method, the class MUST be abstract Can inherit from other abstract classes May choose to implement any abstract methods, or pass them down to their children as abstract Concrete Classes: Can have NO abstract methods Can be instantiated. Polymorphism allows you to make an arraylist of the parent class, and add children to it.

Related


More Related Content