core java (fundamentals and oops concepts)

Post on 16-Jul-2015

308 Views

Category:

Education

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Core JAVA

Pradeep Jutur

Siva Koka

Programing language evolution

• Assembly languages

• Procedural languages

• Object Oriented Languages

• Functional Languages

Jobs at a glance

MVC Job Trends

Java Install

• Download and install JDK latest

• set JAVA_HOME = C:/Program Files/java/jdkxxx

• set PATH = %JAVA_HOME%\bin

• Testing…… “java –version” in command line /terminal

What is Java ? Few attributes

• Object Oriented language

• Used to code internet applications

• Platform Independent

• Write Once execute anywhere (JVM Dependent)

What is Java Language

• Character set• Keywords• Identifiers• Data types• Variables• Constants• Literals• Operators • Control statements • Arrays

Character Set

• Digits (0-9)

• Alphabets (upper case and lower case )

• Special Symbols(_ and $)

Keywords

• There are in total 50 Keywords (including reserved) in java

If ElseSwitchCase BreakcontinueDefaultForWhileDoTryCatchfinallyThrowsthrow

PackageImportClassExtendsImplementsThis SuperPublicPrivate ProtectedStaticReturnVoidsynchronize

ByteShortcharIntlongFloatDoubleBooleanStringNew Instanceofenum

AssertGotoConstNativeVolatile

Identifiers

• It is name which can be used for classes, interfaces, methods and variables

– Should not start with a number

– Can contain “_” and “$”

– It can start with “_” and “$”

Data Types

• Byte(1)

• Short(2)

• Char(2)

• Int(4)

• Long(8)

• Float(4)

• Double(8)

• Boolean(?)

Variables and Constants

• Used to store some value of any primitive datatype and in all other cases typically a memory location /reference.

• We don’t use CONST keyword but use final for the same purpose , CONST is a keyword though

Literals

• Literals are syntactic representation of Boolean , Numeric , String data.

• Numeric(integer and float)

• Character (16 bit integer)

• String (Characters included in the double quote marks)

Operators

• Athematic operators

• Relational operators

• Assignment operators

• Logical operators

• Unary operators

• Ternary operator

• Bitwise operators(&,|,~,>>,<<)

Control statements

• If , if else

• Switch , case , default ,break

• for , continue , break

• while, continue , break

• do while , continue, break

OOPS

• Abstraction

• Encapsulation

• Inheritance

• Polymorphism

Abstraction and Encapsulation

Abstraction and Encapsulation

Resource for definitions

• http://stackoverflow.com/questions/24626/abstraction-vs-information-hiding-vs-encapsulation/8694874#8694874

• http://www.quora.com/What-is-the-difference-between-Encapsulation-and-Abstraction-in-Computer-Programming

Definition in OOW

• Abstraction : Providing/refining away necessary/essential attributes & Operations of an object/group of objects from non essential properties/attributes.

• Encapsulation/Information hiding is writing properties and operations that operate on those properties in a single entity is called encapsulation.(black boxing) emphasis should be on bundling related state and behavior.

• Abstraction talks about the outward view of the object where as encapsulation talks about internal view of the object.

• Abstraction is defined by interfaces and abstract classes.

• Encapsulation is defines by using private, protected and public modifiers.

Inheritance and Polymorphism

• Writing a new class by using the functionality of an existing class is called Inheritance ,Existing class is called parent /base/super class and the new class is called child /derived/sub class.– Makes the objects align with real world objects

– Code reuse

• One operation behaving differently in different situations is called Polymorphism, which means one operation has multiple implementations– There are two types of polymorphisms in java

• Compile time /early binding

• Runtime /late binding

Class Structure

Class: is an entity which contains variables and methods these two are called members of the class.

Class class-name {

data-type var1,var2,var3;

return-type method-name(args){//method body //java instruction

}

}

Java Memory Modal

• Operating system java process structure.

• Native heap (sockets and stack) , Heap , JVM

• GC and GC types

• Java –XX:+PrintFlagsFinal –version

• Java –XX:+PrintFlagsFinal –version | grep – iE ‘heapsize|permsize|threadstacksize’

Local Variables

• Variables defined in a method are called local variables ,

• Scope of the variables is the method where they are declared

• Local variables need explicit initialization else compiler will throw “variable x is not initialized”

• Local variables can be primitives or reference variables

