25 Questions
0 Views
3.4 Stars

CSC1208: Java Object Oriented Programming Chapter 11

Learn about object oriented programming techniques in Java, specifically chapter 11 covering inheritance and polymorphism. Quiz yourself on the concepts and techniques of OOP in Java.

Created by
@ImpressivePrehistoricArt
1/25
Find out if you were right!
Create an account to continue playing and access all the benefits such as generating your own quizzes, flashcards and much more!
Quiz Team

Access to a Library of 520,000+ Quizzes & Flashcards

Explore diverse subjects like math, history, science, literature and more in our expanding catalog.

Questions and Answers

What is the best way to design classes to avoid redundancy when modeling circles, rectangles, and triangles?

Use inheritance

Which class can be used to model all geometric objects such as Rectangle and Circle?

GeometricObject

A subclass inherits accessible data fields and methods from its superclass.

True

Inheritance is used to model the ______ relationship.

<p>is-a</p> Signup and view all the answers

Match the following Java keywords with their functionalities:

<p>super() = Explicitly calls a superclass constructor this() = Invoke an overloaded constructor within the same class new = Create a new instance of a class</p> Signup and view all the answers

What is method overriding in object-oriented programming?

<p>Method overriding is when a subclass modifies the implementation of a method defined in its superclass, using the same signature and return type.</p> Signup and view all the answers

Can a private method in a superclass be overridden in a subclass?

<p>False</p> Signup and view all the answers

What happens when a static method in a superclass is redefined in a subclass?

<p>The method in the superclass is hidden.</p> Signup and view all the answers

Every class in Java is descended from the ___ class.

<p>Object</p> Signup and view all the answers

Match the following terms:

<p>Polymorphism = Variable of a supertype can refer to a subtype object Dynamic Binding = Correct subclass version of a method is called at execution time Method Overriding = Subclass modifies the implementation of a method in the superclass Inheritance = Class is descended from another class</p> Signup and view all the answers

What is the output of the following Java code snippet?

class Person {
    public Person() {
        System.out.println("(1) Person's no-arg constructor is invoked");
    }
}

class Employee extends Person {
    public Employee() {
        this("(2) Invoke Employee’s overloaded constructor");
        System.out.println("(3) Employee's no-arg constructor is invoked");
    }
    public Employee(String s) {
        System.out.println(s);
    }
}

class Faculty extends Employee {
    public Faculty() {
        System.out.println("(4) Faculty's no-arg constructor is invoked");
    }
    public static void main(String[] args) {
        new Faculty();
    }
}

<p>(1) Person's no-arg constructor is invoked (2) Invoke Employee’s overloaded constructor (3) Employee's no-arg constructor is invoked (4) Faculty's no-arg constructor is invoked</p> Signup and view all the answers

What should be used in Java to call a superclass constructor from a subclass?

<p>super</p> Signup and view all the answers

In Java, when calling a superclass constructor in a subclass, the 'super' keyword must appear first in the constructor.

<p>True</p> Signup and view all the answers

What does the default implementation of the equals method in the Object class do?

<p>Return this == obj</p> Signup and view all the answers

What is the purpose of overriding the equals method in a class like Circle?

<p>To check for the same contents</p> Signup and view all the answers

ArrayList is known as a generic class with a generic type _.

<p>E</p> Signup and view all the answers

Match the following programming languages with their primary usage:

<p>Python = General-purpose programming JavaScript = Client-side scripting for web applications SQL = Database queries CSS = Styling web pages</p> Signup and view all the answers

A subclass can weaken the accessibility of a method defined in the superclass.

<p>False</p> Signup and view all the answers

When should the final modifier be used in a class?

<p>To prevent the class from being extended</p> Signup and view all the answers

What is polymorphism in Java?

<p>Polymorphism occurs when a program invokes a method through a superclass variable. At execution time, the correct subclass version of the method is called, based on the type of the reference stored in the superclass variable.</p> Signup and view all the answers

How does dynamic binding work in Java?

<p>Searches for method implementation in subclasses at runtime</p> Signup and view all the answers

Casting can be used to convert an object of one class type to another within an ______________ hierarchy.

<p>inheritance</p> Signup and view all the answers

The statement 'Object o = new Student();' is an example of implicit casting.

<p>True</p> Signup and view all the answers

What is the purpose of using explicit casting in Java?

<p>Explicit casting is used to inform the compiler about the specific type of an object when converting between classes, especially when downcasting from a superclass to a subclass.</p> Signup and view all the answers

Match the following terms with their definitions:

<p>instanceof Operator = Tests whether an object is an instance of a class equals Method = Compares two objects</p> Signup and view all the answers

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Study Notes

Inheritance and Polymorphism

  • Inheritance is used to design classes that have common features, avoiding redundancy.
  • A superclass (general class) is extended to more specialized classes (subclasses).
  • Multi-level inheritance is possible, where a subclass can extend another subclass.

