For example, when you declare two fields of different types (e.g. Multiple inheritances are not allowed in Java (a class can’t extend more than one class).īesides the four main principles of OOP, Java also works with three further concepts (association, aggregation, composition) you can make use of when designing your programs. Aggregation is a special form of association, while composition is a special form of aggregation.Īssociation simply means the act of establishing a relationship between two unrelated classes.Multilevel inheritance is allowed in Java (a child class can have its own child class as well).Implements the DRY (Don’t Repeat Yourself) programming principle.A class (child class) can extend another class (parent class) by inheriting its features.Java uses the extends keyword to implement the principle of inheritance in code. Parent classes are also called super classes or base classes, while child classes are known as sub classes or derived classes as well. It can also add new data and functionality to its parent. The child class can override the values and methods of the parent class, however it’s not necessary. Inheritance makes it possible to create a child class that inherits the fields and methods of the parent class. Example of dynamic polymorphism in Java is method overriding.Example of static polymorphism in Java is method overloading.All Java objects can be considered polymorphic (at the minimum, they are of their own type and instances of the Object class).Different methods of the same name can be called from the object.The same method name is used several times.Method overriding occurs when the child class overrides a method of its parent. When they are called, they are differentiated by the number, order, and types of their parameters. Method overloading happens when various methods with the same name are present in a class. In Java, polymorphism can take two forms: method overloading and method overriding. Polymorphism refers to the ability to perform a certain action in different ways. Setter methods let us change the value of the field.Each field has a getter and setter method.Restricts direct access to data members (fields) of a class.Java Beans are examples of fully encapsulated classes. You can implement encapsulation in Java by keeping the fields (class variables) private and providing public getter and setter methods to each of them. As its name suggests, it safeguards the internal contents of a class like a real-life capsule. Total abstraction (100%) can be achieved with interfaces.Įncapsulation allows us to protect the data stored in a class from system-wide access.Partial abstraction (0-100%) can be achieved with abstract classes.Gives flexibility to programmers to change the implementation of the abstract behavior.Presents only the signature of internal functionality.
0 Comments
Leave a Reply. |