Please enable JavaScript.
Coggle requires JavaScript to display documents.
Data structure (Chapter 1) - Coggle Diagram
Data structure (Chapter 1)
Class member
Instance
Variables
Variables that are defined inside the class but outside all
methods.
Instance variables have default values. For numbers, the default
values is 0, for Booleans it is false, for objects its null
Instance variables are created when an object is created and
destroyed when the object is destroyed.
constructors
used to initialize
the object.
called implicitly at the time when object is
created.
Two rules to define a constructor:
Its name must be same as its class name
It must have no return type (even no void)
Class can have more than one constructor (overloading).
methods
It is a block of code that is referred by name and can be
called to perform an operation.
Local variables
Variables defined inside a method are called local variables.
They have no default initial values, so they should be
initialized before they are used.
Method Overloading
Method overloading: when a class has two or more methods by
the same name but different parameters, it is called method
overloading
Access Modifiers
Public
visible everywhere
Default (no modifier):
visible only within the same package.
Protected
visible within the same package and to sub classes
outside the package.
Private:
visible only within the class
Static instance variable:
share the same copy of that variable
Static method can be called using the class name without creating an
object.
The static method can not use non static data member or call non-static
method directly
Composition
Has-a relation
Embedding one class inside another is called composition.
Inheritance
Is a relationship
Every class has one and only one direct superclass
Every class in Java implicitly inherits class Object
extends keyword
Polymorphism
Dynamic binding
figuring out which method to call at
runtime
Upcasting
the reference variable of Parent class refers to the object
of Child class, it is known as upcasting.
Example
class A{}
class B extends A{}
A a = new B(); //upcasting
variable a
This reference variable a can access all the methods and
variables of class A but only overridden methods in child
class B
runtime polymorphism.
Downcasting
Attempting to invoke a subclass-only method directly on a
superclass reference is a compilation error.
Upcasting is casting to a supertype, while downcasting is casting to a
subtype.
Abstract Classes
Abstract classes can contain implemented methods and abstract
methods that are NOT implemented
define a template to
guarantee that all subclasses
Guarantees that all subclasses will have certain methods =>
enforce a common design.
Lets you make collections of mixed type objects that can
processed polymorphically
abstract keyword
Interfaces
implements keyword