Inheritance and Polymorphism

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Extends

Extends is the key word used to indicate that you're about to initiate inheritance. public class Subclass extends Superclass { blablablablablablablabla } In this case, the Subclass is inheriting from the parent Superclass. For example, a bicycle super-class will have 3 fields, one constructor, and 4 methods and the mountainbike sub-class will have 1 field 1 constructor and 1 method inherited through the use of extends. public class MountainBike extends Bicycle { blablablablablablablabla }

Inheritance

Inheritance allows a class to use the properties and methods of another class. In other words, the derived class inherits the states and behaviors from the base class. Base class is also known as super-class. Derived classes are also known as sub classes. A super-class can have multiple sub classes, but only one super-class can be dedicated toward a subclass because Java does not allow "multiple inheritance". The key word used for inheritance is called "extends". An example is an imaginary class called Vehicle. Since all cars have the same traits, (color, size, etc) we can create a super-class with all of these common traits and create multiple different sub classes that have variations of these traits.

Polymorphism

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Use it when you want to implement multiple inheritance (a sub-class getting traits from more than one).

Super class

Super class is another term for base class that will have all of its traits inherited by each of the sub classes. To pass on the traits of the Super-class to the sub class: public class Subclass extends Superclass { blablablablablablablabla } Sub classes inherit only the members and methods that are declared public.

This

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this. The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter. For example: Without this: public class Point { public int x = 0; public int y = 0; //constructor public Point(int a, int b) { x = a; y = b; } } With this: public class Point { public int x = 0; public int y = 0; //constructor public Point(int x, int y) { this.x = x; this.y = y; } }


Ensembles d'études connexes

TestOut Flashcards - 10.4 Post Installation

View Set

Chapter 9: Partnership: Formation and Operation

View Set

Chapter 13: Health Insurance Providers

View Set

Adult Health- Respiratory Alterations

View Set