• Memory to the local variables will be allocated when the method execution happens

• Memory for the primitives and reference variables will be created in the stack frame.

Instance Variables

• Variables declared inside a class and outside all the methods are called instance variables.

• They must not be qualified with static keyword

• You don’t have to initialize instance variables

• You can access these variables in all the other methods of the class.

• Instance variables can be primitives as well as reference variables

• Memory to the instance variables is allocated during the creation of the object.

• Memory will be allocated on Heap

Class variables or Static variables

• static is the keyword that can be applied to classes, variables and methods.

• In case of classes remember that it is applied to the inner classes

• Members with static keyword are called static members

• Memory for the static variables is allocated in the “PemGen” of the heap as part class metadata.

• And it will be allocated when the class is loaded for the first time.

• There is only one copy of the static variables exists , it will be shared by all the instances of the object

• You cant qualify the local variables using static ,since they belongs to classes

• You can call the static variables using the class name

Class variables or Static variables

• Most of the same attributes applies to static methods

• Inside a static method you can use static variables and static methods , you can not use not static variables and methods.

• But you can call not static methods of an object

• You can use static members of a class from non static methods.

Final Variables

• Variables declared using final modifier are called final variables

• final variables are also called as constant variables

• final variable is allowed for class, instance and local variables. It could also be applied for a class and methods

Constructors

• Constructor is a special method whose name is same as class name

• Constructor does not contain return type even the void.

• Constructors are invoked by JVM when we are creating the object.

• Constructors are used to initialize the object with some values

• When we write a class without any constructor JVM inserts default constructor.

• When we write a class with argument constructor, we have to write the default constructor explicitly.

Constructors , Contd.,

• super() is used to invoke immediate superclass constructor

• With inheritance constructors will be invoked from bottom to top

• Constructors will be executed from top to bottom.• “super” should be the first line in the constructor.• When you are not writing any super() in the

constructor , default super()will be inserted by the JVM.

• When you are not writing one JVM doesn’t insert one • Only one super() is allowed in the constructor.

Access Specifiers

• Access Specifiers , Access modifiers ,Visibility Modifiers.

– private

– default

– protected

– Public

• These specifiers specify the scope .

• private members can be accessed within the class where they are declared. They are not allowed outside the class.

• Top level classes can be default and public but not private

• We can use these 4 specifies for all the members of the class

• These specifiers are not allowed for local variables

• We cannot define local methods , i.e methods inside a method.

Method Overloading

• Writing more than one method with the same name with different parameters is called method overloading.

• Method overloading belongs to the same class

• Method overloading belongs to the same class.

• When you are overloading a methods, we have to change the parameters in one of the following rules– Number of parameters

– Order of the parameters

– Type of parameters

Method Overriding

• Method overriding in OOP is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes– Subclass methods can increase the scope but not decrease it

– If super class signature throws an exception , sub class method can eliminate the exception altogether / can throw subclass exception but not new unrelated one

– The return type of the subclass method can vary in a way the subclass return type is a subclass of superclass return type

– final methods cannot be overridden

– final classes cannot be extended

Abstract classes

• Abstract class is a class with that keyword , it may/may not contain abstract methods and concrete methods

• When you are unable to implement the method/ unable to provide boday of the method make that class abstract.

• When you write one or more abstract classes ,we should make the class abstract reverse is not true

• Abstract class cant be instantiated but we can create reference variables.

• When extending an abstract class ,we have to override the abstract methods otherwise make the subclass abstract.

• We can write concrete static methods in abstract class

• We cant write abstract static methods in the abstract classes

• We can have concrete final methods in abstract classes

• We cant write abstract final methods

• No abstract constructors

Interface

• Interface is a fully abstract class which contains only constants and abstract methods

• All the variables are static and final by default

• All the methods are public and abstract by default

• No constructors and concrete methods in interface

• We cant instantiate interface but can have reference variables

Difference between abstract classes and interface

We cant achieve multiple inheritance using abstract classes

We can have multiple inheritance with interface

Abstract classes contains variables, constructors ,concrete methods, abstract methods and constructors.

Constants and abstract methods

We have to extend the abstract classes by using extend keywords.

We have to use “implement” keyword

When you are extending abstract classes we have to override all abstract methods in subclass otherwise declare the subclass abstract class

Same

We cant create object for abstract class but can create reference variable.

Same

Not all JVMs are created equal

top related