Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7528465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:23:18+00:00 2026-05-30T04:23:18+00:00

I’m a newcomer of OOP, so I has one silly question about when a

  • 0

I’m a newcomer of OOP, so I has one silly question about when a class extends another class.

Here my example:

public class Test {
    public Monitor getMonitor(){
        return new LCD();
    }

    class LCD extends Monitor {   NO-ERROR
    class LCD {                   ERROR at line `return new LCD` 
        //some other method or function not in Monitor Class. Example:
        boolean isSamsung;
        public LCD whatkindLCD(){           
        }       
    }
}

I have one question for above code : because LCD is extended from Monitor and LCD has some other properties/methods that Monitor does not. So, LCD is child of Monitor, right ?

It means you try to put a “big box” to a “small box”. So, why when I return new LCD, Eclipse don’t notice error as when I just use class LCD {.

Thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:23:19+00:00Added an answer on May 30, 2026 at 4:23 am

    Understand Inheritance as a “is a” relationship. Here is a simple example I used to understand inheritance during my novice years.

    class Employee
    {
        String name;
        int salary;
    
        Employee()
        {
            name = "Employee";
            salary = 5000;
        }
        public String getName()
        {
            return name;
        }
        public int getSalary()
        {
            return salary;
        }
    }
    class Manager extends Employee
    {
        int bonus;
        int salary;
    
        Manager()
        {
            bonus = 1000;
            salary = 6000;
        }
        public int getBonus()
        {
            return bonus;
        }
        public int getSalary()
        {
            return salary;
        }   
    }
    
    class Test
    {        
        public static void main(String[] args)
        {
            Employee e = new Employee();
            System.out.println(e.getName());
            //System.out.println(e.getBonus());
            System.out.println(e.getSalary());
    
            System.out.println();
    
            Manager m = new Manager();
            System.out.println(m.getName());
            System.out.println(m.getBonus());
            System.out.println(m.getSalary());
    
            System.out.println();
    
            Employee em = new Manager();
            System.out.println(em.getName());                   
            //System.out.println(em.getBonus());            
            System.out.println(((Manager)em).getBonus());
            System.out.println(em.getSalary());     
            }
    }
    

    Compiler looks for reference type before calling any operations on it.
    em.getBonus() doesn’t work because Employee doesn’t have a bonus method.
    But using a cast we can make it work.
    ((Manager)em.)getBonus()

    Reason why compiler looks for the reference type before calling any operation on it is as follows:

    Manager[] managers = new Manager[10];

    It is legal to convert this array to an Employee[] array:

    Employee[] staff = managers; // OK

    Sure, why not, you may think. After all, if manager[i] is a Manager, it is also an Employee. But actually, something surprising is going on. Keep in mind that managers and staff are references to the same array.

    Now consider the statement

    staff[0] = new Employee(“John Eipe”, …);

    The compiler will cheerfully allow this assignment.
    But staff[0] and manager[0] are the same reference, so it looks as if we managed to smuggle a mere employee into the management
    ranks.

    That would be very bad — calling managers[0].setBonus(1000) would try to access a
    nonexistent instance field and would corrupt neighboring memory.
    To make sure no such corruption can occur, all arrays remember the element type with
    which they were created, and they monitor that only compatible references are stored into
    them. For example, the array created as new Manager[10] remembers that it is an array of
    managers. Attempting to store an Employee reference causes an ArrayStoreException.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.