Beruflich Dokumente
Kultur Dokumente
class Car extends Vehicle {
private String modelName = "Mustang"; // Car attribute
public static void main(String[] args) {
// Call the honk() method (from the Vehicle class) on the myCar object
myCar.honk();
// Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class
System.out.println(myCar.brand + " " + myCar.modelName);
}
}
Extend constructors - super
• The super keyword in java is a reference variable that is used to refer
parent class objects.
• 1. Use of super with variables: This scenario occurs when a derived class and
base class has same data members. In that case there is a possibility of
ambiguity for the JVM
Example
• /* Base class vehicle */ • /* Driver program to test */
• class Vehicle
•{
• class Test
• int maxSpeed = 120; •{
•} • public static void main(String[] args)
• /* sub class Car extending vehicle */
• class Car extends Vehicle • {
•{ • Car small = new Car();
• int maxSpeed = 180;
• small.display();
• void display()
• { • }
• /* print maxSpeed of base class (vehicle) */ •}
• System.out.println("Maximum Speed: " + super.maxSpeed);
• }
• both base class and subclass have a member
•} maxSpeed. We could access maxSpeed of
• base class in sublcass using super keyword.
2. Use of super with methods: This is used when we
want to call parent class method. So whenever a parent
and child class have same named methods then to
resolve ambiguity we use super keyword. This code
snippet helps to understand the said usage of super
keyword.
• class Person • void display()
• { • {
• void message() • // will invoke or call current class message() method
• { • message();
• System.out.println("This is person class"); • // will invoke or call parent class message() method
• } • super.message();
• } • }
• /* Subclass Student */ • }
• class Student extends Person • class Test
• { • {
• void message() • public static void main(String args[])
• { • {
• System.out.println("This is student class"); • Student s = new Student();
• } • // calling display() of Student
• /* Base class Person */ • s.display();
• // Note that display() is only in Student class • }
• }
3. Use of super with constructors: super keyword can also be
used to access the parent class constructor. One more important
thing is that, ‘’super’ can call both parametric as well as non
parametric constructors depending upon the situation. Following is
the code snippet to explain the above concept:
• class Person • /* Driver program to test*/
• { • class Test
• Person() • {
• { • public static void main(String[] args)
• System.out.println("Person class Constructor"); • {
• } • Student s = new Student();
• } • }
• /* subclass Student extending the Person class */ • }
• class Student extends Person
• {
• Student()
• {
• // invoke or call parent class constructor
• super();
• System.out.println("Student class Constructor");
• }
• }
Private Access Modifier - Private
• Methods, variables, and constructors that are declared private can
only be accessed within the declared class itself.
• Private access modifier is the most restrictive access level. Class and
interfaces cannot be private.
• Variables that are declared private can be accessed outside the class,
if public getter methods are present in the class.
• Using the private modifier is the main way that an object
encapsulates itself and hides data from the outside world.
• public class Logger {
• private String format;
void wait(long timeout)Causes the current thread to wait until either another thread invokes the notify()
method or the notifyAll() method for this object, or a specified amount of time has elapsed.
void wait(long timeout, int nanos)Causes the current thread to wait until another thread invokes the
notify() method or the notifyAll() method for this object, or some other thread interrupts the current
thread, or a certain amount of real time has elapsed.
Interfaces
• An interface is a reference type in Java. It is similar to class. It is a
collection of abstract methods. A class implements an interface,
thereby inheriting the abstract methods of the interface.
• Writing an interface is similar to writing a class. But a class describes
the attributes and behaviors of an object. And an interface contains
behaviors that a class implements.
• Unless the class that implements the interface is abstract, all the
methods of the interface need to be defined in the class.
Only in java 8
• An interface is similar to a class in the following ways −
• An interface can contain any number of methods.
• An interface is written in a file with a .java extension, with the name of the interface
matching the name of the file.
• The byte code of an interface appears in a .class file.
• Interfaces appear in packages, and their corresponding bytecode file must be in a directory
structure that matches the package name.
• However, an interface is different from a class in several ways, including −
• You cannot instantiate an interface.
• An interface does not contain any constructors.
• All of the methods in an interface are abstract.
• An interface cannot contain instance fields. The only fields that can appear in an interface
must be declared both static and final.
• An interface is not extended by a class; it is implemented by a class.
• An interface can extend multiple interfaces.
• A class uses the implements keyword to • public void travel() {
implement an interface. • System.out.println("Mammal travels");
• interface Animal { • }
• public void eat(); • public int noOfLegs() {
• public void travel(); • return 0;
•} • }
• public static void main(String args[]) {
• /* File name : MammalInt.java */ • MammalInt m = new MammalInt();
• public class MammalInt implements Animal { • m.eat();
• public void eat() { • m.travel();
• System.out.println("Mammal eats"); • }
• } •}
Declaring Interface
• The interface keyword is used to • Interfaces have the following
declare an interface. properties −
• An interface is implicitly abstract.
• import java.lang.*; You do not need to use
the abstract keyword while
declaring an interface.
• public interface NameOfInterface {
• Each method in an interface is also
• // Any number of final, static fields implicitly abstract, so the abstract
• // Any number of abstract method keyword is not needed.
declarations\ • Methods in an interface are
•} implicitly public.
Extending Interfaces
• An interface can extend another interface • // Filename: Football.java
in the same way that a class can extend • public interface Football extends Sports {
another class. The extends keyword is used • public void homeTeamScored(int points);
to extend an interface, and the child • public void visitingTeamScored(int points);
interface inherits the methods of the • public void endOfQuarter(int quarter);
parent interface.
•}
• // Filename: Hockey.java
• // Filename: Sports.java • public interface Hockey extends Sports {
• public interface Sports { • public void homeGoalScored();
• public void setHomeTeam(String name); • public void visitingGoalScored();
• public void endOfPeriod(int period);
• public void setVisitingTeam(String name);
• public void overtimePeriod(int ot);
•} •}
Maker Interface
• t is an empty interface (no field or methods). Examples of marker
interface are Serializable, Clonnable and Remote interface. All these
interfaces are empty interfaces.
• Cloneable interface : Cloneable interface is present in java.lang
package. There is a method clone() in Object class. A class that
implements the Cloneable interface indicates that it is legal for clone()
method to make a field-for-field copy of instances of that class.
Invoking Object’s clone method on an instance of the class that does
not implement the Cloneable interface results in an exception
CloneNotSupportedException being thrown. By convention, classes that
implement this interface should override Object.clone() method.
• // By implementing Cloneable interface • throws CloneNotSupportedException
• // we make sure that instances of class A • {
• return super.clone();
• // can be cloned.
• }
• class A implements Cloneable
•}
•{ • public class Test
• int i; •{
• String s; • public static void main(String[] args)
• // A class constructor • throws CloneNotSupportedException
• public A(int i,String s) • {
• { • A a = new A(20, “Input");
• // cloning 'a' and holding
• this.i = i;
• // new cloned object reference in b
• this.s = s;
• // down-casting as clone() return type is Object
• } • A b = (A)a.clone();
• // Overriding clone() method • System.out.println(b.i);
• // by simply calling Object class • System.out.println(b.s);
• // clone() method. • }
• protected Object clone() •}
Serializable interface
• Serializable interface : Serializable interface is present in java.io
package. It is used to make an object eligible for saving its state into a
file. This is called Serialization.
Classes that do not implement this interface will not have any of their
state serialized or deserialized. All subtypes of a serializable class are
themselves serializable.
Remote interface
• Remote interface : Remote interface is present in java.rmi package. A
remote object is an object which is stored at one machine and
accessed from another machine. So, to make an object a remote
object, we need to flag it with Remote interface. Here, Remote
interface serves to identify interfaces whose methods may be invoked
from a non-local virtual machine.Any object that is a remote object
must directly or indirectly implement this interface. RMI (
Remote Method Invocation) provides some convenience classes that
remote object implementations can extend which facilitate remote
object creation.