In this session you will learn:
Object Orientation
Overloading
Overriding
Constructor
For more information, visit this link: https://www.mindsmapped.com/courses/software-development/online-java-training-for-beginners/
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();
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
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