📕
Dan Fitz's Notes
  • README
  • Ai
    • Supervised Machine Learning
      • Introduction To Machine Learning
      • Regression With Multiple Input Variables
      • Classification
  • Csharp
    • C Sharp Advanced
      • Generics
      • Delegates
      • Lambda Expressions
      • Events
    • C Sharp Fundamentals
      • Intro To C
      • Primitive Types And Expressions
      • Non Primitive Types
      • Control Flow
      • Arrays And Lists
      • Working With Dates
      • Working With Text
      • Working With Files
      • Debugging Applications
    • C Sharp Intermediate
      • Classes
      • Association Between Classes
      • Inheritance
      • Polymorphism
      • Interfaces
  • Java
    • Inheritance Data Structures Java
      • Inheritance Polymorphism Using Overriding And Access Modifiers
      • Abstract Classes And Debugging
      • File I O And Exceptions
      • Collections Maps And Regular Expressions
    • Intro To Java
      • Introduction To Java Classes And Eclipse
      • Unit Testing Arrays And Array Lists
      • Static Variables Methods And Polymorphism Using Overloading
  • Javascript
    • Algorithms Data Structures
      • Big O Notation
      • Analyzing Performance Of Arrays And Objects
      • Problem Solving Approach
      • Problem Solving Patterns
      • Recursion
      • Searching Algorithms
      • Bubble Selection And Insertion Sort
      • Merge Sort
      • Quick Sort
      • Radix Sort
      • Data Structures Introduction
      • Singly Linked Lists
      • Doubly Linked Lists
      • Stacks And Queues
      • Binary Search Trees
      • Tree Traversal
      • Binary Heaps
    • Complete Nodejs
      • Understanding Node.js
      • REST AP Is And Mongoose
      • API Authentication And Security
      • Node.js Module System
      • File System And Command Line Args
      • Debugging Node.js
      • Asynchronous Node.js
      • Web Servers
      • Accessing API From Browser
      • Application Deployment
      • Mongo DB And Promises
    • Complete React Native
      • Working With Content
      • Building Lists
      • Navigating Users Between Screens
      • State Management
      • Handling Screen Layout
      • Setting Up An App
      • More On Navigation
      • Advanced Statement Management With Context
      • Building A Custom Express API
      • In App Authentication
    • Epic React
      • React Fundamentals
      • React Hooks
      • Advanced React Hooks
      • Advanced React Patterns
      • React Performance
    • Fireship Firestore
      • Firestore Queries And Data Modeling Course
      • Model Relational Data In Firestore No SQL
    • Functional Light Javascript
      • Intro
      • Function Purity
      • Argument Adapters
      • Point Free
      • Closure
      • Composition
      • Immutability
      • Recursion
      • List Operations
      • Transduction
      • Data Structure Operations
      • Async
    • Js Weird Parts
      • Execution Contexts And Lexical Environments
      • Types And Operators
      • Objects And Functions
      • Object Oriented Java Script And Prototypal Inheritance
      • Defining Objects
    • Mastering Chrome Dev Tools
      • Introduction
      • Editing
      • Debugging
      • Networking
      • Auditing
      • Node.js Profiling
      • Performance Monitoring
      • Image Performance
      • Memory
    • React Complete Guide
      • What Is React
      • React Basics
      • Rendering Lists And Conditionals
      • Styling React Components
      • Debugging React Apps
      • Component Deep Dive
      • Building A React App
      • Reaching Out To The Web
      • Routing
    • React Testing
      • Intro To Jest Enzyme And TDD
      • Basic Testing
      • Redux Testing
      • Redux Thunk Testing
    • Serverless Bootcamp
      • Introduction
      • Auction Service Setup
      • Auction Service CRUD Operations
      • Auction Service Processing Auctions
    • Testing Javascript
      • Fundamentals Of Testing
      • Static Analysis Testing
      • Mocking Fundamentals
      • Configuring Jest
      • Test React Components With Jest And React Testing Library
    • Typescript Developers Guide
      • Getting Started With Type Script
      • What Is A Type System
      • Type Annotations In Action
      • Annotations With Functions And Objects
      • Mastering Typed Arrays
      • Tuples In Type Script
      • The All Important Interface
      • Building Functionality With Classes
    • Web Performance With Webpack
      • Intro
      • Code Splitting
      • Module Methods Magic Comments
  • Other
    • Algo Expert
      • Defining Data Structures And Complexity Analysis
      • Memory
      • Big O Notation
      • Logarithm
      • Arrays
      • Linked Lists
      • Hash Tables
      • Stacks And Queues
      • Strings
      • Graphs
      • Trees
    • Aws Solutions Architect
      • AWS Fundamentals IAM EC 2
    • Fundamentals Math
      • Numbers And Negative Numbers
      • Factors And Multiples
      • Fractions
    • Mysql Bootcamp
      • Overview And Installation
      • Creating Databases And Tables
      • Inserting Data
      • CRUD Commands
      • The World Of String Functions
      • Refining Our Selections
      • The Magic Of Aggregate Functions
    • Random Notes
      • Understanding React Hooks
  • Python
    • Data Analysis Using Python
      • Loading Querying And Filtering Data Using The Csv Module
      • Loading Querying Joining And Filtering Data Using Pandas
      • Summarizing And Visualizing Data
    • Intro To Python
      • Course Introduction Intro To Programming And The Python Language Variables Conditionals Jupyter Notebook And IDLE
      • Intro To Lists Loops And Functions
      • More With Lists Strings Tuples Sets And Py Charm
      • Dictionaries And Files
