SlideShare a Scribd company logo
1 of 25
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

L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritanceteach4uin
 
Polymorphism
PolymorphismPolymorphism
PolymorphismNuha Noor
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritanceArjun Shanka
 
02 java basics
02 java basics02 java basics
02 java basicsbsnl007
 
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
البرمجة الهدفية بلغة جافا - مفاهيم أساسية البرمجة الهدفية بلغة جافا - مفاهيم أساسية
البرمجة الهدفية بلغة جافا - مفاهيم أساسية Mahmoud Alfarra
 
Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3PawanMM
 
Review Session - Part -2
Review Session - Part -2Review Session - Part -2
Review Session - Part -2Hitesh-Java
 
Session 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedSession 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedPawanMM
 
‫‫Chapter4 Polymorphism
‫‫Chapter4 Polymorphism‫‫Chapter4 Polymorphism
‫‫Chapter4 PolymorphismMahmoud Alfarra
 

What's hot (17)

Packages
PackagesPackages
Packages
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
inheritance
inheritanceinheritance
inheritance
 
Java
JavaJava
Java
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
02 java basics
02 java basics02 java basics
02 java basics
 
Inheritance and polymorphism
Inheritance and polymorphism   Inheritance and polymorphism
Inheritance and polymorphism
 
Java session2
Java session2Java session2
Java session2
 
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
البرمجة الهدفية بلغة جافا - مفاهيم أساسية البرمجة الهدفية بلغة جافا - مفاهيم أساسية
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
 
Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3Session 09 - OOP with Java - Part 3
Session 09 - OOP with Java - Part 3
 
Review Session - Part -2
Review Session - Part -2Review Session - Part -2
Review Session - Part -2
 
Session 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedSession 08 - OOP with Java - continued
Session 08 - OOP with Java - continued
 
‫‫Chapter4 Polymorphism
‫‫Chapter4 Polymorphism‫‫Chapter4 Polymorphism
‫‫Chapter4 Polymorphism
 

Similar to Java OOP and Constructor Training

Lecture d-inheritance
Lecture d-inheritanceLecture d-inheritance
Lecture d-inheritanceTej Kiran
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented ProgrammingRatnaJava
 
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.pptAshwathGupta
 
Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class featuresRakesh Madugula
 
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdfmarkbrianBautista
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardHari kiran G
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
 
Unit 7 inheritance
Unit 7 inheritanceUnit 7 inheritance
Unit 7 inheritanceatcnerd
 
Object-oriented Analysis, Design & Programming
Object-oriented Analysis, Design & ProgrammingObject-oriented Analysis, Design & Programming
Object-oriented Analysis, Design & ProgrammingAllan Mangune
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in javaagorolabs
 

Similar to Java OOP and Constructor Training (20)

Lecture d-inheritance
Lecture d-inheritanceLecture d-inheritance
Lecture d-inheritance
 
Java
JavaJava
Java
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
 
Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class features
 
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
javainheritance
javainheritancejavainheritance
javainheritance
 
Unit 4
Unit 4Unit 4
Unit 4
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Unit 7 inheritance
Unit 7 inheritanceUnit 7 inheritance
Unit 7 inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
28csharp
28csharp28csharp
28csharp
 
28c
28c28c
28c
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Object-oriented Analysis, Design & Programming
Object-oriented Analysis, Design & ProgrammingObject-oriented Analysis, Design & Programming
Object-oriented Analysis, Design & Programming
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
 

More from DeeptiJava

Generating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status CodesGenerating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status CodesDeeptiJava
 
Java Collection
Java CollectionJava Collection
Java CollectionDeeptiJava
 
Java Exception Handling
Java Exception HandlingJava Exception Handling
Java Exception HandlingDeeptiJava
 
Java Access Specifier
Java Access SpecifierJava Access Specifier
Java Access SpecifierDeeptiJava
 
Java Inner Class
Java Inner ClassJava Inner Class
Java Inner ClassDeeptiJava
 
Java Hibernate Basics
Java Hibernate BasicsJava Hibernate Basics
Java Hibernate BasicsDeeptiJava
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to JavaDeeptiJava
 

More from DeeptiJava (13)

Generating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status CodesGenerating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status Codes
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Java Collection
Java CollectionJava Collection
Java Collection
 
Java Exception Handling
Java Exception HandlingJava Exception Handling
Java Exception Handling
 
Java Access Specifier
Java Access SpecifierJava Access Specifier
Java Access Specifier
 
Java JDBC
Java JDBCJava JDBC
Java JDBC
 
Java Thread
Java ThreadJava Thread
Java Thread
 
Java Inner Class
Java Inner ClassJava Inner Class
Java Inner Class
 
JSP Part 2
JSP Part 2JSP Part 2
JSP Part 2
 
JSP Part 1
JSP Part 1JSP Part 1
JSP Part 1
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java Hibernate Basics
Java Hibernate BasicsJava Hibernate Basics
Java Hibernate Basics
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 

Recently uploaded

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsAndrey Dotsenko
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 

Recently uploaded (20)

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 

Java OOP and Constructor Training

  • 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