Você está na página 1de 11

Java Concepts

4 August 2015

Prasun Neogy

Contents
1. Some basic but IMPORTANT concepts

4 August 2015

Prasun Neogy

Some important Java core concepts


There are 5 basic characteristics of a Object-oriented programming
1. Everything is an Object. Think of an object as a fancy variable; it stores data, but you can
make requests to that object, asking it to perform operations on itself. In theory, you can
take any conceptual component in the problem youre trying to solve (dogs, buildings,
services, etc.) and represent it as an object in your program.
2. A program is a bunch of objects telling each other what to do by sending messages. To
make a request of an object, you send a message to that object. More concretely, you can
think of a message as a request to call a method that belongs to a particular object.
3. Each object has its own memory made up of other objects. Put another way, you create a
new kind of object by making a package containing existing objects. Thus, you can build
complexity into a program while hiding it behind the simplicity of objects.
4. Every object has a type. Using the parlance, each object is an instance of a class, in which
class is synonymous with type. The most important distinguishing characteristic of a class
is What messages can you send to it?
5. All objects of a particular type can receive the same messages. This is actually a loaded
statement, as you will see later. Because an object of type circle is also an object of type
shape, a circle is guaranteed to accept shape messages. This means you can write code
that talks to shapes and automatically handle anything that fits the description of a shape.
This substitutability is one of the powerful concepts in OOP.
4 August 2015

Prasun Neogy

Some important Java core concepts


Creating abstract data types (classes) is a fundamental concept in object-oriented
programming.
Abstract data types work almost exactly like built-in types: You can create variables of a type
(called objects or instances in object-oriented parlance) and manipulate those variables
(called sending messages or requests; you send a message and the object figures out what
to do with it).
The members (elements) of each class share some commonality.
This entity is the object, and each object belongs to a particular class that defines its
characteristics and behaviors.
So, although what we really do in object-oriented programming is create new data types,
virtually all object-oriented programming languages use the class keyword.
When you see the word type think class and vice versa.
Since a class describes a set of objects that have identical characteristics (data elements)
and behaviors (functionality), a class is really a data type
Once a class is established, you can make as many objects of that class as you like, and
then manipulate those objects as if they are the elements that exist in the problem you are
trying to solve.
4 August 2015

Prasun Neogy

Some important Java core concepts


But how do you get an object to do useful work for you? There needs to be a way to make a
request of the object so that it will do something, such as complete a transaction, draw
something on the screen, or turn on a switch. And each object can satisfy only certain
requests. The requests you can make of an object are defined by its interface, and the type
is what determines the interface.
The interface determines the requests that you can make for a particular object. However,
there must be code somewhere to satisfy that request. This, along with the hidden data,
comprises the implementation.
One of the best ways to think about objects is as service providers. Your program itself will
provide services to the user, and it will accomplish this by using the services offered by other
objects.
Thinking of an object as a service provider has an additional benefit: It helps to improve the
cohesiveness of the object. High cohesion is a fundamental quality of software design: It
means that the various aspects of a software component (such as an object, although this
could also apply to a method or a library of objects) fit together well.
In a good object-oriented design, each object does one thing well, but doesnt try to do too
much.
4 August 2015

Prasun Neogy

Some important Java core concepts


Class - A class is a specification, or blueprintexpressed as a piece of program codethat
defines what goes to make up a particular sort of object.
Object - An object is an instance of a class. Every object that your program uses must have
a corresponding class definition somewhere for objects of that type.
An instance of a class is a technical term for an existing object of that class. After you have a
class defined you can create objects, or instances, of that class.
Methods - A method is basically a behavior. A class can contain many methods. It is in
methods where the logics are written, data is manipulated and all the actions are executed.
Instance Variables - Each object has its unique set of instance variables. An object's state is
created by the values assigned to these instance variables.
A class definition identifies all the parameters that define an object of that particular class
type
Encapsulation - Encapsulation refers to the hiding of items of data and methods within an
object. This is achieved by specifying them as private in the definition of the class.

4 August 2015

Prasun Neogy

Some important Java core concepts


Inheritance - Java classes can be derived from classes. Basically, if you need to create a
new class and here is already a class that has some of the code you require, then it is
possible to derive your new class from the already existing code.
This concept allows you to reuse the fields and methods of the existing class without having
to rewrite the code in a new class.
In this scenario, the existing class is called the superclass and the derived class is called the
subclass.
Interfaces - In Java language, an interface can be defined as a contract between objects on
how to communicate with each other. Interfaces play a vital role when it comes to the
concept of inheritance. An interface defines the methods, a deriving class(subclass) should
use. But the implementation of the methods is totally up to the subclass.
Generic Classes - A generic class is a recipe for creating classes of a similar nature. You can
specify a generic class representing a collection of things that you can use to create a class
that can store a collection of things of a particular type.
Annotations - A Java source file can contain annotations. An annotation is not a Java
language statement, but a special statement that changes the way program statements are
treated by the compiler or the libraries. You can define your own annotations but most Java
programmers never need to do so.
4 August 2015

Prasun Neogy

Some important Java core concepts


A class can contain any of the following variable types.
Local variables: Variables defined inside methods, constructors or blocks are called
local variables. The variable will be declared and initialized within the method and the
variable will be destroyed when the method has completed.
Instance variables: Instance variables are variables within a class but outside any
method. These variables are instantiated when the class is loaded. Instance variables
can be accessed from inside any method, constructor or blocks of that particular class.
Class variables: Class variables are variables declared within a class, outside any
method, with the static keyword.
A class can have any number of methods to access the value of various kinds of methods.
Constructors - Every class has a constructor. If we do not explicitly write a constructor for a
class the Java compiler builds a default constructor for that class.
Each time a new object is created, at least one constructor will be invoked.
The main rule of constructors is that they should have the same name as the class.
A class can have more than one constructor.

4 August 2015

Prasun Neogy

Some important Java core concepts


Creating an Object :
A class provides the blueprints for objects. So basically an object is created from a class.
In Java the new keyword is used to create new objects.
There are three steps when creating an object from a class:
Declaration: A variable declaration with a variable name with an object type.
Instantiation: The 'new' keyword is used to create the object.
Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the
new object.
Concrete class - A concrete class is the class that has the implementation of an interface. A
concrete subclass on the other hand has the implementation of an abstract superclass that is
extended. Both the interface and abstract superclass has method definitions which are then
implemented in their concrete classes.
Abstract Class - an abstract class is one that does not provide implementations for all its
methods. A class must be declared abstract if any of the methods in that class are abstract.
An abstract class is meant to be used as the base class from which other classes are derived.
The derived class is expected to provide implementations for the methods that are not
implemented in the base class. A derived class that implements all the missing functionality is
called a concrete class . In Java it is not possible to instantiate an abstract class.
4 August 2015

Prasun Neogy

References

4 August 2015

Prasun Neogy

10

End of Presentation
Java core Concepts

4 August 2015

Prasun Neogy

11

Você também pode gostar