Powered by GitBook
On this page
  • Abstract methods
  • Abstract Classes
  • Extending abstract classes
  • Purpose of abstract classes
  • Syntax errors when accessing methods in subclass casted to superclass
  • Abstract method as solution to syntax error
  • Interfaces
  • Eclipse Debugger
  1. Java
  2. Inheritance Data Structures Java

Abstract Classes And Debugging

Abstract methods

Just like how you can declare a variable without defining it, it's possible to declare a method without defining it too. These methods are called abstract methods:

public abstract void draw(int size);

Note:

  • abstract keyword is essential

  • You provide the access modifier, return type, method name, and parameters still

  • Only the body (curly braces) are missing

Abstract Classes

An abstract class tells the Java compiler that the class cannot be instantiated:

public abstract class Shape {
  // ...
}

Shape shape = new Shape(); // illegal move!

Note: The moment you declare an abstract method in a class, that class must be turned into an abstract class!

Note 2: Not all methods in an abstract class have to be abstract. You can have some concrete methods too.

Extending abstract classes

When you inherit an abstract class as a parent class, there are two possible outcomes:

  1. If the subclass defines all of the inherited abstract methods, it's considered a concrete class and can now be instantiated.

  2. If the subclass does not define all of the inherited abstract methods, it must be abstract too!

Purpose of abstract classes

Abstract classes are useful when

  1. You want to make sure each subclass implements certain methods before they can be instantiated, and

  2. You don't want the class itself to be instantiated because it's too abstract to be useful on its own.

For example, using the Plant class is not very useful on its own, so by making the class and the method grow both abstract, we ensure it never gets used.

At the same time, Hibiscus inherits from Plant, but maybe every plant has its own way of growing. By making grow an abstract method in Plant, we force the user to define grow for Hibiscus before it can be instantiated/used.

Syntax errors when accessing methods in subclass casted to superclass

Suppose you have a Shape superclass and a bunch of subclasses:

class Shape {
  // ...
}

class Circle extends Shape {
  void draw() {
    // ...
  }
}

class Triangle extends Shape {
  void draw() {
    // ...
  }
}

Shape has no draw method, but its subclasses do.

Now suppose you want to store a bunch of shapes into an ArrayList:

Circle circle = new Circle();
Triangle triangle = new Triangle();

ArrayList<Shape> shapes = new ArrayList<Shape>();
shapes.add(circle);
shapes.add(triangle);

for (Shape s : shapes) {
  s.draw();
}

Even though we know every subclass has a draw method, Java doesn't know that! Calling draw will result in a syntax error.

Important: Generally, subclasses know everything about their superclasses, but superclasses don't know about their subclasses. (That's why when we cast to Shape, Java has no awareness of the draw method anymore.)

Abstract method as solution to syntax error

Abstract methods solve this problem with draw. By making draw an abstract method (and Shape an abstract class as a result), you're giving Java awareness of the draw method and requiring each subclass to implement it!

abstract class abstract Shape {
  abstract void draw();
}

class Circle extends Shape {
  void draw() {
     // required
   }
}

Interfaces

Eclipse Debugger

Debugging allows you to run a program while watching the source code during execution.

Eclipse allows you to switch out of the Java perspective and into the Debug perspective, a set of views that make debugging easier.

PreviousInheritance Polymorphism Using Overriding And Access ModifiersNextFile I O And Exceptions

Last updated 3 years ago