java nested_Java Nested Class

Normal class(Non-nested class) can only be public, abstract or final. For nested classes, static is allowed.

Nested classes(private, public, protected) -> 2 Categories.

1. Static Nested Class (Very Different from C# static class)

alpha: Have access to all the static members of its parent class.

beta: Can instantiate objects by new ParentClass.StaticNestClass(). Now allowed to create objects in C#.

gamma: Can have non-static members.

delta: same as gamma. Cannot access ParentClass.this.

2. Non-static nested class(Inner Class)

alpha: Have access to all the members of its parent class.

beta: Can instantiate objects by new ParentClass().new InnerClass().

gamma: Cannot have static members, except for final static fields.

delta: Variable shadowing:

ParentClass.this.memberName -> Parent class member.

this.memberName, ParentClass.InnerClass.this.memberName  -> Inner class member

======================================

1. Interface can only be defined inside a top-level class, an interface or in static context because interfaces are inherently static.

2. Anonymous Classes:

Interface/AbstractClass obj = new Interface/AbstractClass(Empty for interface. Initialize for abstract class){

String/int ...;

public void ImplementAbstractMethodA(){...}

public void ImplementAbstractMethodB(){...}

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值