



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of inheritance in java, a fundamental technique used to create reusable classes. Learn how to derive a new class from an existing one, the concept of is-a relationship, and the use of protected modifier. Discover the difference between multiple inheritance and overriding methods, as well as the importance of class hierarchies and abstract classes.
Typology: Papers
1 / 6
This page cannot be seen from the preview
Don't miss anything!




//******************************************************************** // Words.java Java Foundations // // Demonstrates the use of an inherited method. //******************************************************************** public { class Words //----------------------------------------------------------------- // Instantiates a derived class and invokes its inherited and // //----------------------------------------------------------------- local methods. public { static void main (String[] args) Dictionary webster = new Dictionary(); System.out.println ("Number of pages: " + webster.getPages()); System.out.println ("Number webster (^) .ofgetDefinitions definitions:()); " + System.out.println ("Definitions webster.computeRatio per page:()); " + }^ }
//******************************************************************** // Book.java Java Foundations // // Represents a book. Used as the parent of a derived class to // //******************************************************************** demonstrate inheritance. public { class Book protected int pages = 1500; //---------------------------------------------------------------- // Pages mutator. //---------------------------------------------------------------- public void setPages (int numPages) { pages = numPages; } //---------------------------------------------------------------- // Pages accessor. //---------------------------------------------------------------- public int getPages () { return pages; }^ }
//******************************************************************** // Dictionary.java Java Foundations // //******************************************************************** Represents a dictionary, which is a book. Used to demonstrate inheritance. public { class Dictionary extends Book private int definitions = 52500; // public Computes double ration computeRatio of definitions () per page// to demo use of parent vars { return definitions/pages; } // public Setter void aka setDefinitions mutator (int numDefinitions) { definitions = numDefinitions; } // public Setter int akagetDefinitions. accessor () { return definitions; }^ }
//******************************************************************** // Words2.java Java Foundations // // Demonstrates the use of the super reference. //******************************************************************** public { class Words //----------------------------------------------------------------- // Instantiates a derived class and invokes its inherited and // //----------------------------------------------------------------- local methods. public { static void main (String[] args) Dictionary2 webster = new Dictionary2 (1500, 52500); System.out.println ("Number of pages: " + webster.getPages()); System.out.println ("Number webster (^) .ofgetDefinitions definitions:()); " + System.out.println ("Definitions webster.computeRatio per page:()); " + }^ }
//******************************************************************** // Book2.java Java Foundations // // Represents a book. Used as the parent of a derived class to // //******************************************************************** demonstrate inheritance and the use of the super reference. public { class Book protected int pages; public { Book2 (int numPages) // Constructor (to be invoked by child) }^ pages^ =^ numPages; public { void setPages (int numPages) pages = numPages; } public { int getPages () }^ return^ pages; }
//******************************************************************** // Dictionary2.java Java Foundations // // Represents a dictionary, which is a book. Used to demonstrate // //******************************************************************** the use of the super reference. public { class Dictionary2 extends Book private int definitions; //----------------------------------------------------------------- // Constructor: Sets up the dictionary with the specified number // //----------------------------------------------------------------- of pages and definitions. public { Dictionary2 (int numPages, int numDefinitions) super(numPages); }^ definitions^ =^ numDefinitions; (etc for computeRatio(), setDefinitions(), getDefinitions()…)
Therefore, a child class inherits from all its ancestor classes 1- 20
//******************************************************************** // FoodAnalyzer.java Java Foundations // // Demonstrates indirect access to inherited private members. //******************************************************************** public { class FoodAnalyzer //----------------------------------------------------------------- // Instantiates a Pizza object and prints its calories per // //----------------------------------------------------------------- serving. public { static void main (String[] args) Pizza special = new Pizza (275); System.out.println ("Calories special.caloriesPerServing per serving: " +()); }^ }
//******************************************************************** // Pizza.java Java Foundations // // Represents a pizza, which is a food item. Used to demonstrate // //******************************************************************** indirect referencing through inheritance. public { class Pizza extends FoodItem //----------------------------------------------------------------- // Sets up a pizza with the specified amount of fat (assumes // //----------------------------------------------------------------- eight servings). public { Pizza (int fatGrams) }^ super^ (fatGrams,^ 8); }
//******************************************************************** // FoodItem.java Java Foundations // // Represents an item of food. Used as the parent of a derived class // //******************************************************************** to demonstrate indirect referencing. public { class FoodItem final private private int fatGrams; int CALORIES_PER_GRAM = 9; protected int servings; //----------------------------------------------------------------- // Sets up this food item with the specified number of fat grams // //----------------------------------------------------------------- and number of servings. public { FoodItem (int numFatGrams, int numServings) fatGrams servings == numFatGrams;numServings; } (more…)