
Abstract Classes and Methods in Object-Oriented Programming
Abstract classes and methods are key concepts in object-oriented programming that allow for code reusability and structure. Abstract classes cannot be instantiated and can have abstract methods that serve as placeholders for implementation in child classes. This concept of abstraction is essential for creating flexible and extensible codebases, ensuring that child classes implement necessary functionality while providing a common structure.
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
Module 3 - Part 3 Abstract Classes
2 Introduction Inheritance major advantage Reusing proven and debugged high-quality software. Some methods are not implemented in the parent But in the child / subclass(es) These methods are called Abstract
Mammals Talking We ve seen with inheritance that children classes learn how to do things from their parents. In the last presentation we spoke about Mammal and it s children Dog, Cat and Cow. If we think about these for a moment, they all speak differently. Dogs say woof , Cats say meow and Cows moo . So what should the talk method in Mammal say? It doesn t really make sense for it to say anything, since each child will do their own thing. However, it does make sense for there to be a placeholder in mammal, because most mammals can talk. This is where you d use an abstract method. It s a placeholder which guarantees that all children will have a talk method But it doesn t have any code, since we don t know what each child will do. public talk(); Note: No {} s with a body.
Recollect slide from Inheritance Mammal - temp - weight - iQ - furColor + Eat( ) + Drink( ) + Move( ) +(abstract) talk() Cow Cat Dog talk( ) talk( ) talk( ) Note: arrows point up to class they inherit from
Abstract Methods Can have parameters and a return type. Cannot have a body, not even empty {} s An Abstract Method means that your class is no longer complete, it cannot be instantiated. The intention is that a child class will implement the method, and the child class will be complete and therefore can be instantiated.
Abstract Classes 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 it can Declare attributes/variables Implement methods Constructors/ toString ( ) Any Child classes will be forced to implement all the abstract methods.
Abstract Classes Continued An abstract class can have a mixture of methods which are abstract and concrete. Concrete Method: Implies there is a method body e.g: public void do_stuff() { x=7;} is a concrete method Abstract Method: Implies there is NO method body: e.g: public abstract void do_stuff(); Notice there is no {} s.
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.
9 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.
10 Common Misunderstanding True: If any method in a class is abstract, the class must be marked abstract. False: All abstract classes have at least one abstract method. An abstract class can have any number of abstract methods (INCLUDING 0!). A concrete class must have 0 abstract methods.
11 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 assuming Dog, Cat and Cow have no abstract methods
12 Creating an object with subclass that extends an Abstract class Dog d = new Dog (); d.Eat() // is valid!!!
Implementing Polymorphism with Abstract Classes Consider creating a variable m of type Mammal. Mammal m; Since the left hand side (Mammal) is a parent class and multiple child classes (Cat, Dog, Cow) exist which are concrete we can say: m = new Cat () OR m = new Cow(): Regardless of which child is on m, we are allowed to say: m.makeSound(); The result could be meow OR moo
Calling methods in abstract classes abstract class A { public abstract void do_things(); } class B extends A { @Override public void do_things() { System.out.println( 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.
Multiple Layers Here you can see it s possible for intermediate classes to leave talk abstract. There can be as many layers as necessary The only rule is that SOMEONE must give a concrete talk() method before you reach the bottom. Mammal - temp - weight - iQ - furColor + Eat( ) + Drink( ) + Move( ) +(abstract) talk() Primate +(abstract) talk() Human +talk()
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.