JAVA
SUJIT MAJETY | 2 : OBJECT ORIENTED PROGRAMMING CONCEPTS
Course Description
 Naming Conventions
 Data Types
 Variables
 Data Hiding
 Abstraction
 Encapsulation
 IS-A relationship
 HAS-A relationship
 Method Signature
 Polymorphism
 Static
 Dynamic
 Constructors
 Wrapper Classes
Naming Conventions
 It is a rule to follow while naming an identifier (e.g. class, package, variable, method, etc.).
 By using them readability increases.
 Class name: should begin with uppercase and should be a noun.
 Interface name: should begin with uppercase and should be an adjective.
 Method name: should begin with lowercase and should be a proverb, second word first
character should be capital.
 Variable name: should begin with lowercase, second word first character should be a
capital.
 Package name: should be completely in lowercase.
 Constants name: should be completely in uppercase.
Data Types
 Java supports UNICODE character set. Hence, each character is represented in two bytes.
 In java every variable has a type, every expression has a type and all assignments should be
checked by the compiler for type compatibility. Hence, java is treated as strongly typed
language.
 We are having 8 primitive data types.
 These data types fall under 3 categories.
 Numeric Data types
 Character Data types
 Boolean Data types
Data Types (cont.)
 Numeric Data types
 Integer Data types
 Byte – 1 byte
 Short – 2 bytes
 Int – 4 bytes
 Long – 8 bytes
 Floating point Data types
 Float – 4 bytes
 Double – 8 bytes
 Character Data types
 char – 2 bytes
 Boolean Data Types
 Boolean – 1 bit
Variables
 Variable is name of a memory location.
Types of variables
There are three types of variables
 Local variables
 Variables that are declared inside a method
 Instance variables
 Variables declared in class outside the methods.
 Static variables
 Variables declared in class outside the methods with static keyword.
Data Hiding
 Providing security to the data. ie., No outsider can access the data.
 By using the private modifier, we can implement the data hiding mechanism.
class User
{
private String password = “password”;
}
Abstraction
 Is the process of hiding the unnecessary details and exposing only the essential
features of a particular object.
 Hiding internal implementation details & just highlight the set of services what
we are offering, is called “Abstraction”.
 E.g. Car.
 Logic of car is abstract to you.
 You just know how to use it.
 In the same way java methods
internal details need not be
known to you, still you can use it.
Encapsulation
 Encapsulation is the ability of an object to be a container for related properties and methods.
 E.g. Cars and their owners
 All the functions of cars are encapsulated with owners
 No one else can access it.
class Student{
private name;
public String getName(){
return name;
}
Public void setName(String name){
this.name=name;
}
}
IS-A Relationship
 Is nothing but inheritance
 By using extends keyword we can implement IS-A relationship.
 The main advantage of the IS-A relationship is re-usability.
