Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to access child class variable in parent class in java

In simple words:

- the left side before "=" means what the object may do, 
- the right side after "=" means how an object will do.

So:

public class Parent {
    private String someVar = "Parent variable";
    public void someMethod(){
        System.out.println(someVar);
    }
}

public class Child extends Parent{
    protected String someVar = "Child variable";
    public void someMethod() {
        System.out.println(someVar);
    }
}

public class Main {
    public static void main(String[] args) {
        Parent p = new Child();
        p.someMethod();
        Parent p2 = new Parent();
        p2.someMethod();
    }
}

out:
Child variable
Parent variable
Comment

how to call child class method from parent class in java

//Hope it helps
//This is the child class
public class child
{
    public classCall()
    {
    	String str = "I am the child class accessed from parent class!!!";
    }
}
//this the parent class
class parent
{
	public static void main(String args[])
    {
    	child obj = new child(); //objext of child class is created
    	System.out.println(obj.classCall()); //'classCall method is called from child class
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to add a number to the ascii value of a char in java 
Java :: javadoc link 
Java :: java, how to find the most repeated character 
Java :: springboot avoid generated security password: 
Java :: error: incompatible types: NonExistentClass cannot be converted to Annotation 
Java :: how to stop screen going off android studio 
Java :: java transaction example 
Java :: java 8 stream add to list 
Java :: Java array repeating 
Java :: $950 at 6% per annum for three years. 
Java :: java forcing user to input int 
Java :: como pegar o valor de um campo de texto swing 
Sql :: mysql set safe mode off 
Sql :: count of tables in database mysql 
Sql :: postgres get columns names 
Sql :: Port 5432 is already in use Usually this means that there is already a PostgreSQL server running on your Mac. If you want to run multiple servers simultaneously, use different ports. 
Sql :: postgresql server restart 
Sql :: print in pl sql 
Sql :: oracle create table comment 
Sql :: alembic.util.exc.CommandError: Target database is not up to date. 
Sql :: SQLSTATE[HY000] [1049] Unknown database 
Sql :: sql server: query to find out all the places where the table is used 
Sql :: sqlite rename column 
Sql :: mysql get last row 
Sql :: how to unlock table in mysql 
Sql :: mysql get last day of month 
Sql :: revokeprivileges mysql 
Sql :: mysql your password does not satisfy the current policy requirements 
Sql :: tsql create unique index composite 
Sql :: how to delay stored procedure execution in sql server 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =