UNIT - II
INHERITANCE, PACKAGES AND
INTERFACES
INHERITANCE
• One class is allowed to inherit the
features(fields and methods) of another class.
• Inheritance means creating new classes based
on existing ones.
3.By changing the Order of the parameters
class Geek {
public void geekIdentity(String name, int id)
{
System.out.println("geekName :" + name + " "
+ "Id :" + id);
}
public void geekIdentity(int id, String name)
{
System.out.println("Id :" + id + " "
+ "geekName :" + name);
}
}
• Type Promotion: When a data type of smaller size is
promoted to the data type of bigger size than this is called type
promotion, for example: byte data type can be promoted to
short, a short data type can be promoted to int, long, double
etc.
Type Promotion in Method Overloading:
• One type is promoted to another implicitly if no matching
datatype is found.
Type Promotion Table:
• The data type on the left side can be promoted to the any
of the data type present in the right side of it.
• byte → short → int → long → double short → int → long
→ float → double int → long → float → double
• float → double
• long → float → double
• char → int → long → float → double
Object as Parameter
• When passing an object as a parameter to a
method, a reference to the object is passed
rather than a copy of the object itself.
• This means that any modifications made to
the object within the method will have an
impact on the original object.
public class MyClass {
private int attribute1;
private String attribute2;
private double attribute3;
// Constructor
public MyClass(int attribute1, String attribute2, double attribute3)
{
this.attribute1 = attribute1;
this.attribute2 = attribute2;
this.attribute3 = attribute3;
} // Method with object as parameter
public void myMethod(MyClass obj)
{
// block of code to define this method
}
// More methods
}
Returning Objects
• Methods in Java can return objects, allowing
for flexible and reusable code.
class Calculator {
int result;
Calculator(int initialValue) {
this.result = initialValue;
}
public Calculator add(int value) {
this.result += value;
return this;
}
public Calculator subtract(int value) {
this.result -= value;
return this;
}
}
public class ObjectReturningExample
{
public static void main(String[] args) {
Calculator calculator = new Calculator(10);
int finalResult = calculator.add(5).subtract(3).result;
System.out.println("Final result: " + finalResult);
}
}
• Member Inner Class A class created within
class and outside method.
• Anonymous Inner Class A class created for
implementing an interface or extending class.
The java compiler decides its name.
Advantage of inner classes
• Nested classes represent a particular type of
relationship that is it can access all the members
(data members and methods) of the outer
class, including private.
• Nested classes are used to develop more
readable and maintainable code because it
logically group classes and interfaces in one place
only.
• Code Optimization: It requires less code to write.
Inheritance
• Inheritance is a process of deriving a new
class from existing class, also called as
“extending a class”.
• When an existing class is extended, the new
(inherited) class has all the properties and
methods of the existing class and also
possesses its own characteristics.
SINGLE INHERITANCE
• The process of creating only one subclass from
only one super class is known as Single
Inheritance.
• Only two classes are involved in this inheritance.
• The subclass can access all the members of
super class.
1 . class Animal
2. {
3. void eat()
4. {
5. System.out.println("eating..."); 6. }
7. }
8. class Dog extends Animal
9. {
10. void bark()
11. {
12. System.out.println("barking...");
13. }
14.}
15.class TestInheritance
16.{
17. public static void main(String args[])
18. {
19.Dog d=new Dog();
20.d.bark();
21.d.eat();
22. }
23.}
2. MULTILEVEL INHERITANCE:
– The process of creating a new sub class from an
already inherited sub class is known as
Multilevel Inheritance.
– Multiple classes are involved in inheritance, but
one class extends only one.
– The lowermost subclass can make use of all its
super classes' members.
– Multilevel inheritance is an indirect way of
implementing multiple inheritance.
– Example: Animal à Dog à BabyDog
class Animal
{
void eat()
{
System.out.println("eating..."); }
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
}
}
class TestInheritance2
{
public static void main(String args[]) {
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
29. }}
3. HIERARCHICAL INHERITANCE
• The process of creating more than one sub
classes from one super class is called
Hierarchical Inheritance.
Example
1.class Animal
2. {
3. void eat()
4. {
5. System.out.println("eating...");
6.}
7. }
8. class Dog extends Animal
9. {
10.void bark()
11. {
12.System.out.println("barking...");
13.}
14.}
15.class Cat extends Animal
16.{
17.void meow()
18.{
19.System.out.println("meowing...");
20.}
}
class TestInheritance3
{
public static void main(String args[])
{
Cat c=new Cat();
c.meow();
c.eat();
c.bark();
}
}
PROTECTED MEMBER
• The private members of a class cannot be
directly accessed outside the class.
• Only methods of that class can access the
private members directly.
• if a member of a superclass needs to be
(directly) accessed in a subclass then you must
declare that member protected.
CONSTRUCTORS IN SUB – CLASSES
• constructor of base class with no argument gets
automatically called in derived class constructor.
• Constructors are called in order of derivation,
from superclass to subclass.
• Because a superclass has no knowledge of any
subclass, any initialization it needs to perform is
separate from and possibly prerequisite to any
initialization performed by the subclass.
• Therefore, it must be executed first.
“super” keyword
• Super is a special keyword that directs the compiler to
invoke the superclass members.
• It is used to refer to the parent class of the class in
which the keyword is used.
super keyword is used for the following three purposes:
• To invoke superclass constructor.
• To invoke superclass members variables.
• To invoke superclass methods.