An irregular polygon needs a series of lines.īy making the class responsible for its code as well as its data, you can achieve polymorphism. A square or rectangle needs two co-ordinates for the top left and bottom right corners and (possibly) a rotation. A point shape needs only two co-ordinates (assuming it's in a two-dimensional space of course). With polymorphism, each of these classes will have different underlying data. The classic example is the Shape class and all the classes that can inherit from it (square, circle, dodecahedron, irregular polygon, splat and so on). They're rarely considered as objects in the usual term.īut, in that same way, a class like BigDecimal or Rational or Imaginary can also provide those operations, even though they operate on different data types. So polymorphism is the ability (in programming) to present the same interface for differing underlying forms (data types).įor example, in many languages, integers and floats are implicitly polymorphic since you can add, subtract, multiply and so on, irrespective of the fact that the types are different. Morph = change or form: morphology = study of biological form, Morpheus = the Greek god of dreams able to take any form.Poly = many: polygon = many-sided, polystyrene = many styrenes (a), polyglot = many languages, and so on.A parent class is defined just like a normal class.If you think about the Greek roots of the term, it should become obvious. The OO-way to do this is to create a parent class, and put all common fields and methods into the parent. Where similar functions are carried out by distinct pieces of code, it is generally beneficial to combine them into one by abstracting out the varying parts." 3įollowing the principle, we want to implement these style-related fields and methods in just one place. One principle that we can follow is the abstraction principle, which says "Each significant piece of functionality in a program should be implemented in just one place in the source code. We would have to edit every single class that supports colors and borders! Just consider what needs to be done if we decide to support border styles (dotted border, solid border, dashed border, etc). The code above, however, is not good code, even though it is correct. A method table stores a table of pointers to the methods, along with a table to the class fields.Īs an example, consider the following class:Ĭlass A An object is referred to through its reference, which is a pointer to the memory location where the instance fields for the object is stored, along with a pointer to a method table. In the figure above, there are two objects of the same class. It turned out that different implementations of Java may store the objects differently, but here is one way that we will follow for CS2030: Since an object contains both fields and methods, where do we keep an object? We mentioned last week that data (e.g., fields) and code (e.g., methods) are stored in two different regions in the memory. To help understand how classes and objects work, it is useful to visualize how they are stored in the memory. know the purpose and usage of Java keywords implements, extends, super, this, and understand Java concepts of arrays, enhanced for loop, and method signature.understand the concepts of object-oriented programming, including interface, polymorphism, late binding, inheritance, method overloading, and the usage of these concepts in programming. be able to build a mental model for how objects and classes are represented in Java. Lecture 2: Inheritance & Polymorphism Learning Objectives Enforcing Abstraction Barrier with Interface
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |