0% found this document useful (0 votes)
76 views

Java Inner Classes

Java allows classes to be nested within other classes. This is known as inner classes. Inner classes can access fields and methods of their enclosing class. They provide a way to logically group classes that are only used in one place. Inner classes can be declared as private, protected, public, static or final like regular classes.

Uploaded by

Santhosh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Java Inner Classes

Java allows classes to be nested within other classes. This is known as inner classes. Inner classes can access fields and methods of their enclosing class. They provide a way to logically group classes that are only used in one place. Inner classes can be declared as private, protected, public, static or final like regular classes.

Uploaded by

Santhosh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Java 

Inner Classes
Introduction
• In Java, it is also possible to nest classes (a class within a class).
The purpose of nested classes is to group classes that belong
together, which makes your code more readable and
maintainable.
• To access the inner class, create an object of the outer class, and
then create an object of the inner class:
Sample Code for Inner and Outer Classes
class OuterClass {
int x = 10;

class InnerClass {
int y = 5;
}
}

public class Main {


public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);
}
}
Private

Inner Class
Unlike a "regular" class, an inner class can be private or protected. If you don't want outside
objects to access the inner class, declare the class as private:

class OuterClass {
int x = 10;
private class InnerClass {
int y = 5; Main.java:13: error: OuterClass.InnerClass has private access in OuterClass
OuterClass.InnerClass myInner = myOuter.new InnerClass();
} }
^
public class Main {
public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);
}
}
Static Inner Class
class OuterClass {
• An inner class can also be static, which means int x = 10;
that you can access it without creating an
object of the outer class: static class InnerClass {
int y = 5;
}
}

public class Main {


public static void main(String[] args) {
OuterClass.InnerClass myInner = new OuterClass.InnerClass();
System.out.println(myInner.y);
}
}

// Outputs 5
Garbage Collection in Java

• Perform automatic memory management.


• Java programs compile to bytecode that can be run on a Java Virtual
Machine.
• reclaiming the runtime unused memory automatically.
• free() function in C language and delete() in C++. But, in java it is
performed automatically. So, java provides better memory
management.
Advantage of Garbage Collection

• It makes java memory efficient because garbage collector removes


the unreferenced objects from heap memory.
• It is automatically done by the garbage collector(a part of JVM) so
we don't need to make extra efforts.
How can an object be unreferenced?
There are many ways:
•By nulling the reference
•By assigning a reference to another
•By anonymous object etc.
1) By nulling a reference:
Employee e=new Employee();  
e=null;  
2) By assigning a reference to another:
Employee e1=new Employee();  
Employee e2=new Employee();  
e1=e2;//now the first object referred by e1 is available for garbage collection  
3) By anonymous object:
new Employee();  
Inheritance in Java
Introduction
• Inheritance in Java is a mechanism in which one object acquires all
the properties and behaviors of a parent object. It is an important part
of OOPs: (Object Oriented programming system).
• The idea behind inheritance in Java is that you can create new classes
that are built upon existing classes. 
• IS-A relationship which is also known as a parent-child relationship
Terms used in Inheritance

• Class: A class is a group of objects which have common properties. It is a


template or blueprint from which objects are created.
• Sub Class/Child Class: Subclass is a class which inherits the other class.
It is also called a derived class, extended class, or child class.
• Super Class/Parent Class: Superclass is the class from where a subclass
inherits the features. It is also called a base class or a parent class.
• Reusability: As the name specifies, reusability is a mechanism which
facilitates you to reuse the fields and methods of the existing class when
you create a new class. You can use the same fields and methods already
defined in the previous class.
Programmer IS-A
Employee

Java Inheritance: Syntax and Example


class Employee{  
The syntax of Java Inheritance  float salary=40000;  
class Super }  
{ ..... ..... }
class Sub extends Super class Programmer extends Employee{  
{ ..... ..... }
 int bonus=10000;  
 public static void main(String args[]){  
Note: The extends keyword indicates that you are
   Programmer p=new Programmer();  
making a new class that derives from an existing class.
The meaning of "extends" is to increase the    System.out.println("Programmer salary is:"+p.salary);  
functionality.    System.out.println("Bonus of Programmer is:"+p.bonus);
}  
Parent or superclass – Class which is inherited.
Child Class – New Class. }  
Example :
class Calculation {
public class My_Calculation extends Calculation {
int z;
public void multiplication(int x, int y) {
z = x * y;
public void addition(int x, int y) {
System.out.println("The product of the given
z = x + y;
numbers:"+z);
System.out.println("The sum of the given numbers:"+z);
}
}
public static void main(String args[]) {
public void Subtraction(int x, int y) {
int a = 20, b = 10;
z = x - y;
My_Calculation demo = new My_Calculation();
System.out.println("The difference between the given numbers:"+z);
demo.addition(a, b);
}
demo.Subtraction(a, b);
}
demo.multiplication(a, b);
}
}
Types of inheritance in java: Single, Multilevel and Hierarchical.
Single Inheritance Example

• When a class inherits another class, it is known as a single inheritance. In the


example given below, Dog class inherits the Animal class, so there is the single
inheritance.

class Animal{  
void eat(){System.out.println("eating...");}  
}  
class Dog extends Animal{  
void bark(){System.out.println("barking...");}  
}  
class TestInheritance{  
public static void main(String args[]){  
Dog d=new Dog();  
d.bark();  
d.eat();  
}} 
Multilevel Inheritance Example
• When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the
example given below, BabyDog class inherits the Dog class which again inherits the Animal class,
so there is a multilevel inheritance.
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();  
}}  
Hierarchical Inheritance Example
• When two or more classes inherits a single class, it is known as hierarchical inheritance. In the
example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical
inheritance.
class Animal{  
void eat(){System.out.println("eating...");}  
}  
class Dog extends Animal{  
void bark(){System.out.println("barking...");}  
}  
class Cat extends Animal{  
void meow(){System.out.println("meowing...");}  
}  
class TestInheritance3{  
public static void main(String args[]){  
Cat c=new Cat();  
c.meow();  
c.eat();  
//c.bark();//C.T.Error  
}}  
Sample Exercise:
• To write a Java program which implements multiple classes related with inheritance. The program should have a
main class Product, subclasses Book and CD. Also create a sub-class for Book called Scientific. All the Subclass
should inherit the properties of the main class and it can have its own properties also. Observe the inheritance
hierarchy output and display.

 Develop a Java program to define a class called Account which contains two private data elements, an integer
account number and a floating point account balance and three methods:
A constructor that allows the user to set initial values for account number and account balance and a default constructor that prompts for
the input of the values for the above data members.

 A method which reads a character value for transaction type (D for deposit and W for withdrawal), and a floating point value for transaction
amount, and updates account balance.

 A method, which prints on the screen the account number and account balance.

You might also like