Skip to content
This repository was archived by the owner on Nov 5, 2023. It is now read-only.

Latest commit

 

History

History
170 lines (143 loc) · 2.89 KB

File metadata and controls

170 lines (143 loc) · 2.89 KB

Notes (Sep 6, 2022)

Topics

  • Nested class
  • Anonymous object and class
  • Abstract class

General Notes

  • Class can be prefixed with static.
  • In C, and C++ we can define function after main, and we will get a compilation error. Similarly in Java, it considers it as an undefined data member.
  • Abstract class:-
  • Memory isn't allocated in class definition unless static data member as class is a blueprint. And memory can only be allocated by an Object.

Nested class

  • A class which is defined in another class.
  • There are 2 ways to create a nested class: Static and non-static nested class.
  • Nested Scopes:
{ //outer scope
	int a=12; //inner scope
	{
		int b=14;
	}
}

Inner class

  • Inner class can be static, and it can only access the Static data member and static member function of the outer class.
  • It is a non-static nested class or static inner class.

Anonymous object and class

  • Object created without a name.
  • Anonymous object is very commonly used in object classes.
  • Anonymous inner class is a class created without a name.
\*Sample*\
class Student{

}
class StudentDemo{
	public static void main(){
			Student s=new Student();
			s.display(); 
			new Student(); //anonymous class
	}
}

Abstract class

  • In abstract class, we no need to create an IIB block.

Program 1: Invoking object in Outer class

class Outer{
	int a;
	class Inner{
		void display(){
			System.out.println("Inner class...");
		}
	}
	Outer(int a){
		this.a=a;
	}
	void display(){
		System.out.println("outer...");
		Inner in=new Inner(); //object created
/*If the object is created inside a function the scope is only inside it.*/
		in.display();
	}
}
class OuterDemo{
	public static void main(String args[]){
		Outer out=new Outer(10);
		out.display();
		
	}
}
Output:
outer...
Inner class...

Program 2: Using abstract class

abstract class Inner{
	abstract void display(); //abstract class forces inheritance
}
class Outer{
	int a;
	
	Outer(int a){
		this.a=a;
	}
	void display(){
		System.out.println("Outer class...");
		new Inner(){
			void display(){
				System.out.println("Inner class..");
			}
		}.display();
		//new Student().display();
	}
}
class OuterDemo{
	public static void main(String args[]){
		Outer out=new Outer(10);
		out.display();
	}
}
Output:
Outer class...
Inner class..
  • Continuation:
abstract class Inner{
	//abstract void display(); 
}
class Outer{
	int a;
	
	Outer(int a){
		this.a=a;
	}
	abstract class Inner{}
	void display(){
		System.out.println("Outer class...");
		new Inner(){
			int b;
			{
				b=10;
			}
			void display(){
				System.out.println("Inner class.."+b);
			}
		}.display();
		//new Student().display();
	}
}
class OuterDemo{
	public static void main(String args[]){
		Outer out=new Outer(10);
		out.display();
	}
}
Output:
Outer class...
Inner class..10