🔏
Tech
  • 🟢App aspects
    • Software architecture
      • Caching
      • Anti-patterns
      • System X-ability
      • Coupling
      • Event driven architecture
        • Command Query Responsibility Segregation (CQRS)
        • Change Data Capture (CDC)
      • Distributed transactions
      • App dev notes
        • Architecture MVP
      • TEMP. Check list
      • Hexagonal arch
      • Communication
        • REST vs messaging
        • gRPC
        • WebSocket
      • Load balancers
      • Storage limits
      • Event storming
    • Authentication
    • Deployment strategy
  • Databases
    • Classification
    • DB migration tools
    • PostreSQL
    • Decision guidance
    • Index
      • Hash indexes
      • SSTable, LSM-Trees
      • B-Tree
      • Engines, internals
    • Performance
  • System design
    • Interview preparation
      • Plan
        • Instagram
        • Tinder
        • Digital wallet
        • Dropbox
        • Live video streaming
        • Uber
        • Whatsup
        • Tiktok
        • Twitter
        • Proximity service
    • Algorithms
    • Acronyms
  • 🟢Programming languages
    • Java
      • Features
        • Field hiding
        • HashCode() and Equals()
        • Reference types
        • Pass by value
        • Atomic variables
      • Types
      • IO / NIO
        • Java NIO
          • Buffer
          • Channel
        • Java IO: Streams
          • Input streams
            • BufferedInputStream
            • DataInputStream
            • ObjectInputStream
            • FilterInputStream
            • ByteArrayInputStream
        • Java IO: Pipes
        • Java IO: Byte & Char Arrays
        • Java IO: Input Parsing
          • PushbackReader
          • StreamTokenizer
          • LineNumberReader
          • PushbackInputStream
        • System.in, System.out, System.error
        • Java IO: Files
          • FileReader
          • FileWriter
          • FileOutputStream
          • FileInputStream
      • Multithreading
        • Thread liveness
        • False sharing
        • Actor model
        • Singleton
        • Future, CompletableFuture
        • Semaphore
      • Coursera: parallel programming
      • Coursera: concurrent programming
      • Serialization
      • JVM internals
      • Features track
        • Java 8
      • Distributed programming
      • Network
      • Patterns
        • Command
      • Garbage Collectors
        • GC Types
        • How GC works
        • Tools for GC
    • Kotlin
      • Scope functions
      • Inline value classes
      • Coroutines
      • Effective Kotlin
    • Javascript
      • Javascript vs Java
      • TypeScript
    • SQL
      • select for update
    • Python
      • __init.py__
  • OS components
    • Network
      • TCP/IP model
        • IP address in action
      • OSI model
  • 🟢Specifications
    • JAX-RS
    • REST
      • Multi part
  • 🟢Protocols
    • HTTP
    • OAuth 2.0
    • LDAP
    • SAML
  • 🟢Testing
    • Selenium anatomy
    • Testcafe
  • 🟢Tools
    • JDBC
      • Connection pool
    • Gradle
    • vim
    • git
    • IntelliJ Idea
    • Elastic search
    • Docker
    • Terraform
    • CDK
    • Argo CD
      • app-of-app setup
    • OpenTelemetry
    • Prometheus
    • Kafka
      • Consumer lag
  • 🟢CI
    • CircleCi
  • 🟢Platforms
    • AWS
      • VPC
      • EC2
      • RDS
      • S3
      • IAM
      • CloudWatch
      • CloudTrail
      • ELB
      • SNS
      • Route 53
      • CloudFront
      • Athena
      • EKS
    • Kubernetes
      • Networking
      • RBAC
      • Architecture
      • Pod
        • Resources
      • How to try
      • Kubectl
      • Service
      • Tooling
        • ArgoCD
        • Helm
        • Istio
    • GraalVM
    • Node.js
    • Camunda
      • Service tasks
      • Transactions
      • Performance
      • How it executes
  • 🟢Frameworks
    • Hibernate
      • JPA vs Spring Data
    • Micronaut
    • Spring
      • Security
      • JDBC, JPA, Hibernate
      • Transactions
      • Servlet containers, clients
  • 🟢Awesome
    • Нейробиология
    • Backend
      • System design
    • DevOps
    • Data
    • AI
    • Frontend
    • Mobile
    • Testing
    • Mac
    • Books & courses
      • Path: Java Concurrency
    • Algorithms
      • Competitive programming
    • Processes
    • Finance
    • Electronics
  • 🟢Electronics
    • Arduino
    • IoT
  • Artificial intelligence
    • Artificial Intelligence (AI)
  • 🚀Performance
    • BE
  • 📘Computer science
    • Data structures
      • Array
      • String
      • LinkedList
      • Tree
    • Algorithms
      • HowTo algorithms for interview
  • 🕸️Web dev (Frontend)
    • Trends
    • Web (to change)
  • 📈Data science
    • Time series
Powered by GitBook
On this page

Was this helpful?

  1. Programming languages
  2. Java
  3. Features

Field hiding

There is a class hierarchy and on each level there is a field with same name publicName.

public class TestCode {
	
	public static void main(String[] args) {
		GrandPa grandPa = new GrandPa();
		System.out.println("Real gradPa name is :" + grandPa.getPublicName());
		
		GrandPa grandPaFatherActually = new Father();
		System.out.println("gradPa type, but father instance:  " + grandPaFatherActually.getPublicName());
		
		GrandPa grandPaSonActually = new Son();
		System.out.println("gradPa type, but son instance:  " + grandPaSonActually.getPublicName());
	}
	
}

class GrandPa {
	private String publicName = "GrandPaName";
	
	public String getPublicName() {
		return publicName;
	}
}

class Father extends GrandPa {
	private String publicName = "FatherName";
	
	public String getPublicName() {
		return publicName;
	}
}

class Son extends Father{
	private String publicName = "SonName";
	
	public String getPublicName() {
		return publicName;
	}
}
Real gradPa name is :GrandPaName
gradPa type, but father instance:  FatherName
gradPa type, but son instance:  SonName

Fields in subclasses can hide fields with the same name in their superclasses. This means that when you access a field using an instance of a subclass, the field in the subclass takes precedence, hiding the fields with the same name in its superclasses.

In the case of your grandPaSonActually instance, which is an instance of the Son class, the field publicName from the Son class takes precedence. So technically, when you access grandPaSonActually.publicName, you are accessing the publicName field from the Son class, and its value is "SonName." This is known as field hiding.

While debuggers may show you the fields from the entire class hierarchy for inspection purposes, the actual field that is accessible and used when you access publicName on grandPaSonActually is the one defined in the Son class. The fields from the superclasses (Father and GrandPa) are effectively hidden in this context.

So, to summarise, only the publicName field from the Son class ("SonName") is technically available and accessible in the grandPaSonActually instance. The fields with the same name in the superclasses are hidden and not accessible through this instance.

PreviousFeaturesNextHashCode() and Equals()

Last updated 1 year ago

Was this helpful?

🟢