SlideShare a Scribd company logo
Java/J2EE Programming Training
OOPs Orientation
Page 1Classification: Restricted
Agenda
• Object Orientation
• Overloading
• Overriding
• Constructor
Page 2Classification: Restricted
Objectives
• State the benefits of encapsulation in object oriented design and write
code that implements tightly encapsulated classes and the relationships
"is a" and "has a".
• Write code to invoke overridden or overloaded methods and parental or
overloaded constructors; and describe the effect of invoking these
methods.
• Write code to construct instances of any concrete class including normal
top level classes and nested classes.
Page 3Classification: Restricted
Encapsulation
• Hiding the implementation details of a class behind a public
programming interface is called encapsulation
• Advantage :- Ease of code maintenance and extensibility. You can change
the implementation without causing changes in the calling code
• Keep the instance variables private(or protected if need be) and allow
access only through public methods
Eg:
public class Employee {
private float salary;
public float getSalary() { return salary; }
public void setSalary(float salary)
{ this.salary=salary; } }
Page 4Classification: Restricted
IS-A Relationship
• The IS-A relationship stands for inheritance. In Java it is implemented
using the keyword ‘extends’
Eg:
public class Person { // general code for Person here}
public class Employee extends Person
{ // Employee specific code. Person details are inherited
}
• Here “Employee extends Person” means that “Employee IS-A Person”
Page 5Classification: Restricted
HAS-A Relationship
• If an instance of class A has a reference to an instance of class B, we say
that class A HAS-A B
Eg:
class Manager {
private Secretary s;
}
class Secretary {}
// Here the Manager class can make use of the functionality of the
Secretary class, by delegating work
Page 6Classification: Restricted
Overloading
• Overloading methods have the same name
• They must have different argument lists
• They may have different return types
• They may have different access modifiers
• They may throw different exceptions
• A subclass can overload super class methods
• Constructors can be overloaded
Page 7Classification: Restricted
Overloading Example
Eg:
class A {
int j;
void test() { }
void test(int i) { j=i; } // Overloaded in same class
}
class B extends A {
int test(String s) // Overloaded in subclass
{ System.out.println(s); }
}
Page 8Classification: Restricted
Overriding
• The overriding method must have the same name, arguments and return
type as the overridden method
• The overriding method cannot be less public can the overridden method
• The overriding method should not throw new or broader checked
exceptions
• Polymorphism comes into action during overriding, the method invoked
depends on the actual object type at runtime. If the object belongs to
superclass, superclass method is called and if the object belongs to
subclass, the subclass version is invoked
Page 9Classification: Restricted
Overriding Example
Eg:
class A {
void print() { System.out.println(“Base”); }
}
class B extends A{
void print() { System.out.println(“Derived”); }
public static void main(String args[]) {
A obj=new B();
obj.print(); // “Derived” is printed
}
}
Page 10Classification: Restricted
Polymorphism and Overloading
• Polymorphism comes into play in overriding, but not in overloading
methods
Eg:
class A {
void print() { System.out.println(“Base”); }
}
class B extends A{
void print(String s) { System.out.println(“Derived”); }
public static void main(String args[]) {
A obj=new B();
obj.print(“hello”); // Won’t compile
}
}
Page 11Classification: Restricted
Difference Between Overloaded and Overridden Methods
Overloaded Method Overridden Method
Argument list Must change Must not change
Return type Can change Must not change
Exceptions Can change Can reduce or eliminate. Must
not throw new or broader
checked exceptions.
Access Can change Must not make more restrictive
(can be less restrictive)
Invocation Reference type determines which overloaded version
(based on declared argument types) is selected.
Happens at compile time. The actual method that’s
invoked is still a virtual method invocation that
happens at runtime, but the compiler will already
know the signature of the method to be invoked. So
at runtime, the argument match will already have
been nailed down, just not the actual class in which
the method lives.
Object type (in other words, the
type of the actual instance on the
heap) determines which method
is selected. Happens at runtime.
Page 12Classification: Restricted
Constructor Overloading Example
Eg:
class Base {
Base() {}
Base(int a)
{ System.out.println(a); } //Overloaded constructors
}
class Derived {
Derived(int a, int b){ super(a); }
}
Page 13Classification: Restricted
Extra points to remember..
• Final methods cannot be overridden
• The overloading method which will be used is decided at compile time,
looking at the reference type
• Constructors can be overloaded, but not overridden
• Overridden methods can throw any unchecked exceptions, or narrower
checked exceptions
• Methods with the modifier ‘final’ cannot be overridden
• If a method cannot be inherited, it cannot be overridden
• To invoke the superclass version of an overridden method from the
subclass, use super.method();
Page 14Classification: Restricted
Objective
• For a given class, determine if a default constructor will be created, and if
so, state the prototype of that constructor.
Page 15Classification: Restricted
Constructors
• A constructor is called whenever an object is instantiated
• The constructor name must match the name of the class
• Constructors must not have a return type
• Constructors can have any access modifier, even private
• Constructors can be overloaded, but not inherited
Page 16Classification: Restricted
Invoking Constructors
• To invoke a constructor in the same class, invoke this() with matching
arguments
• To invoke a constructor in the super class, invoke super() with matching
arguments
• A constructor can be invoked only from another constructor
• When a subclass object is created all the super class constructors are
invoked in order starting from the top of the hierarchy
Page 17Classification: Restricted
Default Constructors
• A default constructor is created by the compiler only if you have not
written any other constructors in the class
• The default constructor has no arguments
• The default constructor calls the no argument constructor of the super
class
• The default constructor has the same access modifier as the class
Page 18Classification: Restricted
Compiler-Generated Constructor Code
Class Code (what you type) Compiler-Generated Constructor Code
(In Bold Type)
class Foo { } class Foo {
Foo() { super(); } }
class Foo { Foo() { } } class Foo { Foo() { super(); } }
public class Foo { } class Foo { public Foo() { super(); } }
class Foo { Foo(String s) { } } class Foo { Foo(String s) { super(); } }
class Foo { Foo(String s) { super(); } } Nothing-compiler doesn’t need to insert
anything
class Foo { void Foo() { } } class Foo { void Foo() { }
Foo(){ super(); } }
Page 19Classification: Restricted
Extra points to remember…
• A call to this() or super() can be made only from a constructor and it
should be the first statement in it
• You can either call this() or super(), not both, from the same constructor
• Subclasses without any declared constructors will not compile if the
super class doesn’t have a default constructor
• A method with the same name as the class is not a constructor if it has a
return type, but it is a normal method
• Abstract classes have constructors, but interfaces don’t
Page 20Classification: Restricted
Objective
• Identify legal return types for any method given the declarations of all
related methods in this or parent classes.
Page 21Classification: Restricted
Return Types in Overloading and Overriding
• Return type of the overriding method should match that of the
overridden method
• Return types of overloaded methods can be different, but changing only
the return type is not legal. The arguments should also be different
• A subclass can overload methods in the super class
Ex: class Base {
void callme() {} }
class Derived extends Base {
String callme(int y){return null;}
}
Page 22Classification: Restricted
Primitive Return types
• In a method with a primitive return type, you can return any value or
variable which can be explicitly cast to the declared return type
Eg:
public int fun1() {
char c=‘A’;
return c;}
public int fun2() {
float f=100.45f;
return (int)f;
}
Page 23Classification: Restricted
Extra points to remember…
• Methods which return object reference types are allowed to return a null
value
• Casting rules apply when returning values
• A method that declares a class return type can return any object which is
of the subclass type
• A method that declares an interface return type can return any object
whose class implements the interface
• Nothing should be returned from a function which has void return type
Page 24Classification: Restricted
Thank You

More Related Content

What's hot (17)

PPTX
Packages
Nuha Noor
 
PPT
L7 inheritance
teach4uin
 
PPTX
Polymorphism
Nuha Noor
 
PPTX
‫Chapter3 inheritance
Mahmoud Alfarra
 
PPT
inheritance
Mohit Patodia
 
PPTX
oops concept in java | object oriented programming in java
CPD INDIA
 
PPTX
Dynamic method dispatch
yugandhar vadlamudi
 
PDF
java-06inheritance
Arjun Shanka
 
PPT
02 java basics
bsnl007
 
PPT
Inheritance and polymorphism
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Java session2
Rajeev Kumar
 
PPT
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
Mahmoud Alfarra
 
PPTX
Session 09 - OOP with Java - Part 3
PawanMM
 
PPSX
Review Session - Part -2
Hitesh-Java
 
PPTX
Session 08 - OOP with Java - continued
PawanMM
 
PPTX
‫‫Chapter4 Polymorphism
Mahmoud Alfarra
 
Packages
Nuha Noor
 
L7 inheritance
teach4uin
 
Polymorphism
Nuha Noor
 
‫Chapter3 inheritance
Mahmoud Alfarra
 
inheritance
Mohit Patodia
 
oops concept in java | object oriented programming in java
CPD INDIA
 
Dynamic method dispatch
yugandhar vadlamudi
 
java-06inheritance
Arjun Shanka
 
02 java basics
bsnl007
 
Java session2
Rajeev Kumar
 
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
Mahmoud Alfarra
 
Session 09 - OOP with Java - Part 3
PawanMM
 
Review Session - Part -2
Hitesh-Java
 
Session 08 - OOP with Java - continued
PawanMM
 
‫‫Chapter4 Polymorphism
Mahmoud Alfarra
 

Similar to Java OOPs (20)

PPT
Lecture d-inheritance
Tej Kiran
 
PPTX
Java
nirbhayverma8
 
PPTX
Quick Interview Preparation for C# All Concepts
Karmanjay Verma
 
PPTX
Object Oriented Programming
RatnaJava
 
PPT
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
AshwathGupta
 
PPTX
Ch5 inheritance
HarshithaAllu
 
PPT
Md06 advance class features
Rakesh Madugula
 
PDF
Btech chapter notes batcha ros zir skznzjsbaajz z
bhatiaanushka101
 
PDF
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
markbrianBautista
 
PPTX
Java chapter 5
Abdii Rashid
 
PDF
javainheritance
Arjun Shanka
 
PPTX
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
PPTX
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
PPT
Unit 7 inheritance
atcnerd
 
PPT
Inheritance
abhay singh
 
PPTX
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
PPTX
28c
Sireesh K
 
PPTX
28csharp
Sireesh K
 
PPT
Java inheritance
GaneshKumarKanthiah
 
Lecture d-inheritance
Tej Kiran
 
Quick Interview Preparation for C# All Concepts
Karmanjay Verma
 
Object Oriented Programming
RatnaJava
 
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
AshwathGupta
 
Ch5 inheritance
HarshithaAllu
 
Md06 advance class features
Rakesh Madugula
 
Btech chapter notes batcha ros zir skznzjsbaajz z
bhatiaanushka101
 
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
markbrianBautista
 
Java chapter 5
Abdii Rashid
 
javainheritance
Arjun Shanka
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Unit 7 inheritance
atcnerd
 
Inheritance
abhay singh
 
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
28csharp
Sireesh K
 
Java inheritance
GaneshKumarKanthiah
 
Ad

More from DeeptiJava (13)

PPT
Generating the Server Response: HTTP Status Codes
DeeptiJava
 
PPTX
Java Generics
DeeptiJava
 
PPTX
Java Collection
DeeptiJava
 
PPTX
Java Exception Handling
DeeptiJava
 
PPTX
Java Access Specifier
DeeptiJava
 
PPTX
Java JDBC
DeeptiJava
 
PPTX
Java Thread
DeeptiJava
 
PPTX
Java Inner Class
DeeptiJava
 
PPT
JSP Part 2
DeeptiJava
 
PPT
JSP Part 1
DeeptiJava
 
PPTX
Java I/O
DeeptiJava
 
PPT
Java Hibernate Basics
DeeptiJava
 
PPTX
Introduction to Java
DeeptiJava
 
Generating the Server Response: HTTP Status Codes
DeeptiJava
 
Java Generics
DeeptiJava
 
Java Collection
DeeptiJava
 
Java Exception Handling
DeeptiJava
 
Java Access Specifier
DeeptiJava
 
Java JDBC
DeeptiJava
 
Java Thread
DeeptiJava
 
Java Inner Class
DeeptiJava
 
JSP Part 2
DeeptiJava
 
JSP Part 1
DeeptiJava
 
Java I/O
DeeptiJava
 
Java Hibernate Basics
DeeptiJava
 
Introduction to Java
DeeptiJava
 
Ad

Recently uploaded (20)

PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 

Java OOPs