class A{
public void m1(){
…….
}
class B extends A{
public void m2(){
……...
}
HAS-A Relationship
 HAS-A relation ship is also known as composition or aggregation.
 There is no specific keyword to implement HAS-A relationship. We are using the new
keyword.
 The main advantage of Has-A relationship is Re-usability.
 And one disadvantage of HAS-A relationship is it increases dependency between the
classes and creates maintenance problems.
class car{
Engine e =new Engine();
}
class Engine{
// Engine specific functionality
}
Method Signature
 Method signature consists of name of the method & argument-list.
 The first line written in the method definition is said to be the method signature.
public void m1 (int i, float p)
m1(int ,float)
 The return type is not part of the method signature.
 Compiler will always use method signature while resolving method calls.
 With in the same class two methods with the same signature not allowed other wise we will get
compile time error.
Method Signature (cont..)
class A{
public void m1(int i){
}
public int m1(int i){
}
}
class Test{
public static void main(String[] args){
A a=new A();
}
Compile Error : method m1(int)
is already defined in class Test
Inheritance
 An object of a class acquiring the properties of an object of another class is called
inheritance.
 Uses :
 Method Overloading
 Code Reusability
 Types
 Single
 Multi Level
 Hierarchical
 Multiple
Inheritance (cont..)

Single
Class A
Class B
Class C
Class CClass B Interface C
Interface BInterface AClass A
Multilevel
Hierarchical Multiple
Polymorphism
 Polymorphism is brought up from a greek word which means having more forms.
 There are two types of polymorphism.
 Static Polymorphism
 Dynamic Polymorphism.
 Static polymorphism is achieved with the help of method overloading.
 Dynamic polymorphism is achieved with the help of method over riding.
Static Polymorphism
 Two methods are said to be overloaded iff method names are same but arguments
are different.
 This concept in java simplifies the programming.
 E.g. : System.out.println();
Dynamic Polymorphism
 Method overriding takes place between two IS-A relation dependant classes
having methods with the same signature and same name.
 Method over riding is also known as “runtime polymorphism or dynamic
polymorphism or late binding.
 Over riding method resolution is also known as “Dynamic method dispatch@.
 Rules :
 Method signatures must be matched.
 Parent class final method cannot be over ridden.
 Private methods are not visible in the child classes. Hence over riding concept is not applicable
for private methods.
Differences
Property Overloading Overriding
Arguments Must be different Must be same
Method Signature Must be different Must be same
Return type No restrictions Must be same until 1.4
Private, static & final
methods
Can be overloaded Cannot be overloaded
Access Modifiers No restrictions We can’t decrease scope
Throws Clause No restrictions We can decrease the size and
level for checked exceptions
Method Resolutions Always taken care by
compiler based on reference
type.
Always takes care by jvm
based on runtime object.
Constructors
 A special method in a class, having the same name as class without any return type.
 Constructor is executed when a class is instantiated, i.e. an object is created.
 Constructors cannot be called explicitly.
 Used to initialize an object i.e. initialize instance variables as well as class variables.
 Compiler provides default constructor if the programmer doesn’t provide a constructor.
 If a constructor is specified by the programmer, then compiler doesn’t provide the default
constructor.
 More than one constructor can be specified in a class, this concept is known as constructor
overloading.
 The default constructor initializes the instance variables and class variables with default values.
 To calculate dynamically how much memory is needed by the object, constructor is used.
Wrapper Classes
 Java uses primitive types, such as int, char, double to hold the basic data types
supported by the language.
 Sometimes it is required to create an object representation of these primitive types.
 These classes need to wrap the primitive types in a class.
 To satisfy this need, java provides classes that correspond to each of the primitive
types.
 Basically, these classes encapsulate, or wrap, the primitive types within a class.
 Thus, they are commonly referred to as type wrapper. Type wrapper are classes
that encapsulate a primitive type within an object.
 The wrapper types are Byte, Short, Integer, Long, Character, Boolean, Double,
Float.
Important Interview Questions
 What is hash code?
 How can you find the hash code of an object
 Can you declare class as private? Why?
 When is constructor called? Before or after creating the object?
 What is constructor overloading?
 Difference between float and double?
 Why do we need wrapper classes?
 What is boxing and unboxing?
Questions?

Object Oriented Principles

