Sunteți pe pagina 1din 3

MIDTERM SUMMARY THEORY PART

A class is the blueprint from which individual objects are created.


Objects : An entity that has state and behavior. An object has three characteristics:
state: represents data (value) of an object.
behavior: represents the behavior (functionality) of an object such as deposit, withdraw etc.
identity: Object identity is typically implemented via a unique ID. The value of the ID is not
visible to the external user. But, it is used internally by the JVM to identify each object uniquely.
The term object, refers to an actual instance of a class. Every object must belong to a class. Objects are
created and eventually destroyed so they only live in the program for a limited time. While objects are
living their properties may also be changed significantly. So, we can say that whereas a class is a
general concept, an object is a very specific embodiment of that class, with a limited lifespan .
Immutable object: the contents of an object cannot be changed once the object is create d.
Constructors are a special kind of methods that are invoked to construct (initialize) objects. A
constructor with no parameters is referred to as a no-arg constructor. Constructors must have the same
name as the class itself. Constructors do not have a return typenot even void. A default constructor
(no-arg constructor), is provided automatically only if no constructors are explicitly declared in the class.
You must use the keyword super to call the superclass constructor
Instance variables are variables declared in a class, but outside a method, constructor or any block.
Public: The class, data, or method is visible to any class in any package.
Private: The data or methods can be accessed only by the declaring class. The get and set methods are
used to read and modify private properties.
Protected : protected data or methods can be accessed from the class itself, subclasses of the class and
also all classes in the same package of the class (doesn't matter if those are subclasses or not).
Default: If a class has no modifier ( the default, also known as package-private), it is visible only within
its own package.
Final: defines a constant data. Final method cannot be overridden by its subclasses. Final class cannot be
extended.
Static: The static keyword in Java means that the variable or function is shared between all instances of
that class as it belongs to the type, not the actual objects themselves. Static methods can be used
without instantiating an object.

Tip: Make the fields private and accessor methods public if they are intended for the users of the class.
Make the fields or method protected if they are intended for extenders of the class.
Dynamic Binding: When compiler is not able to resolve the call/binding at compile time, such binding is
known as Dynamic or late Binding( occurs during Runtime). Overriding is a perfect example of dynamic
binding as in overriding both parent and child classes have same method.
Casting : used to convert an object of one class type to another within an inheritance hierarchy. 2 types
of casting : explicit and implicit.
Encapsulation: Encapsulation in Java is a me chanism of wrapping the data (variables) and code acting on
the data (methods) together as as single unit. In encapsulation the variables of a class will b e hidden
from other classes, and can be accessed only through the methods of their current class, therefore it is
also known as data hiding.
Polymorphism: refers to Javas ability to use base-class variables to refer to subclass objects, keep track
of which subclass an object belongs to, and use overridden methods of the subclass even though the
subclass isnt known when the program is compiled. Whenever a parameter calls for a particular type,
you can use an object created from a subclass of that type instead. (Remember print method for cats
and dogs, subclasses of animal).
An abstract class is a class that is declared using the abstract keyword. An abstract class cannot be
instantiated. It can be used only as a super-class for those classes that extend the abstract class. The
default functionality of the class still exists, with its fields, methods and constructors being accessed in
the same way as with the other classes.
Moreover, an abstract class may contain methods without any implementation, called abs tract
methods. The declaration of an abstract method starts with the abstract keyword and ends with a
semicolon, instead of the methods body. If a class contains an abstract method, either declared or
inherited, it must be declared as an abstract class.
A class that extends an abstract class must implement all its abstract methods (if any). Otherwise, the
sub-class must be declared as abstract as well. Finally, any implementation of an abstract method can be
overridden by additional sub-classes.
Last things to mention are that abstract classes can also implement methods, despite providing just
their signature and that an abstract class may have static fields and static methods. The purpose of an
abstract class is to specify the default functionality of an object and let its sub-classes to explicitly
implement that functionality. Thus, it stands as an abstraction layer that must be extended and
implemented by the corresponding sub-classes.
Interface: An interface is a classlike construct that contains only constants and abstract methods. An
interface is similar to an abstract class, but the intent of an interface is to specify behavior for objects .
Definition: public interface InterfaceName{ constant declarations; method signitures;}

Abstract Class vs. Interface


Java provides and supports the creation of abstract classes and interfaces. Both
implementations share some common features, but they differ in the following features:

All methods in an interface are implicitly abstract. On the other hand, an abstract class
may contain both abstract and non-abstract methods.

A class may implement a number of Interfaces, but can extend only one abstract class.

In order for a class to implement an interface, it must implement all its declared
methods. However, a class may not implement all declared methods of an abstract class.
Though, in this case, the sub-class must also be declared as abstract.

Abstract classes can implement interfaces without even providing the implementation
of interface methods.

Variables declared in a Java interface is by default final. An abstract class may contain
non-final variables.

Members of a Java interface are public by default. A member of an abstract class can
either be private, protected or public.

An interface is absolutely abstract and cannot be instantiated. An abstract class also


cannot be instantiated, but can be invoked if it contains a main method.

S-ar putea să vă placă și