  • 2. Page 1Classification: Restricted Agenda • Object Orientation • Overloading • Overriding • Constructor
  • 3. Page 2Classification: Restricted Objectives • State the benefits of encapsulation in object oriented design and write code that implements tightly encapsulated classes and the relationships "is a" and "has a". • Write code to invoke overridden or overloaded methods and parental or overloaded constructors; and describe the effect of invoking these methods. • Write code to construct instances of any concrete class including normal top level classes and nested classes.
  • 4. Page 3Classification: Restricted Encapsulation • Hiding the implementation details of a class behind a public programming interface is called encapsulation • Advantage :- Ease of code maintenance and extensibility. You can change the implementation without causing changes in the calling code • Keep the instance variables private(or protected if need be) and allow access only through public methods Eg: public class Employee { private float salary; public float getSalary() { return salary; } public void setSalary(float salary) { this.salary=salary; } }
  • 5. Page 4Classification: Restricted IS-A Relationship • The IS-A relationship stands for inheritance. In Java it is implemented using the keyword ‘extends’ Eg: public class Person { // general code for Person here} public class Employee extends Person { // Employee specific code. Person details are inherited } • Here “Employee extends Person” means that “Employee IS-A Person”
  • 6. Page 5Classification: Restricted HAS-A Relationship • If an instance of class A has a reference to an instance of class B, we say that class A HAS-A B Eg: class Manager { private Secretary s; } class Secretary {} // Here the Manager class can make use of the functionality of the Secretary class, by delegating work
  • 7. Page 6Classification: Restricted Overloading • Overloading methods have the same name • They must have different argument lists • They may have different return types • They may have different access modifiers • They may throw different exceptions • A subclass can overload super class methods • Constructors can be overloaded
  • 8. Page 7Classification: Restricted Overloading Example Eg: class A { int j; void test() { } void test(int i) { j=i; } // Overloaded in same class } class B extends A { int test(String s) // Overloaded in subclass { System.out.println(s); } }
  • 9. Page 8Classification: Restricted Overriding • The overriding method must have the same name, arguments and return type as the overridden method • The overriding method cannot be less public can the overridden method • The overriding method should not throw new or broader checked exceptions • Polymorphism comes into action during overriding, the method invoked depends on the actual object type at runtime. If the object belongs to superclass, superclass method is called and if the object belongs to subclass, the subclass version is invoked
  • 10. Page 9Classification: Restricted Overriding Example Eg: class A { void print() { System.out.println(“Base”); } } class B extends A{ void print() { System.out.println(“Derived”); } public static void main(String args[]) { A obj=new B(); obj.print(); // “Derived” is printed } }
  • 11. Page 10Classification: Restricted Polymorphism and Overloading • Polymorphism comes into play in overriding, but not in overloading methods Eg: class A { void print() { System.out.println(“Base”); } } class B extends A{ void print(String s) { System.out.println(“Derived”); } public static void main(String args[]) { A obj=new B(); obj.print(“hello”); // Won’t compile } }
  • 12. Page 11Classification: Restricted Difference Between Overloaded and Overridden Methods Overloaded Method Overridden Method Argument list Must change Must not change Return type Can change Must not change Exceptions Can change Can reduce or eliminate. Must not throw new or broader checked exceptions. Access Can change Must not make more restrictive (can be less restrictive) Invocation Reference type determines which overloaded version (based on declared argument types) is selected. Happens at compile time. The actual method that’s invoked is still a virtual method invocation that happens at runtime, but the compiler will already know the signature of the method to be invoked. So at runtime, the argument match will already have been nailed down, just not the actual class in which the method lives. Object type (in other words, the type of the actual instance on the heap) determines which method is selected. Happens at runtime.
  • 13. Page 12Classification: Restricted Constructor Overloading Example Eg: class Base { Base() {} Base(int a) { System.out.println(a); } //Overloaded constructors } class Derived { Derived(int a, int b){ super(a); } }
  • 14. Page 13Classification: Restricted Extra points to remember.. • Final methods cannot be overridden • The overloading method which will be used is decided at compile time, looking at the reference type • Constructors can be overloaded, but not overridden • Overridden methods can throw any unchecked exceptions, or narrower checked exceptions • Methods with the modifier ‘final’ cannot be overridden • If a method cannot be inherited, it cannot be overridden • To invoke the superclass version of an overridden method from the subclass, use super.method();
  • 15. Page 14Classification: Restricted Objective • For a given class, determine if a default constructor will be created, and if so, state the prototype of that constructor.
  • 16. Page 15Classification: Restricted Constructors • A constructor is called whenever an object is instantiated • The constructor name must match the name of the class • Constructors must not have a return type • Constructors can have any access modifier, even private • Constructors can be overloaded, but not inherited
  • 17. Page 16Classification: Restricted Invoking Constructors • To invoke a constructor in the same class, invoke this() with matching arguments • To invoke a constructor in the super class, invoke super() with matching arguments • A constructor can be invoked only from another constructor • When a subclass object is created all the super class constructors are invoked in order starting from the top of the hierarchy
  • 18. Page 17Classification: Restricted Default Constructors • A default constructor is created by the compiler only if you have not written any other constructors in the class • The default constructor has no arguments • The default constructor calls the no argument constructor of the super class • The default constructor has the same access modifier as the class
  • 19. Page 18Classification: Restricted Compiler-Generated Constructor Code Class Code (what you type) Compiler-Generated Constructor Code (In Bold Type) class Foo { } class Foo { Foo() { super(); } } class Foo { Foo() { } } class Foo { Foo() { super(); } } public class Foo { } class Foo { public Foo() { super(); } } class Foo { Foo(String s) { } } class Foo { Foo(String s) { super(); } } class Foo { Foo(String s) { super(); } } Nothing-compiler doesn’t need to insert anything class Foo { void Foo() { } } class Foo { void Foo() { } Foo(){ super(); } }
  • 20. Page 19Classification: Restricted Extra points to remember… • A call to this() or super() can be made only from a constructor and it should be the first statement in it • You can either call this() or super(), not both, from the same constructor • Subclasses without any declared constructors will not compile if the super class doesn’t have a default constructor • A method with the same name as the class is not a constructor if it has a return type, but it is a normal method • Abstract classes have constructors, but interfaces don’t
  • 21. Page 20Classification: Restricted Objective • Identify legal return types for any method given the declarations of all related methods in this or parent classes.
  • 22. Page 21Classification: Restricted Return Types in Overloading and Overriding • Return type of the overriding method should match that of the overridden method • Return types of overloaded methods can be different, but changing only the return type is not legal. The arguments should also be different • A subclass can overload methods in the super class Ex: class Base { void callme() {} } class Derived extends Base { String callme(int y){return null;} }
  • 23. Page 22Classification: Restricted Primitive Return types • In a method with a primitive return type, you can return any value or variable which can be explicitly cast to the declared return type Eg: public int fun1() { char c=‘A’; return c;} public int fun2() { float f=100.45f; return (int)f; }
  • 24. Page 23Classification: Restricted Extra points to remember… • Methods which return object reference types are allowed to return a null value • Casting rules apply when returning values • A method that declares a class return type can return any object which is of the subclass type • A method that declares an interface return type can return any object whose class implements the interface • Nothing should be returned from a function which has void return type