core java (fundamentals and oops concepts)

39
Core JAVA Pradeep Jutur Siva Koka

Upload: pradeep-jutur

Post on 16-Jul-2015

303 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Core java (fundamentals and OOPS concepts)

Core JAVA

Pradeep Jutur

Siva Koka

Page 2: Core java (fundamentals and OOPS concepts)

Programing language evolution

• Assembly languages

• Procedural languages

• Object Oriented Languages

• Functional Languages

Page 3: Core java (fundamentals and OOPS concepts)

Jobs at a glance

Page 4: Core java (fundamentals and OOPS concepts)

MVC Job Trends

Page 5: Core java (fundamentals and OOPS concepts)

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

Page 6: Core java (fundamentals and OOPS concepts)

What is Java ? Few attributes

• Object Oriented language

• Used to code internet applications

• Platform Independent

• Write Once execute anywhere (JVM Dependent)

Page 7: Core java (fundamentals and OOPS concepts)

What is Java Language

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

Page 8: Core java (fundamentals and OOPS concepts)

Character Set

• Digits (0-9)

• Alphabets (upper case and lower case )

• Special Symbols(_ and $)

Page 9: Core java (fundamentals and OOPS concepts)

Keywords

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

If ElseSwitchCase BreakcontinueDefaultForWhileDoTryCatchfinallyThrowsthrow

PackageImportClassExtendsImplementsThis SuperPublicPrivate ProtectedStaticReturnVoidsynchronize

ByteShortcharIntlongFloatDoubleBooleanStringNew Instanceofenum

AssertGotoConstNativeVolatile

Page 10: Core java (fundamentals and OOPS concepts)

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 “$”

Page 11: Core java (fundamentals and OOPS concepts)

Data Types

• Byte(1)

• Short(2)

• Char(2)

• Int(4)

• Long(8)

• Float(4)

• Double(8)

• Boolean(?)

Page 12: Core java (fundamentals and OOPS concepts)

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

Page 13: Core java (fundamentals and OOPS concepts)

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)

Page 14: Core java (fundamentals and OOPS concepts)

Operators

• Athematic operators

• Relational operators

• Assignment operators

• Logical operators

• Unary operators

• Ternary operator

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

Page 15: Core java (fundamentals and OOPS concepts)

Control statements

• If , if else

• Switch , case , default ,break

• for , continue , break

• while, continue , break

• do while , continue, break

Page 16: Core java (fundamentals and OOPS concepts)

OOPS

• Abstraction

• Encapsulation

• Inheritance

• Polymorphism

Page 17: Core java (fundamentals and OOPS concepts)

Abstraction and Encapsulation

Page 18: Core java (fundamentals and OOPS concepts)

Abstraction and Encapsulation

Page 19: Core java (fundamentals and OOPS concepts)

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

Page 20: Core java (fundamentals and OOPS concepts)

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.

Page 21: Core java (fundamentals and OOPS concepts)

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

Page 22: Core java (fundamentals and OOPS concepts)

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

}

}

Page 23: Core java (fundamentals and OOPS concepts)

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’

Page 24: Core java (fundamentals and OOPS concepts)

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.

Page 25: Core java (fundamentals and OOPS concepts)

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

Page 26: Core java (fundamentals and OOPS concepts)

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

Page 27: Core java (fundamentals and OOPS concepts)

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.

Page 28: Core java (fundamentals and OOPS concepts)

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

Page 29: Core java (fundamentals and OOPS concepts)

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.

Page 30: Core java (fundamentals and OOPS concepts)

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.

Page 31: Core java (fundamentals and OOPS concepts)

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.

Page 32: Core java (fundamentals and OOPS concepts)

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

Page 33: Core java (fundamentals and OOPS concepts)

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

Page 34: Core java (fundamentals and OOPS concepts)

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

Page 35: Core java (fundamentals and OOPS concepts)

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

Page 36: Core java (fundamentals and OOPS concepts)

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

Page 37: Core java (fundamentals and OOPS concepts)
Page 38: Core java (fundamentals and OOPS concepts)

Not all JVMs are created equal

Page 39: Core java (fundamentals and OOPS concepts)