Skip to content

Instantly share code, notes, and snippets.

View Sottti's full-sized avatar
🏠
Remote

Pablo Costa Sottti

🏠
Remote
View GitHub Profile
@Sottti
Sottti / MainActivity.java
Last active November 7, 2019 09:20
Navigation Drawer Sizing according to Material Design
{...}
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
// OverridenFromAnotherFile.kt
// Error: getAge() is not visible because is protected
val userAge = User().getAge()
// Error: getAge() is not visible because inherits the protected modifier
val moderatorAge = Moderator().getAge()
// getAge() is visible because the modifier is declared explicitly as public
val staffAge = Staff().getAge()
// Overridden.kt
public open class User {
protected open fun getAge() = 16
}
public class Moderator : User() {
// getAge() inherits the protected modifier as default
override fun getAge() = 18
}
// User is public
// The User primary constructor is private
class User private constructor(id : Int) {
// ...
}
// Moderator is internal
// The Moderator primary constructor is private
internal Moderator private constructor(id : Int) {
// ...
open class User(val userId: Int, val userName: String)
class Editor(userId: Int, userName: String, val rank: Int) : User(userId, userName)
class Moderator(userId: Int, userName: String, val type: Int) : User(userId, userName)
class Staff(userId: Int, userName: String, val department: String) : User(userId, userName)
@Sottti
Sottti / Public.kt
Last active December 3, 2018 07:20
// Public.kt file
// Visible everywhere
public const val numberThree = 3
// Visible everywhere
public open class User() {
// Visible to everyone with visibility on the User class
public val numberEight = numberThree.plus(5)
}
// Internal.kt file
// Visible to everyone in the same module
internal const val numberThree = 3
// Visible to everyone in the same module
internal open class User() {
// Visible to everyone in the same module that has visibility on User
internal val numberEight = numberThree.plus(5)
}
// AnotherFile.kt file
// numberThree is visible to any other file because it's public
// numberEight is visible to any other file because User and numberEight are public
// numberEleven is visible to any other file because Moderator and numberEleven are public
private const val numberTwentyTwo = numberThree + User().numberEight + Moderator().numberEleven
// Protected.kt file
// Visible inside this file
private const val numberThree = 3
// Visible inside this file
private open class User() {
// Visible inside the User class and subclasses
protected val numberEight = numberThree.plus(5)
}
// SameModule.kt file
// numberThree is visible because this file is in the same module than Internal.kt
// numberEight is visible because this file is in the same module than User
private const val numberEleven = numberThree.plus(User().numberEight)