Superclasses and Subclasses

  • A subclass inherits accessible data fields and methods from its superclass.
  • A subclass can also add new data fields and methods.
  • Private data fields in a superclass are not accessible outside the class.
  • A subclass can access private data fields through public accessors and mutators in the superclass.

Implementation

  • A subclass can inherit methods from its superclass.
  • A method in a subclass can override a method in its superclass.
  • The superclass's constructors are not inherited by a subclass, but can be invoked explicitly using the super keyword.

Remarks

  • A subclass is not a subset of its superclass, but usually contains more information and methods.
  • Inheritance is used to model the "is-a" relationship between classes.
  • A Java class may inherit directly from only one superclass (single inheritance).
  • A subclass and its superclass must have the "is-a" relationship.

Inheritance of Constructors

  • The constructors of a superclass are not inherited by a subclass.
  • A subclass can invoke a superclass's constructor explicitly using the super keyword.
  • If no constructor is explicitly invoked, the compiler will add a call to the superclass's no-arg constructor.

Example of Constructor Inheritance

  • An example of constructor inheritance is shown, where a Faculty class extends an Employee class, which extends a Person class.
  • The constructors of each class are invoked in the order of Person, Employee, Faculty.### Object Oriented Programming Techniques
  • Inheritance: a subclass inherits from a superclass, and can also add new properties, add new methods, or override the methods of the superclass.
  • Method overriding: a subclass inherits methods from a superclass, but can modify the implementation of a method defined in the superclass by using the same signature and return type.
  • Method overloading: a class can have multiple methods with the same name but different parameters.

Constructors

  • A subclass constructor must call the superclass constructor using the super keyword.
  • If no constructor is specified in a subclass, the default constructor in the superclass is called.
  • A constructor can be overloaded with different parameters.

The Object Class and Its Methods

  • Every class in Java is descended from the java.lang.Object class.
  • The Object class is the parent class of all classes in Java by default.
  • The Object class has 11 methods, including toString(), equals(), and hashCode().
  • The toString() method returns a string representation of the object.
  • The equals() method checks if two objects are equal.
  • The hashCode() method returns a unique integer for each object.

Polymorphism

  • Polymorphism means that a variable of a supertype can refer to a subtype object.
  • A class defines a type, and a subtype is a type defined by a subclass.
  • A supertype is a type defined by a superclass.
  • Polymorphism allows for generic programming, where a method can be invoked on an object of any type.

Dynamic Binding

  • Dynamic binding occurs when a program invokes a method through a superclass variable.
  • At execution time, the correct subclass version of the method is called, based on the type of the reference stored in the superclass variable.
  • The Java Virtual Machine dynamically binds the implementation of the method at runtime.

Method Matching vs. Binding

  • Method matching involves finding a matching method signature at compilation time.

  • Method binding involves dynamically binding the implementation of the method at runtime.

  • The Java Virtual Machine searches for the implementation of a method in the order of subclass to superclass until it is found.### Polymorphism

  • Polymorphism allows methods to be used generically for a wide range of object arguments.

  • This is known as generic programming.

  • If a method's parameter type is a superclass, you may pass an object to this method of any of the parameter's subclasses.

Techniques

  • Casting can be used to convert an object of one class type to another within an inheritance hierarchy.
  • Implicit casting, or upcasting, is legal because an instance of a subclass is automatically an instance of its superclass.
  • Explicit casting, or downcasting, must be used when casting an object from a superclass to a subclass.
  • Casting does not change the object, but labels it in a different way.

Casting Objects

  • The statement Object o = new Student(); is an example of implicit casting, where an object of type Student is assigned to a variable of type Object.
  • The statement Student b = o; would cause a compile error because the compiler does not know that o is a Student object.
  • To fix this, use explicit casting: Student b = (Student) o;.

The instanceof Operator

  • The instanceof operator is used to test whether an object is an instance of a class.
  • Example: Object myObject = new Circle(); ... if (myObject instanceof Circle) {...}

The equals Method

  • The equals method is defined in the Object class and compares two objects.
  • The default implementation of the equals method checks whether two reference variables point to the same object using the == operator.
  • The equals method can be overridden in a class to make the test on specific values.

The ArrayList Class

  • The ArrayList class is a generic class that can be used to store an unlimited number of objects.
  • The ArrayList class is similar to an array, but its size is not fixed.
  • You can create an ArrayList from an array of objects and vice versa.

###.Generic Type

  • ArrayList is known as a generic class with a generic type E.
  • You can specify a concrete type to replace E when creating an ArrayList.

Accessibility Summary

  • private access is only accessible within the same class.
  • default access is accessible within the same class and package.
  • protected access is accessible within the same class, package, and its subclasses.
  • public access is accessible from anywhere.

The final Modifier

  • The final class cannot be extended.
  • The final variable is a constant.
  • The final method cannot be overridden by its subclasses.

Trusted by students at

More Quizzes Like This

Use Quizgecko on...
Browser
Browser