  • 1.
    JAVA SUJIT MAJETY |2 : OBJECT ORIENTED PROGRAMMING CONCEPTS
  • 2.
    Course Description  NamingConventions  Data Types  Variables  Data Hiding  Abstraction  Encapsulation  IS-A relationship  HAS-A relationship  Method Signature  Polymorphism  Static  Dynamic  Constructors  Wrapper Classes
  • 3.
    Naming Conventions  Itis a rule to follow while naming an identifier (e.g. class, package, variable, method, etc.).  By using them readability increases.  Class name: should begin with uppercase and should be a noun.  Interface name: should begin with uppercase and should be an adjective.  Method name: should begin with lowercase and should be a proverb, second word first character should be capital.  Variable name: should begin with lowercase, second word first character should be a capital.  Package name: should be completely in lowercase.  Constants name: should be completely in uppercase.
  • 4.
    Data Types  Javasupports UNICODE character set. Hence, each character is represented in two bytes.  In java every variable has a type, every expression has a type and all assignments should be checked by the compiler for type compatibility. Hence, java is treated as strongly typed language.  We are having 8 primitive data types.  These data types fall under 3 categories.  Numeric Data types  Character Data types  Boolean Data types
  • 5.
    Data Types (cont.) Numeric Data types  Integer Data types  Byte – 1 byte  Short – 2 bytes  Int – 4 bytes  Long – 8 bytes  Floating point Data types  Float – 4 bytes  Double – 8 bytes  Character Data types  char – 2 bytes  Boolean Data Types  Boolean – 1 bit
  • 6.
    Variables  Variable isname of a memory location. Types of variables There are three types of variables  Local variables  Variables that are declared inside a method  Instance variables  Variables declared in class outside the methods.  Static variables  Variables declared in class outside the methods with static keyword.
  • 7.
    Data Hiding  Providingsecurity to the data. ie., No outsider can access the data.  By using the private modifier, we can implement the data hiding mechanism. class User { private String password = “password”; }
  • 8.
    Abstraction  Is theprocess of hiding the unnecessary details and exposing only the essential features of a particular object.  Hiding internal implementation details & just highlight the set of services what we are offering, is called “Abstraction”.  E.g. Car.  Logic of car is abstract to you.  You just know how to use it.  In the same way java methods internal details need not be known to you, still you can use it.
  • 9.
    Encapsulation  Encapsulation isthe ability of an object to be a container for related properties and methods.  E.g. Cars and their owners  All the functions of cars are encapsulated with owners  No one else can access it. class Student{ private name; public String getName(){ return name; } Public void setName(String name){ this.name=name; } }
  • 10.
    IS-A Relationship  Isnothing but inheritance  By using extends keyword we can implement IS-A relationship.  The main advantage of the IS-A relationship is re-usability. class A{ public void m1(){ ……. } class B extends A{ public void m2(){ ……... }
  • 11.
    HAS-A Relationship  HAS-Arelation ship is also known as composition or aggregation.  There is no specific keyword to implement HAS-A relationship. We are using the new keyword.  The main advantage of Has-A relationship is Re-usability.  And one disadvantage of HAS-A relationship is it increases dependency between the classes and creates maintenance problems. class car{ Engine e =new Engine(); } class Engine{ // Engine specific functionality }
  • 12.
    Method Signature  Methodsignature consists of name of the method & argument-list.  The first line written in the method definition is said to be the method signature. public void m1 (int i, float p) m1(int ,float)  The return type is not part of the method signature.  Compiler will always use method signature while resolving method calls.  With in the same class two methods with the same signature not allowed other wise we will get compile time error.
  • 13.
    Method Signature (cont..) classA{ public void m1(int i){ } public int m1(int i){ } } class Test{ public static void main(String[] args){ A a=new A(); } Compile Error : method m1(int) is already defined in class Test
  • 14.
    Inheritance  An objectof a class acquiring the properties of an object of another class is called inheritance.  Uses :  Method Overloading  Code Reusability  Types  Single  Multi Level  Hierarchical  Multiple
  • 15.
    Inheritance (cont..)  Single Class A ClassB Class C Class CClass B Interface C Interface BInterface AClass A Multilevel Hierarchical Multiple
  • 16.
    Polymorphism  Polymorphism isbrought up from a greek word which means having more forms.  There are two types of polymorphism.  Static Polymorphism  Dynamic Polymorphism.  Static polymorphism is achieved with the help of method overloading.  Dynamic polymorphism is achieved with the help of method over riding.
  • 17.
    Static Polymorphism  Twomethods are said to be overloaded iff method names are same but arguments are different.  This concept in java simplifies the programming.  E.g. : System.out.println();
  • 18.
    Dynamic Polymorphism  Methodoverriding takes place between two IS-A relation dependant classes having methods with the same signature and same name.  Method over riding is also known as “runtime polymorphism or dynamic polymorphism or late binding.  Over riding method resolution is also known as “Dynamic method dispatch@.  Rules :  Method signatures must be matched.  Parent class final method cannot be over ridden.  Private methods are not visible in the child classes. Hence over riding concept is not applicable for private methods.
  • 19.
    Differences Property Overloading Overriding ArgumentsMust be different Must be same Method Signature Must be different Must be same Return type No restrictions Must be same until 1.4 Private, static & final methods Can be overloaded Cannot be overloaded Access Modifiers No restrictions We can’t decrease scope Throws Clause No restrictions We can decrease the size and level for checked exceptions Method Resolutions Always taken care by compiler based on reference type. Always takes care by jvm based on runtime object.
  • 20.
    Constructors  A specialmethod in a class, having the same name as class without any return type.  Constructor is executed when a class is instantiated, i.e. an object is created.  Constructors cannot be called explicitly.  Used to initialize an object i.e. initialize instance variables as well as class variables.  Compiler provides default constructor if the programmer doesn’t provide a constructor.  If a constructor is specified by the programmer, then compiler doesn’t provide the default constructor.  More than one constructor can be specified in a class, this concept is known as constructor overloading.  The default constructor initializes the instance variables and class variables with default values.  To calculate dynamically how much memory is needed by the object, constructor is used.
  • 21.
    Wrapper Classes  Javauses primitive types, such as int, char, double to hold the basic data types supported by the language.  Sometimes it is required to create an object representation of these primitive types.  These classes need to wrap the primitive types in a class.  To satisfy this need, java provides classes that correspond to each of the primitive types.  Basically, these classes encapsulate, or wrap, the primitive types within a class.  Thus, they are commonly referred to as type wrapper. Type wrapper are classes that encapsulate a primitive type within an object.  The wrapper types are Byte, Short, Integer, Long, Character, Boolean, Double, Float.
  • 22.
    Important Interview Questions What is hash code?  How can you find the hash code of an object  Can you declare class as private? Why?  When is constructor called? Before or after creating the object?  What is constructor overloading?  Difference between float and double?  Why do we need wrapper classes?  What is boxing and unboxing?
  • 23.