Ameba Ownd

アプリで簡単、無料ホームページ作成

How many concepts in java

2022.01.12 23:07




















Encapsulation lets us do that while keeping our original data private. It also lets us alter our original code without breaking it for others who have adopted it in the meantime. Inheritance is another labor-saving Java OOP concept that works by letting a new class adopt the properties of another. We call the inheriting class a subclass or a child class. The original class is often called the parent. We use the keyword extends to define a new class that inherits properties from an old class.


Polymorphism in Java works by using a reference to a parent class to affect an object in the child class. In method overriding , the child class can use the OOP polymorphism concept to override a method of its parent class.


This means a single method name might work in different ways depending on what arguments are passed to it. In the example below, encapsulation is demonstrated as an OOP concept in Java. Inheritance can be as easy as using the extends keyword:. For a full tutorial on the different ways to use inheritance in java, see this blog post.


The Employee class inherits from the Person class by using the keyword extends. Here, the child class overrides the parent class.


For the full example, see this blog post. The goal of OOP concepts in Java is to save time without sacrificing security and ease of use. The following best practices are all oriented toward advancing that main goal. For a good, full list of best practices for OOP concepts in Java, see this blog post.


Figure 8: Explains inheritance keywords. Relationships I. Generalization Generalization uses an IS-A relationship from a specialization class to generalization class. Figure 9: Generalization diagram II. Aggregation In this relationship, the existence of class A and B are not dependent on each other. Composition In this relationship, class B can not exist without class A — but class A can exist without class B.


Figure Class diagram with inheritance We can implement this diagram in Java to avoid code duplication. Advantages of inheritance Code reuse: the child class inherits all instance members of the parent class.


You have more flexibility to change code: changing code in place is enough. You can use polymorphism: method overriding requires IS-A relationship. Abstraction Abstraction is a process of hiding the implementation details and showing only functionality to the user.


Technically abstract means something incomplete or to be completed later. Abstract class An abstract class is one that contains the keyword abstract. Abstract methods An abstract method is one that contains the keyword abstract. Abstract class and Abstract methods If at least one abstract method exists inside a class then the whole class should be abstract.


We can have an abstract class with no abstract methods. We can have any number of abstract as well as non-abstract methods inside an abstract class at the same time.


The first concrete sub class of an abstract class must provide implementation to all abstract methods. If this doesn't happen, then the sub class also should be marked as abstract. To force sub classes to implement abstract methods. To stop having actual objects of that class. To keep having a class reference. To retain common class code. Interface An interface is a blueprint of a class.


NOTE: Interfaces only define required methods. We can not retain common code. For instance, Serializable, Cloneable, and Remote interfaces. They provide abstraction. They provide loose coupling: objects are independent from one another. When do we want to change a class to an interface? If you just want to define a required method, use an interface.


Polymorphism Polymorphism is the ability of an object to take on many forms. Polymorphism in OOP occurs when a super class references a sub class object. A reference variable can be declared as a class or interface type. Method overloading If a class has multiple methods that have same name but different parameters, this is known as method overloading.


Method overloading rules: Must have a different parameter list. May have different return types. May have different access modifiers. May throw different exceptions. It can only look in reference type for methods.


Outputs a method signature. Run time rules At runtime, JVM follows exact runtime type object type to find method.


Method overriding If a subclass has the same method as declared in the super class, this is known as method overriding. Method overriding rules: Must have the same parameter list. Must have the same return type: although a covariant return allows us to change the return type of the overridden method. Must not have a more restrictive access modifier: may have a less restrictive access modifier.


Must not throw new or broader checked exceptions: may throw narrower checked exceptions and may throw any unchecked exception. Only inherited methods may be overridden must have IS-A relationship. NOTE: Constructors can be overloaded but not overridden.


Student mary. Object type casting Java type casting is classified into two types: Widening casting implicit : automatic type conversion. Narrowing casting explicit : need explicit conversion. Conclusion Thank you everyone for reading. I hope this article helped you. I strongly encourage you to read more related articles on OOP. Checkout my original article series on Medium: Object-oriented programming principles in Java Please feel free to let me know if you have any questions.


Dream is not that which you see while sleeping it is something that does not let you sleep. Happy Coding! A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here. Coupling refers to the knowledge or information or dependency of another class. It arises when classes are aware of each other. If a class has the details information of another class, there is strong coupling.


In Java, we use private, protected, and public modifiers to display the visibility level of a class, method, and field. You can use interfaces for the weaker coupling because there is no concrete implementation.


Cohesion refers to the level of a component which performs a single well-defined task. A single well-defined task is done by a highly cohesive method. The weakly cohesive method will split the task into separate parts. The java. However, the java. Association represents the relationship between the objects. Here, one object can be associated with one object or many objects. There can be four types of association between the objects:.


Let's understand the relationship with real-time examples. For example, One country can have one prime minister one to one , and a prime minister can have many ministers one to many. Also, many MP's can have one prime minister many to one , and many ministers can have many departments many to many. Aggregation is a way to achieve Association. Aggregation represents the relationship where one object contains other objects as a part of its state.


It represents the weak relationship between objects. It is also termed as a has-a relationship in Java. Like, inheritance represents the is-a relationship. It is another way to reuse objects. The composition is also a way to achieve Association. The composition represents the relationship where one object contains other objects as a part of its state. There is a strong relationship between the containing object and the dependent object.


It is the state where containing objects do not have an independent existence. If you delete the parent object, all the child objects will be deleted automatically. We can provide the solution of real word problem if we are using the Object-Oriented Programming language. Object-based programming language follows all the features of OOPs except Inheritance. JavaScript and VBScript are examples of object-based programming languages. JavaTpoint offers too many high quality services.


Mail us on [email protected] , to get more information about given services. Please mail your requirement at [email protected] Duration: 1 week to 2 week. Java Training Basics of Java. Abstract class Interface Abstract vs Interface. Package Access Modifiers Encapsulation.


Can we overload the main method? A Java Constructor returns a value but, what? Can we create a program without main method? What are the six ways to use this keyword? Why is multiple inheritance not supported in Java? Why use aggregation? Can we override the static method?