Escolar Documentos
Profissional Documentos
Cultura Documentos
Java provides a number of access modifiers to set access levels for classes,
variables, methods and constructors. The four access levels are:
Example:
Variables and methods can be declared without any modifiers, as in the
following examples:
String version = "1.5.1";
boolean processOrder() {
return true;
}
Example:
The following class uses private access control:
public class Logger {
private String format;
public String getFormat() {
return this.format;
}
public void setFormat(String format) {
this.format = format;
}
}
Example:
The following function uses public access control:
public static void main(String[] arguments) {
// ...
}
Example:
The following parent class uses protected access control, to allow its child
class override openSpeaker() method:
class AudioPlayer {
protected boolean openSpeaker(Speaker sp) {
// implementation details
}
}
class StreamingAudioPlayer {
boolean openSpeaker(Speaker sp) {
// implementation details
}
}
abstractit may or may not include abstract methods. Abstract classes cannot be
Static Methods can access class variables without using object of the class. It can access non-static
methods and non-static variables by using objects. Static methods can be accessed directly in static
and non-static methods.
Example 1: public static void main itself is a static method
class Example5{
static int i;
static String s;
public static void main(String args[]) //Its a Static Method
{
Example5 obj=new Example5();
//Non Static variables accessed using object obj
System.out.println("i:"+obj.i);
System.out.println("s:"+obj.s);
}
}
Output:
i:0
s:null
Static Variables
Data stored in static variables is common for all the objects( or instances ) of that Class.
Memory allocation for such variables only happens once when the class is loaded in the
memory.
These variables can be accessed in any other class using class name.
Unlike non-static variables, such variables can be accessed directly in static and non-static
methods.
Output:
Var1 is:0
Var2 is:null
static is an important keyword in Java. Static can be applied to inner classes, variables and
methods. static belongs to Class in Java and value of static variable will always be same for all
instance of that Class. static methods are also binded at compile time as opposed to non static
method which are binded at runtime.
Java static keyword can be used in five cases as shown in below image.
Q.2 Constructor
Constructor in Java is block of code which is executed at the time of Object creation. But other than getting called,
Constructor is entirely different than methods and has some specific properties like name of constructor must be
same as name of Class. Constructor also can not have any return type, constructors are automatically chained by
using this keyword and super. Since Constructor is used to create object, object initialization code is normally hosted
in Constructor. Similar to method you can also overload constructor in Java. In this Java tutorial we will some
important points about constructor in Java which is worth remembering for any Java programmer. Its also worth
remember that any static initializer block is executed before constructor because they are executed when class is
loaded into memory while constructors are executed when you create instance of any object e.g.
using new() keyword.
here the problem comes Because D is extending both B & C so if D wants to use
the same method which method would be called (the overridden method of B or the overridden
method of C). Ambiguity. Thats the main reason why Java doesnt support multiple inheritance.
{
public void myMethod();
}
class Demo implements X, Y
{
public void myMethod()
{
System.out.println(" Multiple inheritance example using interfaces");
}
}
Q.4)single Inheritance
Single inheritance is damn easy to understand. When a class extends another one
class only then we call it a single inheritance. The below flow diagram shows that
class B extends only one class which is A. Here A is a parent classof B and B would be
a child class of A.
Class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
}
2) Multiple Inheritance
Multiple Inheritance refers to the concept of one class extending (Or inherits)
more than one base class. The inheritance we learnt earlier had the concept of one
base class or parent. The problem with multiple inheritance is that the derived
class will have to manage the dependency on two base classes.
Note 1: Multiple Inheritance is very rarely used in software projects. Using Multiple
inheritance often leads to problems in the hierarchy. This results in unwanted
complexity when further extending the class.
Note 2: Most of the new OO languages like Small Talk, Java, C# do not support
Multiple inheritance. Multiple Inheritance is supported in C++.
3) Multilevel Inheritance
Multilevel inheritance refers to a mechanism in OO technology where one can inherit
from a derived class, thereby making this derived class the base class for the new
class. As you can see in below flow diagram C is subclass or child class of B and B is a
child class of A. For more details and example refer Multilevel inheritance in Java.
}
Class Z extends Y
{
public void methodZ()
{
System.out.println("class Z method");
}
public static void main(String args[])
{
Z obj = new Z();
obj.methodX(); //calling grand parent class method
obj.methodY(); //calling parent class method
obj.methodZ(); //calling local method
}
}
4) Hierarchical Inheritance
In such kind of inheritance one class is inherited by many sub classes. In below
example class B,C and D inherits the same class A. A is parent class (or base class) of
B,C & D. Read More at Hierarchical Inheritance in java with example program.
5) Hybrid Inheritance
In simple terms you can say that Hybrid inheritance is a combination
of Singleand Multiple inheritance. A typical flow diagram would look like below. A